简体   繁体   中英

default LinkButton on enter in a Master Page

I looked through and through and found no clean solution for this. A colleague of mine did find a pretty dirty solution but I don't see why the solutions in the links reference below does not work when I place it in a master page. I have a master page that basically has a textbox and a linkbutton, when I hit the enter key, the default button for the child page gets called instead.

  1. http://weblogs.asp.net/jeff/archive/2005/07/26/420618.aspx
  2. set linkbutton as default button for asp:panel in asp.net (Ahmad's Solution)

any insight on this please? thanks :)

I will post the dirty solution when I get to work tomorrow.

EDIT: here's the dirty (i think) solution:

string id = ctlToClick.UniqueID;
string someJavascript = //see below
EmailTextBox.Attributes.Add("OnKeyPress", someJavascript);

The javascript (placed it here so StackOverflow can format it better):

javascript:if (window.event){
    if(parseInt(window.event.keyCode) == 13){
    WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('" + id + "', '', true, 'Login', '', false, true));
    return false;
    }
}

Is this really the only solution for this?

Did you try setting the defaultButton property?

<form id="Form1"
        defaultbutton="lnkNextPage" 
        runat="server">

Where lnkNextPage is the LinkButton control's id.

I think you need a simple javascript to control your tab/enter keys. I have run into this before an there was no way to make the default button solution work.

The idea is simple - check each key that is entered while in the text box. If the key presed is enter or tab, find the button either by jquery or document.GetElementById('id') and invoke the click event on the button.

the form's default button doesn't work in asp.net since a): there is only 1 form, and b): for different text boxes you want to be able to control which button is actually clicked (on tab/enter).

A simple javascript file solves this.

<bleepzter/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM