国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Button控件的onclick、oncommand、commandname、CommandArgument的區別

2024-07-10 13:29:17
字體:
來源:轉載
供稿:網友

Button控件
Button控件在執行后被解析為<input type="submit">代碼,而Button控件的優點就在于提交程序的同時能夠傳遞參數,傳遞參數是靠CommandName與CommandArgument屬性來完成的。
舉兩個Button控件的例子。
<% @ Page Language="C#" %>
<Script Language="C#" Runat="Server">
public void Button_Click(Object sender,EventArgs e)
{
     string argName = ((Button)sender).CommandName;

    //sender為一個對象,故CommandArgument為string
     string argArg = ((Button)sender).CommandArgument;
     Label1.Text="您選中的動作為:<font color=red>"+argName+"</font>,動作目標是:<font color=red>"+argArg+"</font>";
}

</script>
<html>
<head>
<title></title>
</head>
<body>
<b>Button控件演示</b>
<hr>
<form runat="server">
<asp:Button id="Button1" CommandName="踢" CommandArgument="足球" Text="動作1" runat="server" OnClick="Button_Click" /><br>
<asp:Button id="Button2" CommandName="打" CommandArgument="籃球" Text="動作2" runat="server" OnClick="Button_Click" /><br>
<hr>
<asp:Label id="Label1" runat="server" />
</form>
</body>
</html>
////////////////////////////////////
<% @ Page Language="C#" %>
<Script Language="C#" Runat="Server">
public void Button_Click(Object sender,CommandEventArgs e)
{
     string argName = e.CommandName;

    //CommandEventArgsr為一個類,故CommandArgument.ToString();
     string argArg = e.CommandArgument.ToString();
     Label1.Text="您選中的動作為:<font color=red>"+argName+"</font>,動作目標是:<font color=red>"+argArg+"</font>";
}

</script>
<html>
<head>
<title></title>
</head>
<body>
<b>Button控件演示</b>
<hr>
<form runat="server">
<asp:Button id="Button1" CommandName="踢" CommandArgument="足球" Text="動作1" runat="server" OnCommand="Button_Click" /><br>
<asp:Button id="Button2" CommandName="打" CommandArgument="籃球" Text="動作2" runat="server" OnCommand="Button_Click" /><br>
<hr>
<asp:Label id="Label1" runat="server" />
</form>
</body>
</html>
分析:1.兩個程序用不同的方法完成相同的功能和效果。
           2.類CommandEventArgs有兩個公共屬性,CommandName(用來獲取命令的名稱)和CommandArgument(用來獲取命令的參數)。二者相互關聯。
           3.兩個程序分別用OnClick與OnCommand激發事件,區別在于OnCommand是帶值傳遞的。
           4.所有Button類的Web控件(如ImageButton,LinkButton)均含有CommandName和CommandArgument這兩個屬性。

問題:上文中兩個程序采用的參數CommandEventArgs,EventArgs有什么區別?為什么在第一個程序中使用EventArgs,而不能使用CommandEventArgs(已經驗證,出現“Button_Click”的重載均與委托“System.EventHandler”不匹配)

例子:http://hi.baidu.com/fuhengyu/blog/item/b37e14fb3e6055264e4aea65.html

解決:在網上查資料得到的結果:

這類問題出現的原因就是在控件的一些事件里面調用了函數,而這些函數的參數與該事件所要用到的參數類型不一致。在MSDN中搜索OnPageIndexChanging、OnPageIndexChanged等關鍵字就可以得到:

C#
protected virtual void OnPageIndexChanged (EventArgs e)

C#
protected virtual void OnPageIndexChanging (DetailsViewPageEventArgs e)

C#
protected virtual void OnRowDeleting (GridViewDeleteEventArgs e)

C#
protected virtual void OnRowDeleted ( DataRowChangeEventArgs e)
這樣,在何種事件中使用什么樣的參數類型就一目了然了。還有后面的OnRowDeleted、OnRowDeleting也是同樣的道理。
相應事件:

public void MyList_Page(object sender, GridViewPageEventArgs e)
public void MyDataGrid_Delete(Object sender, GridViewDeleteEventArgs e)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涟源市| 曲阜市| 平邑县| 英山县| 扶沟县| 会泽县| 噶尔县| 策勒县| 凤凰县| 静宁县| 昭觉县| 卢氏县| 龙陵县| 嘉善县| 汉川市| 湖北省| 怀柔区| 都兰县| 县级市| 丘北县| 和龙市| 葵青区| 信宜市| 固原市| 台东县| 观塘区| 玛沁县| 达尔| 白玉县| 尖扎县| 云南省| 芒康县| 梅州市| 偃师市| 南涧| 邳州市| 黄陵县| 上虞市| 广德县| 华蓥市| 洛浦县|