網(wǎng)頁(yè)開(kāi)發(fā)中最煩人的事情之一就是為表單處理"enter key" ,"enter key"已經(jīng)成為用戶提交表單的偏好。雖然我們?yōu)橛脩籼峁┝颂峤话粹o,但是最簡(jiǎn)單也是最直接的方式仍然是:輸入文字,然后回車完成提交
asp.net 2.0中為此提供了很好的解決方法。只需要將"defaultbutton"屬性指定到想要引發(fā)事件的按鈕控件的id上就可以了。
在表單級(jí)別和面板級(jí)別(<asp:panel> 標(biāo)記)均可以指定"defaultbutton"。當(dāng)表單和面板中同時(shí)指定了defaultbutton,則如果在面板中觸發(fā)了"enter key",則執(zhí)行面板中的
下面的實(shí)例代碼中有一個(gè)表單和4個(gè)面板,報(bào)單和面板中都有按鈕。情各位注意:在文本框中回車后會(huì)觸發(fā)哪些按鈕的事件
<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
 代碼下載
實(shí)在不知道"entry key"應(yīng)該如何翻譯,請(qǐng)各位指教。
謝謝
新聞熱點(diǎn)
疑難解答
圖片精選