locked
This is a sample for ASP.Net to show, how easy it is to 'force' a default button for a textbox. RRS feed

  • Question

  • User95761738 posted
    Submitted by : Dinesh Velupula, WSDOT Matlab Certified Java2 Developer, Microsoft Certified Professional - SQL Server2000 C# & VB.NET Expert Deveoper ------------------------------------------------------------------------------------------------ This is a sample made for Microsoft ASP.Net to show, how easy it is to 'force' a default button for a textbox. So far you have needed to install various DLL's or controls to control what button must be clicked, when a user is submitting some data from an textbox by pressing [ENTER] key. Now this is a lot easyer using only a single line of code (when the my small function is included) to relate/assign the [ENTER]-key event from a textbox to the wanted button. The sub-routine is as followes: Public Sub DefaultButton(ByRef Page As System.Web.UI.Page, ByRef objTextControl As TextBox, ByRef objDefaultButton As Button) ' Sets default buttons. ' Created by Janus Kamp Hansen - http://www.kamp-hansen.dk Dim sScript As New System.Text.StringBuilder() sScript.Append("<script language=""javascript"">" & vbCrLf) sScript.Append("function fnTrapKD(btn){" & vbCrLf) sScript.Append(" if (document.all){" & vbCrLf) sScript.Append(" if (event.keyCode == 13)" & vbCrLf) sScript.Append(" { " & vbCrLf) sScript.Append(" event.returnValue=false;" & vbCrLf) sScript.Append(" event.cancel = true;" & vbCrLf) sScript.Append(" btn.click();" & vbCrLf) sScript.Append(" } " & vbCrLf) sScript.Append(" } " & vbCrLf) sScript.Append("}" & vbCrLf) sScript.Append("</script>" & vbCrLf) objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." & objDefaultButton.ID & ")") Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString) End Sub This is how you use the sub-function inside you own code: DefaultButton(Page, TextBox1, Button1) DefaultButton(Page, TextBox2, Button2) DefaultButton(Page, TextBox3, Button3)
    Tuesday, February 4, 2003 2:31 AM

