0

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 . so How can I add event when press Enter?.

<asp:TextBox runat="server" id="txt_home">Add Comment</asp:TextBox>
udondan
  • 57,263
  • 20
  • 190
  • 175
Myworld
  • 1,861
  • 16
  • 45
  • 75

2 Answers2

1

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: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".

udondan
  • 57,263
  • 20
  • 190
  • 175
Aristos
  • 66,005
  • 16
  • 114
  • 150
-2

The most usual event in a textbox is the 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. Then you could check if the key pressed is the Enter key. For example:

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

Hope I helped!

udondan
  • 57,263
  • 20
  • 190
  • 175
Pantelis Natsiavas
  • 5,293
  • 5
  • 21
  • 36