I have a webpage where there are two buttons and a textbox. When the page loads and user presses enter, the first button is clicked - and that is ok.
I would like to assure that if user enters the textBox and types anything there and THEN presses enter, the second button is clicked. When I enter the textBox I can tell that the first button will be used when I press enter, because it is a bit dark-blue-bordered then开发者_高级运维.
I tried javascript's
if (event && event.keyCode == 13)
document.getElementById("Button2").click();
but it somehow doesn't work. So my second thought was that there must be a possibility to tell the browser which button should defaultly react for an enter press when I'm in a specific textBox (the dark-blue-bordered button).
Edit: it might matter that first button is input and the second one is asp:Button
Edit2: Page.Form.DefaultButton is not an option, as there are somehow two default buttons. One should be clicked if user doesn't enter a textBox, other one if he is inside the textBox. Changing DefaultButton is not an option as this would require a postback after entering textBox by a user...
The Page.Form.DefaultButton
allows to set which button will react to the Enter
key.
<asp:Panel runat="server" DefaultButton="Button2" >
<asp:TextBox runat="server" />
<asp:Button ID="Button2" runat="server" Text="Button2" OnClientClick="alert('Button2')"/>
</asp:Panel>
As soon as you start using the textbox - button2 will be the default button. Up until then the other button will be the default
DefaultButton property
精彩评论