摘要:Button click與command事件
讓不同的Button執行相同的函式
以下兩句都是執行Button1_Click函式
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button1_Click"/>
以下兩句都是執行Button3_Command函式
<asp:Button ID="Button3" runat="server" Text="Button"
oncommand="Button3_Command" commandname="button3"/>
<br />
<asp:Button ID="Button4" runat="server" Text="Button"
oncommand="Button3_Command" commandname="button4"/>
---------------------------------------------------------------------------------------------------------------------------------------------
OnClientClick是給ASP元件使用的呼叫JAVASCRIPT,若是不希望某些條件繼續執行後端則後面值設為FALSE
HTML元件的onclick則是直接呼叫JAVASCRIPT
-----------------------------------------------------------------------------------------------------------------------------------------------
如果使用protected void Button3_Command(object sender, CommandEventArgs e) { //// }
sender : 觸發此函式的來源物件
e : 傳遞該事件的額外描述
- 若是要使用Command事件,commandname或commandargurment記得要設值
- button_click函式內可這樣呼叫 Button1.Commandname
- command_click 內 e.commandname
-------------------------------------------------------------------------------------------------------------------------------------------------
button_click取得來源物件id
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender; //榜到新的按鈕上面
Response.Write("剛剛按下按鈕的為" + btn.ClientID);
}
command_click取得來源物件ID
protected void Button3_Command(object sender, CommandEventArgs e)
{
Button btn = (Button)sender;
Response.Write("剛剛按下的按鈕為" + btn.CommandName.ToString());
}
---------------------------------------------------------------------------------------------------------