One of the most annoying things in web development is dealing with the "Enter key" for forms. The "Enter key" has become a user's preference when submitting forms. Although we provide users with a submit button, the simplest and most direct way is still: enter text and press Enter to complete the submission
. ASP.NET 2.0 provides a good solution for this. Just assign the "defaultbutton" attribute to the ID of the button control you want to raise the event.
"defaultbutton" can be specified at both the form level and the panel level (<asp:panel> tag). When the default button is specified in both the form and the panel, if the "Enter key" is triggered in the panel,
the following example code in the execution panel has a form and 4 panels, and there are buttons in the order and panel. Attention: Which button events will be triggered after pressing Enter in the text box
<form id="form1" runat="server" defaultbutton="btn1">
<div>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:Button ID="Button5" runat="server" Text="Cancel" OnClick="Button5_Click" />
<asp:Button ID="btn1" runat="server" Text="Submit" OnClick="btn1_Click" />
<asp:Panel ID="pnl1" runat="server" defaultbutton="Button1">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />
</asp:Panel>
<asp:Panel ID="Panel1" runat="server" defaultbutton="Button2">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click" />
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" defaultbutton="Button3">
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
<asp:Button ID="Button3" runat="server" Text="Button3" OnClick="Button3_Click" />
</asp:Panel>
<asp:Panel ID="Panel3" runat="server" defaultbutton="Button4">
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
<asp:Button ID="Button4" runat="server" Text="Button4" OnClick="Button4_Click" />
</asp:Panel>
</div>
</form>
The corresponding, sample events for the button clicks are
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Button1.Text);
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write(Button2.Text);
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Write(Button3.Text);
}
protected void Button4_Click(object sender, EventArgs e)
{
Response.Write(Button4.Text);
}
protected void btn1_Click(object sender, EventArgs e)
{
Response.Write(btn1.Text);
}
protected void Button5_Click(object sender, EventArgs e)
{
Response.Write(Button5.Text);
}
Original text: http://forums.asp.net/thread/1270048.aspx
Original text: http://forums.asp.net/thread/1270048.aspx
I really don’t know how to translate "Entry key"
when downloading the code
. Please give me some advice.
Thanks