ASP.NET2.0使用Enter Key作为默认提交问题分析(附源码

网页开发中最烦人的事情之一就是为表单处理"Enter key" ,"Enter key"已经成为用户提交表单的偏好。虽然我们为用户提供了提交按钮,但是最简单也是最直接的方式仍然是:输入文字,然后回车完成提交

ASP.NET 2.0中为此提供了很好的解决方法。只需要将"defaultbutton"属性指定到想要引发事件的按钮控件的ID上就可以了。

在表单级别和面板级别(<asp:panel> 标记)均可以指定"defaultbutton"。当表单和面板中同时指定了defaultbutton,则如果在面板中触发了"Enter key",则执行面板中的

下面的实例代码中有一个表单和4个面板,报单和面板中都有按钮。情各位注意:在文本框中回车后会触发哪些按钮的事件

<form runat="server" defaultbutton="btn1"> <div> <asp:TextBox runat="server"></asp:TextBox> <asp:Button runat="server" Text="Cancel" /> <asp:Button runat="server" Text="Submit" /> <asp:Panel runat="server" defaultbutton="Button1"> <asp:TextBox runat="server"></asp:TextBox> <asp:TextBox runat="server"></asp:TextBox> <asp:Button runat="server" Text="Button1" /> </asp:Panel> <asp:Panel runat="server" defaultbutton="Button2"> <asp:TextBox runat="server"></asp:TextBox> <asp:TextBox runat="server"></asp:TextBox> <asp:Button runat="server" Text="Button2" /> </asp:Panel> <asp:Panel runat="server" defaultbutton="Button3"> <asp:TextBox runat="server"></asp:TextBox> <asp:TextBox runat="server"></asp:TextBox> <asp:Button runat="server" Text="Button3" /> </asp:Panel> <asp:Panel runat="server" defaultbutton="Button4"> <asp:TextBox runat="server"></asp:TextBox> <asp:TextBox runat="server"></asp:TextBox> <asp:Button runat="server" Text="Button4" /> </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); }

完整实例代码代码点击此处本站下载

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwjpw.html