Seem to hear this question alot. "When the user is in a text box how do I control what the default button is when they press "? Here's some sample VB code with two text boxes and two buttons. It shows how to code which button is clicked when enter is pressed
in a particular text box: [CodeFront ASPX]
<form id="Form1" method="post" runat="server">
This text box fires Button B
This text box fires Button A
Enter some txt and then press Enter in one of the text boxes
</form>
[CodeBehind Aspx.Vb]
Public Class DualingSubmits
Inherits System.Web.UI.Page
Protected WithEvents txtA As System.Web.UI.WebControls.TextBox
Protected WithEvents txtB As System.Web.UI.WebControls.TextBox
Protected WithEvents btnA As System.Web.UI.WebControls.Button
Protected WithEvents btnB As System.Web.UI.WebControls.Button
Protected WithEvents lblOut As System.Web.UI.WebControls.Label
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtA.Attributes.Add("onkeydown", "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document.forms(0).btnB.click();return false;} else return true;")
txtB.Attributes.Add("onkeydown", "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document.forms(0).btnA.click();return false;} else return true;")
End Sub
Private Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click
lblOut.Text = "Button A was clicked!"
End Sub
Private Sub btnB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click
lblOut.Text = "Button B was clicked!"
End Sub
End Class
Can you explain why when using the code above, the '&&' in the Attribute.Add() method are encoded into && when sent to the client? This is breaking the code, and a bit annoying to boot. All help appreciated. Brian
Yep, I found it annoying. It worked for me, though. I've read from MS that .Net encodes anything in the attributes for security reasons. You can get around it by moving the code to a javascript function which you can add to the page using RegisterClientScriptBlock
or something. Then add the function call to the attribute instead.
my only problem is that when I hit enter on the second text box the first button is clicked. here's my attributes code: [code] TextBox1.Attributes.Add("onkeydown", "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document.forms(0).button1.click();return
false;} else return true;") TextBox2.Attributes.Add("onkeydown", "if ((event.which && even.which == 13) || (event.keyCode && event.keyCode == 13)) {document.forms(0).button2.click();return false;} else return true;")[/code]
ok...my problem was a stupid one. If you leave your buttons named Button1 and Button2...this code won't work. Apparently Internet Explorer ALWAYS clicks Button1 first...how insane is that? as soon as I changed the names from Button1 to Button2 (without changing
ANY code) it worked perfectly good job man
billupton.co...
Participant
1835 Points
365 Posts
Enter Key fires Chosen Submit Button
Dec 11, 2003 02:54 AM|LINK
<form id="Form1" method="post" runat="server"> This text box fires Button B This text box fires Button A Enter some txt and then press Enter in one of the text boxes </form>[CodeBehind Aspx.Vb]Public Class DualingSubmits Inherits System.Web.UI.Page Protected WithEvents txtA As System.Web.UI.WebControls.TextBox Protected WithEvents txtB As System.Web.UI.WebControls.TextBox Protected WithEvents btnA As System.Web.UI.WebControls.Button Protected WithEvents btnB As System.Web.UI.WebControls.Button Protected WithEvents lblOut As System.Web.UI.WebControls.Label #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtA.Attributes.Add("onkeydown", "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document.forms(0).btnB.click();return false;} else return true;")
txtB.Attributes.Add("onkeydown", "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document.forms(0).btnA.click();return false;} else return true;")
End Sub
Private Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click
lblOut.Text = "Button A was clicked!"
End Sub
Private Sub btnB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click
lblOut.Text = "Button B was clicked!"
End Sub
End Class
bslezak
Member
80 Points
16 Posts
Re: Enter Key fires Chosen Submit Button
Dec 13, 2003 08:27 PM|LINK
billupton.co...
Participant
1835 Points
365 Posts
Re: Enter Key fires Chosen Submit Button
Dec 14, 2003 01:54 AM|LINK
Pselus
Member
10 Points
2 Posts
Re: Enter Key fires Chosen Submit Button
Jan 19, 2004 10:20 PM|LINK
Pselus
Member
10 Points
2 Posts
Re: Enter Key fires Chosen Submit Button
Jan 19, 2004 10:45 PM|LINK