Contact Form Macro Problem - "Error Reading UserControl"
Hi guys,
I've made a contact form for my website which works fine, however I want to use it to be used as a usercontrol so that I can change the mail settings through macro parameters.
I'm so confused, I have no idea what's going wrong.
This is the code so far:
ASCX File
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Contact.ascx.cs" Inherits="Contact_Form.Contact" %>
Have you tried copying the DLL file to /bin/ also? I guess theoretically you shouldn't have to since you are copying the codebehind - but I think to do this without the DLL you need to change something in the page directive. Anyway - try copying the DLL and give it a shot?
I am new. I create a user control name Contact.ascx then i put the all first code into the this file. then i copy the next file code and put it into the Contact.ascx.cs. But the problem is thet when i build the solution it show many error. Please could you infor me more details how do i put this code in it. Thank you.
Contact Form Macro Problem - "Error Reading UserControl"
Hi guys,
I've made a contact form for my website which works fine, however I want to use it to be used as a usercontrol so that I can change the mail settings through macro parameters.
I'm so confused, I have no idea what's going wrong.
This is the code so far:
ASCX File
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Contact.ascx.cs" Inherits="Contact_Form.Contact" %>
<!--Contact Form and Validation Messages-->
<div id="contactform" runat="server" visible="true">
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="BulletList" />
<table id="contact-form">
<tr>
<td>Name:</td>
<td><asp:TextBox ID="txtName" CssClass="onelinetxtbox" runat="server"></asp:TextBox></td>
<td class="validate"><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
ErrorMessage="You must enter your name" SetFocusOnError="true">*</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>E-mail:</td>
<td><asp:TextBox ID="txtEmail" CssClass="onelinetxtbox" runat="server"></asp:TextBox></td>
<td class="validate"><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtEmail"
ErrorMessage="You must enter your email address" SetFocusOnError="true">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail"
ErrorMessage="You must enter a valid email address e.g [email protected])" ValidationExpression=
"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" SetFocusOnError="true">*</asp:RegularExpressionValidator></td>
</tr>
<tr>
<td>Subject:</td>
<td><asp:TextBox ID="txtSubject" CssClass="onelinetxtbox" runat="server"></asp:TextBox></td>
<td class="validate"><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtSubject"
ErrorMessage="You must enter a subject" SetFocusOnError="true">*</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>Your message:</td>
<td><asp:TextBox ID="txtComments" CssClass="multilinetxtbox" TextMode="MultiLine" runat="server"></asp:TextBox></td>
<td class="validate"><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtComments"
ErrorMessage="You must enter a message" SetFocusOnError="true">*</asp:RequiredFieldValidator></td>
</tr>
<tr><td></td>
<td><asp:Button ID="btnSend" runat="server" Text="Send" CssClass="contactsendbtn" /></td></tr>
</table>
</div><!--contact form-->
<!--Thankyou message which is visible when message is sent successfully-->
<div id="thankYou" runat="server" visible="false">
<h2>
<asp:Literal ID="thankyouHeader" runat="server"></asp:Literal>
</h2>
<asp:Literal ID="thankyouMessage" runat="server"></asp:Literal>
</div>
ASCX.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using umbraco;
namespace Contact_Form
{
public partial class Contact : System.Web.UI.UserControl
{
#region Umbraco Properties
private string _serverName, _emailTo, _userName, _userPassword;
private int _serverPort;
public string serverName
{
get
{
return _serverName;
}
set
{
_serverName = value;
}
}
public string emailTo
{
get
{
return _emailTo;
}
set
{
_emailTo = value;
}
}
public string userName
{
get
{
return _userName;
}
set
{
_userName = value;
}
}
public string userPassword
{
get
{
return _userPassword;
}
set
{
_userPassword = value;
}
}
public int serverPort
{
get
{
return _serverPort;
}
set
{
_serverPort = value;
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
btnSend.Click += new EventHandler(btnSend_Click);
}
protected void btnSend_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
{
return;
}
string email;
string name;
string emailname;
//combine name and email for friendly display name
name = "\"" + txtName.Text + "\" ";
email = txtEmail.Text;
emailname = name + email;
SendMail(emailname, txtSubject.Text, txtComments.Text);
}
private void SendMail(string from, string subject, string body)
{
/*Set properties from Umbraco into mail settings
===========================================================
*/
string mailServerName = serverName;
int mailServerPort = serverPort;
string toAddress = emailTo;
string username = userName;
string password = userPassword;
SmtpClient mailClient = new SmtpClient(mailServerName,
mailServerPort);
mailClient.Host = mailServerName;
mailClient.Credentials = new NetworkCredential(username,
password);
mailClient.EnableSsl = true;
using (MailMessage message = new MailMessage(from,
toAddress,
subject,
body))
mailClient.Send(message);
/*Set Thankyou text from thankyou div
========================================================
*/
thankyouHeader.Text = "Thank you";
thankyouMessage.Text = "We will contact you shortly.";
/*
========================================================
Hide & Show divs to show thank you message and hide contact form div
========================================================
*/
contactform.Visible = false; //Hide contact form div
thankYou.Visible = true; //show thank you message div
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using umbraco;
namespace Contact_Form
{
public partial class Contact : System.Web.UI.UserControl
{
#region Umbraco Properties
private string _serverName, _emailTo, _userName, _userPassword;
private int _serverPort;
public string serverName
{
get
{
return _serverName;
}
set
{
_serverName = value;
}
}
public string emailTo
{
get
{
return _emailTo;
}
set
{
_emailTo = value;
}
}
public string userName
{
get
{
return _userName;
}
set
{
_userName = value;
}
}
public string userPassword
{
get
{
return _userPassword;
}
set
{
_userPassword = value;
}
}
public int serverPort
{
get
{
return _serverPort;
}
set
{
_serverPort = value;
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
btnSend.Click += new EventHandler(btnSend_Click);
}
protected void btnSend_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
{
return;
}
string email;
string name;
string emailname;
//combine name and email for friendly display name
name = "\"" + txtName.Text + "\" ";
email = txtEmail.Text;
emailname = name + email;
SendMail(emailname, txtSubject.Text, txtComments.Text);
}
private void SendMail(string from, string subject, string body)
{
/*Set properties from Umbraco into mail settings
===========================================================
*/
string mailServerName = serverName;
int mailServerPort = serverPort;
string toAddress = emailTo;
string username = userName;
string password = userPassword;
SmtpClient mailClient = new SmtpClient(mailServerName,
mailServerPort);
mailClient.Host = mailServerName;
mailClient.Credentials = new NetworkCredential(username,
password);
mailClient.EnableSsl = true;
using (MailMessage message = new MailMessage(from,
toAddress,
subject,
body))
mailClient.Send(message);
/*Set Thankyou text from thankyou div
========================================================
*/
thankyouHeader.Text = "Thank you";
thankyouMessage.Text = "We will contact you shortly.";
/*
========================================================
Hide & Show divs to show thank you message and hide contact form div
========================================================
*/
contactform.Visible = false; //Hide contact form div
thankYou.Visible = true; //show thank you message div
}
}
}
I then added the aspx, aspx.cs and aspx.designer.cs files to the umbraco usercontrols folder and created a macro called 'Contact Form'.
The .net usercontrol was then selected and when I clicked on browse properties I keep getting the following error:
If anybody could help me out it would be much appreciated as I'm pulling my hair out not knowing what has gone wrong.
Thanks,
Daniel
Hi Daniel,
Have you tried copying the DLL file to /bin/ also? I guess theoretically you shouldn't have to since you are copying the codebehind - but I think to do this without the DLL you need to change something in the page directive. Anyway - try copying the DLL and give it a shot?
-Tom
Thanks Tom that's exactly what the problem was!
I I had already tried that but thought I'd give it another go and it's working fine this time!
Thanks again,
Daniel
I am new. I create a user control name Contact.ascx then i put the all first code into the this file. then i copy the next file code and put it into the Contact.ascx.cs. But the problem is thet when i build the solution it show many error. Please could you infor me more details how do i put this code in it. Thank you.
In User Control try changing CodeBehind to CodeFile
<%@ Control CodeBehind="FileName.ascx...."
<%@ Control CodeFile="FileName.ascx...."
is working on a reply...