开发者

How does <ENTER> work on a webpage?

开发者 https://www.devze.com 2023-03-06 06:41 出处:网络
I have a webpage with a Master.The master contains search1Button with PostBackUrl=\"~/Search.aspx\" The child page contains

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 with Forms? 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号