網頁開發中最煩人的事情之一就是為表單處理"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"應該如何翻譯,請各位指教。
謝謝