Loading ...

traps-the-enter-key | CodeAsp.Net

Traps the enter key

 /5
0 (0votes)

One famous problem is to call selected button on the enter key.Suppose we have registration page and want when I fill all information and press enter key the submit button call.Another situation is on the login page when we press enter key login button call.In these cases we need to trap the enter key.this can we do in two  ways one for javascript and another from the C#.In C# code we also write the javascript code.
There is many cases like user want to trap the key for whole page i.e on the page when user press enter key the target button(which we want) Call,another way is when user on the inputcontrol(like text box) then press enter key then target bbutton call.
Below is The c# code to trap the enter key for all page

// Traps the enter key for all page and call the click event for the specified button control.
// defaultButton is the button to click when enter is pressed

public static void SetPageDefaultButton(Page page, WebControl defaultButton)
 {
   if (!page.Request.Browser.Browser.Equals("Firefox"))
    {
      // if browser is not fire fox.
      page.Form.DefaultButton = defaultButton.UniqueID;
    }
 else
 {
StringBuilder jscript = new StringBuilder();
jscript.Append("<script language=\"javascript\">");
jscript.Append("<!--");
jscript.Append("function PageDefaultButton(event, target) {");
jscript.Append("if ((event.keyCode == 13 || event.which == 13) 
&& !(event.target && (event.target.tagName.toLowerCase() == 'textarea'))) ");
 jscript.Append("{");
jscript.Append("var defaultButton = document.getElementById(target);");
jscript.Append("if (defaultButton == 'undefined') defaultButton = document.all[target]; ");
jscript.Append("if (defaultButton && typeof(defaultButton.click) != 'undefined') ");
jscript.Append("{");
jscript.Append("defaultButton.click();");
jscript.Append("event.cancelBubble = true;");
jscript.Append("if (event.stopPropagation) event.stopPropagation();");
jscript.Append("return false;");
jscript.Append("}}");
jscript.Append("return true;");
jscript.Append("}");
jscript.Append("// -->");
jscript.Append("</script>");
page.Form.Attributes.Add("onkeypress", "javascript:return PageDefaultButton(event,\'" + defaultButton.ClientID + "\')");
page.ClientScript.RegisterClientScriptBlock(string.Empty.GetType(), "PageDefaultButton", jscript.ToString());
 }
 }


By this function if user press enter key then always call defaultButton. call this function on the page load.

If we want when user on the specific input control and then press the enter key the specified button call.
Below is the c# code for that:
//Traps the enter key for a control and call the click event for the specified button control.
//The control to trap enter key after that input control.
//The client ID of the button to click when enter is pressed

 
public static void SetDefaultButton(WebControl inputControl, string buttonClientId)
{
    StringBuilder jscript = new StringBuilder();
    jscript.Append("<script language=\"javascript\">");

   jscript.Append("<!--");
   jscript.Append("function TrapEnterKey(e,n){");
   jscript.Append("var key;");
   jscript.Append("if(window.event) key = window.event.keyCode;");
   jscript.Append("else key = e.which;");
   jscript.Append("if (key == 13) {");
   jscript.Append("var o=document.getElementById(n);");
   jscript.Append("if(o!=null){");
   jscript.Append("o.click();");
   jscript.Append("return false;");
   jscript.Append("}}");
   jscript.Append("return true;");
   jscript.Append("}");
   jscript.Append("// -->");
   jscript.Append("</script>");
   inputControl.Attributes.Add("onkeypress", "return TrapEnterKey(event,\'" + buttonClientId + "\')");
   inputControl.Page.ClientScript.RegisterClientScriptBlock(string.Empty.GetType(), "TrapEnterKey", jscript.ToString());
}

Hope it is helpfull for all.

Comments (1)

   
Darren
I like to use a an ASP Panel with the Default Button propery set. If you do not want to actually show the button, like for a search box, then you can set display:none in the style property of the button (change square brackets to angled brackets).

[asp:Panel ID="pnlPanel" DefaultButton="btnGo" runat="server"]
Go to page: [asp:TextBox ID="txtGotoPage" runat="server"/]
[asp:Button ID="btnGo" OnClick="btnGo_Click" style="display:none" runat="server"/]
[/asp:Panel]
11/9/2009
 · 
by

Top Posts