In this article I will explain, how to raise an event on server when enter key is pressed inside TextBox in ASP.Net.
There is no server side key press event for an ASP.Net TextBox, hence using a Panel and its Default Button trick we can achieve the same.
The HTML Markup consists of an ASP.Net TextBox control and a Button control wrapped inside an ASP.Net Panel control.
The Button is set as the DefaultButton for the Panel. When a enter key is pressed in any TextBox inside Panel with a DefaultButton then the Button Click event handler is raised. To know more about it refer my article Setting Default Button in ASP.Net Web Page
The Button control is made hidden by setting its style property with the CSS style display: none.
<asp:Panel runat="server" DefaultButton="Button1">
Name:<asp:TextBox ID="txtName" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="display: none" />
protected void Button1_Click(object sender, EventArgs e)
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Name: " + txtName.Text + "');", true);
Protected Sub Button1_Click(sender As Object, e As System.EventArgs)
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Name: " & txtName.Text & "');", True)