I have a contact form which is used as a usercontrol. Everything works fine with it except I want to set the focus to the "thankyouHeader" div once the message has been sent successfully. I've used the "focus()" property for my validation error messages which works fine but when I use it for the "thankyouHeader" div (thankyouHeader.Focus();) it reloads the page to the top of the screen and does not set the focus.
Here is the aspx.cs code:
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 System.Text.RegularExpressions;
namespace Contact
{
public partial class Contact : System.Web.UI.UserControl
Focus() property not working in UserControl
Hi,
I have a contact form which is used as a usercontrol. Everything works fine with it except I want to set the focus to the "thankyouHeader" div once the message has been sent successfully. I've used the "focus()" property for my validation error messages which works fine but when I use it for the "thankyouHeader" div (thankyouHeader.Focus();) it reloads the page to the top of the screen and does not set the focus.
Here is the aspx.cs code:
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 System.Text.RegularExpressions;
namespace Contact
{
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);
}
#region Send Message
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, txtMessage.Text);
thankyouHeader.Focus();
}
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 have received your message and will contact you as soon as possible.";
/*
========================================================
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
}
#endregion
#region Validation
protected void RequiredName_ServerValidate(object source, ServerValidateEventArgs args)
{
if (txtName.Text == String.Empty)
{
txtName.CssClass = "onelinetxtbox error";
args.IsValid = false;
txtName.Focus();
}
else
{
txtName.CssClass = "onelinetxtbox";
args.IsValid = true;
}
}
protected void RequiredEmail_ServerValidate(object source, ServerValidateEventArgs args)
{
if (txtEmail.Text == String.Empty)
{
txtEmail.CssClass = "onelinetxtbox error";
args.IsValid = false;
txtEmail.Focus();
}
else
{
txtEmail.CssClass = "onelinetxtbox";
args.IsValid = true;
}
}
protected void CheckEmail_ServerValidate(object source, ServerValidateEventArgs args)
{
//Email Regex String
Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
// Check if there is NO match in Regex
if (!reg.IsMatch(txtEmail.Text))
{
//Does not match RegEx - Invalid email address
txtEmail.CssClass = "onelinetxtbox error";
args.IsValid = false;
txtEmail.Focus();
}
else
{
txtEmail.CssClass = "onelinetxtbox";
args.IsValid = true;
}
}
protected void RequiredSubject_ServerValidate(object source, ServerValidateEventArgs args)
{
if (txtSubject.Text == String.Empty)
{
txtSubject.CssClass = "onelinetxtbox error";
args.IsValid = false;
txtSubject.Focus();
}
else
{
txtSubject.CssClass = "onelinetxtbox";
args.IsValid = true;
}
}
protected void RequiredMessage_ServerValidate(object source, ServerValidateEventArgs args)
{
if (txtMessage.Text == String.Empty)
{
txtMessage.CssClass = "multilinetxtbox error";
args.IsValid = false;
txtMessage.Focus();
}
else
{
txtMessage.CssClass = "multilinetxtbox";
args.IsValid = true;
}
}
#endregion
}
}
Any help would be much appreciated,
Thanks,
Dan
Anyone?
is working on a reply...