| 代码说明:如果想在表单中添加多个按钮,而且想根据点击按钮来传递不同的信息,可以使用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>
|