简体   繁体   English

按下回车键时调用onClick事件上的特定按钮C#

[英]Call a specific button onClick event when the enter key is pressed C#

I'm trying to get a specific asp:button onclick event to fire when I press the enter key in a specific asp:textbox control. 当我在特定的asp:textbox控件中按下回车键时,我正试图获取一个特定的asp:按钮onclick事件。

The other factor to be taken into account is that the button is within a asp:Login control template. 另一个需要考虑的因素是该按钮位于asp:Login控制模板中。

I've no idea how to do this, suggestions on a postcard please. 我不知道怎么做,请给明信片上的建议。

您可以查看面板控件的DefaultButton属性。

You could set the DefaultButton property on the form. 您可以在窗体上设置DefaultButton属性。 Either as an attribute of the form tag in your markup DefaultButton = "btnSubmit" or using something like this in your code-behind: 要么作为标记DefaultButton =“btnSubmit”中的表单标记的属性,要么在代码隐藏中使用类似的东西:

Page.Form.DefaultButton = "btnSubmit"

You need to do it with javascript. 你需要用javascript来做。 It's really easy with jQuery. 使用jQuery非常简单

You can do something like (off the top of my head, not tested): 你可以做一些事情(在我的头顶,没有经过测试):

$('#myTextBox').keypress(function(e){
    if(e.which == 13)
        $('#myBtn').click();
});

Edit: Be aware that although jQuery works exceptionally cross browser, there are some quirks with keypress described here . 编辑:请注意,尽管jQuery在浏览器中工作异常,但是这里描述一些带有keypress的怪癖。

Whoops i didnt see you said the "enter key" i thought you said "any key", yeah in that case use DefaultButton on asp:panel 哎呀我没看到你说“输入密钥”我以为你说“任何密钥”,是的,在那种情况下使用默认的按钮在asp:panel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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