It is very Common practice to hit an enter key on a text box in order to submit a form. For example if you are designing a Login Screen it is frequently required that user just hit enter after filling in the password rather then to be forced to click on the login butto itself. ASP.Net by default will just post back the form without clicking the the specified button ..... Here is the code that you can call to tie your text box to desired button you want to get clicked .....
For e.g in the PageLoad event
Jawad.Web.WebControls.Helpers.TieButton(txtBoxPassword, lnkButtonSubmit);
Here is the code .... Just add a .cs class named Helpers.cs into your project or class library and copy the whole code there ....
using System;
using System.Web.UI;
namespace Jawad.Web.WebControls
{
///
/// Summary description for Helpers.
///
public class Helpers
{
///
/// This ties a textbox to a button.
///
///
/// This is the textbox to tie to. It doesn't have to be a TextBox control, but must be derived from either HtmlControl or WebControl,
/// and the html control should accept an 'onkeydown' attribute.
///
///
/// This is the button to tie to. All we need from this is it's ClientID. The Html tag it renders should support click()
///
public static void TieButton(Control TextBoxToTie, Control ButtonToTie)
{
string formName;
try
{
int i=0;
Control c = ButtonToTie.Parent;
// Step up the control hierarchy until either:
// 1) We find an HtmlForm control
// 2) We find a Page control - not what we want, but we should stop searching because we a Page will be higher than the HtmlForm.
// 3) We complete 500 iterations. Obviously we are in a loop, and should stop.
while(! (c is System.Web.UI.HtmlControls.HtmlForm) &! (c is System.Web.UI.Page) && i<500)
{
c=c.Parent;
i++;
}
// If we have found an HtmlForm, we use it's ClientID for the formName.
// If not, we use the first form on the page ("forms[0]").
if (c is System.Web.UI.HtmlControls.HtmlForm)
formName = c.ClientID;
else
formName = "forms[0]";
}
catch
{
//If we catch an exception, we should use the first form on the page ("forms[0]").
formName = "forms[0]";
}
// Tie the button.
TieButton(TextBoxToTie, ButtonToTie, formName);
}
///
/// This ties a textbox to a button.
///
///
/// This is the textbox to tie to. It doesn't have to be a TextBox control, but must be derived from either HtmlControl or WebControl,
/// and the html control should accept an 'onkeydown' attribute.
///
///
/// This is the button to tie to. All we need from this is it's ClientID. The Html tag it renders should support click()
///
///
/// This is the ClientID of the form that the button resides in.
///
public static void TieButton(Control TextBoxToTie, Control ButtonToTie, string formName)
{
// This is our javascript - we fire the client-side click event of the button if the enter key is pressed.
string jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document."+formName+".all['"+ButtonToTie.ClientID+"'].click();return false;} else return true; ";
// We attach this to the onkeydown attribute - we have to cater for HtmlControl or WebControl.
if (TextBoxToTie is System.Web.UI.HtmlControls.HtmlControl)
((System.Web.UI.HtmlControls.HtmlControl)TextBoxToTie).Attributes.Add("onkeydown",jsString);
else if (TextBoxToTie is System.Web.UI.WebControls.WebControl)
((System.Web.UI.WebControls.WebControl)TextBoxToTie).Attributes.Add("onkeydown",jsString);
else
{
// We throw an exception if TextBoxToTie is not of type HtmlControl or WebControl.
throw new ArgumentException("Control TextBoxToTie should be derived from either System.Web.UI.HtmlControls.HtmlControl or System.Web.UI.WebControls.WebControl", "TextBoxToTie");
}
}
}
}