How does <ENTER> work on a webpage?
I have a webpage with a Master. The master contains
search1Button
with PostBackUrl="~/Search.aspx"
The child page contains
search2Button
with onclick="btnSearch_Click"
When a user hits <enter>
search1Button
will execute.开发者_如何学Go When nothing is active I can see how search1Button
can be viewed as the default. But, when the active textbox is say
search2TextBox
Things can be a little confusing when suddenly search1Button
executes instead of search2Button
.
A. How does the page decide what executes?
B. Does this have anything to do withForms
? If yes, does having only 1 Form limit my ability to control the situation (I cannot add more forms due to major problems experienced by me and discussed on SO with nested forms)? You can use the DefaultButton
attribute of the form.
You can do this using JavaScript, here is how can you do this..
FirstTextBox.Attributes.Add("onkeypress", "SetDefaultButton(this,'" + SearchButton1.ClientID + "')");
SecondTextBox.Attributes.Add("onkeypress", "SetDefaultButton(this,'" + SearchButton2.ClientID + "')");
function SetDefaultButton(objTextBox,objBtnID)
{
if(window.event.keyCode==13)
{
document.getElementById(objBtnID).focus();
document.getElementById(objBtnID).click();
}
}
</script>
精彩评论