中国搜 程序源代码学习 编程源代码 直接应用在编程实践中 优秀编程书籍源码欣赏

收藏本页到>> 新浪ViVi  365Key网摘   IE收藏夹 |设为首页

 中国搜首页 >>ASP.NET相关>>Button的Command事件 C#版

代码说明:如果想在表单中添加多个按钮,而且想根据点击按钮来传递不同的信息,可以使用Command事件而不是Click事件。例如下面的程序清单,根据点击的按钮不同显示不同的值。
     CommandName和CommandArgument均是从CommandEventsArgs参数中获得。

<Script Runat="Server">
Sub Button_Command( s As Object, e As CommandEventArgs )
Dim intAmount As Integer
intAmount = e.CommandArgument
If e.CommandName = "Add" Then
lblCounter.Text += intAmount
Else
lblCounter.Text -= intAmount
End If
End Sub
</Script>

<html>
<head><title>ButtonCommand.aspx</title></head>
<body>
<form Runat="Server">
<asp:Button
Text="Add 5"
CommandName="Add"
CommandArgument="5"
OnCommand="Button_Command"
Runat="Server"/>
<asp:Button
Text="Subtract 10"
CommandName="Subtract"
CommandArgument="10"
OnCommand="Button_Command"
Runat="Server"/><p>
<asp:Label
id="lblCounter"
text="1"
Runat="Server"/>
</form>
</body>
</html>

   运行演示程序   

  ©2004   www.zhonguosou.com