网页开发中最烦人的事情之一就是为表单处理"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" />
<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);
}
原文:http://forums.asp.net/thread/1270048.aspx
原文:http://forums.asp.net/thread/1270048.aspx
代码下载
实在不知道"entry key"应该如何翻译,请各位指教。
谢谢
新闻热点
疑难解答
图片精选