All replies

  • User1222855324 posted
    GREAT!
    Wednesday, February 5, 2003 10:23 AM
  • User-522307627 posted
    GREAT! x2!!! I changed the Button to an ImageButton and changed the line: objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." + objDefaultButton.ID + ")"); to: objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." + objDefaultButton.ClientID + ")"); because it is inside of a usercontrol and it works great! Thanks
    Monday, October 13, 2003 5:27 PM
  • User564684961 posted
    thank you guys very much! I have been searching all day for this solution, and it works!
    Wednesday, October 22, 2003 11:29 PM
  • User-376809674 posted
    Awesome work bro. We been looking for something liek this forever. John
    Thursday, October 23, 2003 4:10 PM
  • User-376809674 posted
    I have one question.... since I don't know js for a darn, what is the sinifigance of the text box parameter. usually, when i submit a form I want all the text box info submitted. It appears, in this example, that the text box parameter doesn't really do anything. John
    Thursday, October 23, 2003 4:30 PM
  • User2064163041 posted
    I have copied the code above EXACTLY as it is into a user control but when I try and run it I get the following error message: Compiler Error Message: BC30648: String constants must end with a double quote. Source Error: Line 55: Line 56: sScript.Append("</script>" & vbCrLf) I can't see anything wrong in this line. Anyone any ideas?
    Thursday, October 23, 2003 5:40 PM
  • Thursday, October 23, 2003 5:44 PM
  • User2064163041 posted
    Thank you. I changed to line to: sScript.Append(chr(60) & "/script>" & vbCrLf) and now it works fine. Cheers.
    Thursday, October 23, 2003 6:03 PM
  • User1928316552 posted
    to Velupula: i have a question, do this event doesn't need any postback from the page... thanks... regards, dave
    Monday, October 27, 2003 5:02 AM
  • User1554345290 posted
    I tried to use this script in conjunction with a user control, and received a javascript error when the key down event fired. The javascript function reported that the btn parameter was null or unrecognized. I changed the line: objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." & objDefaultButton.ID & ")") to read: objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." & objDefaultButton.ClientID & ")") Using ClientID appends the user control identifier and supplies the unique identification to the document that it needs when using user controls within a page.
    Tuesday, November 18, 2003 2:36 PM
  • User-207799750 posted
    Super code! works like a charm with the ..ClientId change. Here's the C# version of the method. Don't forget to import access to StringBuilder, "using System.Text;" protected void DefaultButton(System.Web.UI.Page Page, System.Web.UI.WebControls.TextBox txtControl, System.Web.UI.WebControls.Button btnDefault) { //code reference:http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=138589 //Created by Janus Kamp Hansen - http://www.kamp-hansen.dk StringBuilder sScript = new StringBuilder(); sScript.Append("<script language='javascript'>" + "\n"); sScript.Append("function fnTrapKD(btn){" + "\n"); sScript.Append(" if (document.all){" + "\n"); sScript.Append(" if (event.keyCode == 13)" + "\n"); sScript.Append(" { " + "\n"); sScript.Append(" event.returnValue=false;" + "\n"); sScript.Append(" event.cancel = true;" + "\n"); sScript.Append(" btn.click();" + "\n"); sScript.Append(" } " + "\n"); sScript.Append(" } " + "\n"); sScript.Append("}" + "\n"); sScript.Append("</script>"); txtControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." + btnDefault.ClientID + ")"); Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString()); }
    Thursday, December 18, 2003 6:38 PM
  • User913124150 posted
    Great!!!
    Sunday, December 28, 2003 10:43 PM
  • User-1449169821 posted
    Andy Smith has a great control for this already...very reliable, Designer support http://www.metabuilders.com/Tools/DefaultButtons.aspx
    Wednesday, January 7, 2004 8:49 AM
  • User-973072541 posted
    One problem I'm having with this is that if I want to use this code in an .ascx file, how do I know what the eventual id for the button is going to be?
    Wednesday, January 7, 2004 9:33 PM
  • User-973072541 posted
    BTW does anyone know how to make this work on Mozilla?
    Wednesday, January 7, 2004 10:44 PM
  • User-323455760 posted
    Hi, This code works very good for textboxes. But what to do if I have a textbox within a DataGrid? How to use the "DefautlButton(Page, ?????, ImageButton1)" function with a datagid? Thanks
    Friday, January 9, 2004 3:54 PM
  • User483808541 posted
    TOOL WORKS GREAT! It took me a bit to figure out to but the DefaultButton(Page, txtPassword, lnkLogin) in the page_load procedure and keep it outside my if page.ispostback = false statement, example: Private Sub Page_Load(....) DefaultButton(Page, txtPassword, Button1) If Page.IsPostBack = False Then .... End If Exit Sub
    Thursday, January 15, 2004 1:15 AM
  • User-635437160 posted
    Thank u thank u thank u!!!!!!!
    Monday, January 19, 2004 5:52 AM
  • User-130293678 posted
    Yeah, works as well with ImageButtons and LinkButtons... Keep up the good work!!!!!!
    Wednesday, January 21, 2004 8:54 AM
  • User690078794 posted
    Thanks! This is a smart code. It works!
    Tuesday, February 17, 2004 7:35 PM
  • User-1917735787 posted
    Doesn't seem to wirk with other browsers then IE
    Wednesday, February 18, 2004 1:49 PM
  • User-1696021186 posted
    That function is cool, but I've experienced some problems with adding JavaScript like that to a page. To accomplish that task, I usually use a single line in Page_Load: Private Sub Page_Load(... ) Handles MyBase.Load Page.RegisterHiddenField("__EVENTTARGET", "cmdButton") End Sub
    Tuesday, March 23, 2004 7:49 AM
  • User-1628317575 posted
    I had the same problem. Thank you!
    Friday, June 18, 2004 7:14 AM
  • User-46888941 posted
    Nice code :)
    Friday, June 18, 2004 10:02 AM
  • User1019781410 posted
    Has anyone got this working with browsers other than IE?
    Saturday, December 31, 2005 11:51 AM
  • User1941284925 posted
    i found similar code on this site:
    http://www.beansoftware.com/ASP.NET-Tutorials/Accept-Enter-Key.aspx

    again, it works in IE, but not in firefox.

    it looks like 2.0 has some default button functionality, but the way they describe it won't work in my situation.

    anyone have luck getting it to work elsewhere?

    c
    Monday, August 14, 2006 5:06 PM
  • User1726897791 posted

    Good point.

    Property "ID" and "ClientID" are different some time, within a user control is a good example.

    When the control is in a user control, the client side ID of the control will be prefixed with "UserControlName_", the nature of this solution is client side javascript, so got the ClientID is important.

    Wednesday, August 23, 2006 11:17 AM