简体   繁体   English

在按下Enter键时单击按钮,而不是单击控件

[英]Making a button click when pressing enter instead of clicking the control

I'm trying to make a button called btnSubmit submit my customer's information if they hit enter on the keyboard. 我试图让一个名为btnSubmit的按钮提交客户的信息(如果他们按键盘上的Enter)。 I know in VS2012 if I'm coding strictly C#, I can change the properties very easily and set up a button to be activated when enter is pressed. 我知道在VS2012中如果我严格地使用C#进行编码,我可以非常轻松地更改属性并设置一个在按下Enter键时将其激活的按钮。

Because I am coding in .net with C#, I do not have that option for my control. 因为我使用C#在.net中进行编码,所以我的控件没有该选项。 Any advice with coding? 有编码建议吗? Can't find anything. 什么都找不到

I am working with a web form. 我正在使用网络表单。

I think this will do the trick. 我认为这可以解决问题。

Don't attempt to trigger a button click. 不要试图触发按钮单击。 Essentially what you should do is refactor your code so that the event handlers are separated from the actual implementation. 本质上,您应该做的是重构代码,以使事件处理程序与实际实现分开。 eg 例如

void Form_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        CheckAnswer();
    }
}

public void button1_Click(object sender, EventArgs e)
{
    CheckAnswer();
}

private void CheckAnswer()
{
    // do your stuffs on button click 
}

您应该将UseSubmitBehavior属性用于按钮控件,并将其设置为TRUE例如

UseSubmitBehavior="true"

Create a Panel and set its DefaultButton property with the name of the submit button. 创建一个Panel并使用提交按钮的名称设置其DefaultButton属性。 Every time you hit 'enter' in any TextBox inside the panel, it will call button's OnClick event. 每次您在面板内的任何TextBox单击“输入”时,它都会调用按钮的OnClick事件。 Like this: 像这样:

<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"  />

</asp:Panel>

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

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