Pages

Sunday, January 16, 2011

Validation Summary Control and Enter key submit ASP.NET

When you use enter key to sumbit data the validations are working properly but most of the time the validation summary not fired as a part of the validation. To over come this problem you can include some JavaScript code in the form or div section of the ASP.NET form.

How to get enter key to submit data while avoiding validation summary problem.

protected void Page_Load(object sender, EventArgs e)
{
       if (!Page.IsPostBack)
       {
             // Add new attribute to form1 event to fire when enter key submit button click event
             form1.Attributes.Add("onkeydown", "javascript: return WebForm_FireDefaultButton (event, '"  +      ButtonName.ClientID + "')");


             // Allow you to use enter key for sumbit data
             ClientScript.RegisterHiddenField("__EVENTTARGET", "ButtonName");
       }
}

// Then add this Page_Load event to div section on the form1
div id="inputArea" onkeypress="javascript:return WebForm_FireDefaultButton(event,'ButtonName')"

Hope this helps,
Cheers :)

0 comments: