简体   繁体   English

使用C#按Enter时如何添加事件?

[英]How to add event when press enter using C#?

I have solution when add text in textbox and press Enter the textbox content inserted in DB , I tried to get the best solution for that but I couldn't . 当在文本框中添加文本并按Enter插入DB中的文本框内容时,我有解决方案,我试图为此寻求最佳解决方案,但我无法。 so How can I add event when press Enter ?. 所以当按Enter时如何添加事件。

<asp:TextBox runat="server" id="txt_home">Add Comment</asp:TextBox>

in asp.net the simplest way is to use a Panel that warps the textboxes, and what ever user fills, and set DefaultButton to the action you wont when the user press Enter for the included text boxes. 在asp.net中,最简单的方法是使用一个面板来扭曲文本框以及用户填写的内容,然后将DefaultButton设置为当用户按下Enter来包含的文本框时将执行的操作。

<asp:Panel runat="server" ID="pnlAct" DefaultButton="Button1">
    <asp:TextBox runat="server" id="txt_home">Add Comment</asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Panel>

Of course you can use custom javascript to capture the Enter and make custom post back, but asp.net all ready gives you a safe and "ready to use solution". 当然,您可以使用自定义javascript捕获Enter并进行自定义回发,但是asp.net都已准备就绪,可以为您提供一个安全且“立即可用的解决方案”。

The most usual event in a textbox is the TextChangedEvent . 文本框中最常见的事件是TextChangedEvent I suppose this is what you are looking for. 我想这就是您要寻找的。 The link provided, gives you also this example: 提供的链接也为您提供了以下示例:

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
   Label1.Text = Server.HtmlEncode(TextBox1.Text);
}

Alternatively, you could use javascript to capture a keydown or a keyup event. 另外,您可以使用javascript捕获按键按键事件。 Then you could check if the key pressed is the Enter key. 然后,您可以检查所按下的键是否为Enter键。 For example: 例如:

$(".input1").keyup(function (e) {
    if (e.keyCode == 13) {
        // Do something
    }
});

Hope I helped! 希望我能帮上忙!

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

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