职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 923|回复: 0

ASP.NET 2.0 - Enter Key - Default Submit Button

[复制链接]
joe 发表于 2007-1-25 13:44 | 显示全部楼层 |阅读模式
网页开发中最烦人的事情之一就是为表单处理\"Enter key\" ,\"Enter key\"已经成为用户提交表单的偏好。虽然我们为用户提供了提交按钮,但是最简单也是最直接的方式仍然是:输入文字,然后回车完成提交

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

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

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

<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\" />
<aspanel 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\" />
</aspanel>
<aspanel ID=\"anel1\" 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\" />
</aspanel>
<aspanel ID=\"anel2\" 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\" />
</aspanel>
<aspanel ID=\"anel3\" 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\" />
</aspanel>
</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);
}

原文:http://forums.asp.net/thread/1270048.aspx
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-10 16:51 , Processed in 0.116940 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表