最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ASP.NET : Invoke codebehind method on Enter Key press in form - Stack Overflow

programmeradmin0浏览0评论

I have an asp form which has 3 text box and and a asp link button control.I want to invoke the button click event handler method when the user press enter key.I used asp panel and its default button property.Its working in IE.But not in other Browsers like firefox etc.. Is there any other method to do so ?

I have an asp form which has 3 text box and and a asp link button control.I want to invoke the button click event handler method when the user press enter key.I used asp panel and its default button property.Its working in IE.But not in other Browsers like firefox etc.. Is there any other method to do so ?

Share Improve this question edited May 7, 2011 at 17:56 Muhammad Akhtar 52.2k37 gold badges139 silver badges191 bronze badges asked Jun 20, 2009 at 10:28 ShyjuShyju 219k106 gold badges420 silver badges498 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You could try the DefaultButton Property (ASP.Net 2.0 and above). The ASP.Net Form and Panel controls both have a DefaultButton Property. The default action means the enter key should trigger the button click.

<form id="Form1" defaultbutton="SubmitButton" runat="server">
  <asp:panel id="panel1" defaultbutton="anotherbutton" runat="server">
  </asp:panel>
</form>

There is a known FireFox issue that may be resolved by adding: UseSubmitBehavior="False" to your submit button. This Blog describes the problem and solution in an UpdatePanel, it might work here as well.

Also check this question which has links to other possible solutions.

This seems to be a bug with FF3 (not sure), but the script that fixed is given below Keep it at the end of the page so that it overrides the WebForm_FireDefaultButton method rendered by ASP.NET.

var __defaultFired = false;

function WebForm_FireDefaultButton(event, target) { var element = event.target || event.srcElement;

if (!__defaultFired && event.keyCode == 13 && !(element && (element.tagName.toLowerCase() == "textarea"))) {
    var defaultButton;

    if (__nonMSDOMBrowser)
        defaultButton = document.getElementById(target);
    else
        defaultButton = document.all[target];

    if (defaultButton) {
        if(typeof(defaultButton.click) != "undefined")
            defaultButton.click();
        else
            eval(unescape(defaultButton.href.replace("javascript:", "")));

        event.cancelBubble = true;

        if (event.stopPropagation) event.stopPropagation();
        return false;
    }
}
return true;

}

发布评论

评论列表(0)

  1. 暂无评论