Click 和Command

Methods

Description

OnClick

Raises the Click event.

OnCommand

Raises the Command event.

Events

Description

Click

This event is raised when the button is clicked and the form containing the button is submitted to the server.

Command

This event also is raised when the button is clicked. However, the values of the CommandName and CommandArgument properties are passed with this event.

When a button is clicked, the form containing the button is submitted, and both Click and Command events are raised. To handle a Click event, you add a subroutine to your page as follows:

 
 
Sub Button_Click( s As Object, e As EventArgs )
    End Sub
    

To handle a Command event, add a subroutine to your page like this:

 
 
Sub Buttom_Command( s As Object, e As CommandEventArgs )
    End Sub
    

The only difference between a Click event and a Command event is that additional information—the values of the CommandName and CommandArgs properties—is passed to the Command event.

In Listing 2.6, for example, the btnCounter_Click subroutine is associated with the Button control's Click event. This subroutine adds one to the value of the Button control's Text property.

Listing 2.6 Button.aspx
<Script Runat="Server">
    Sub btnCounter_Click( s As Object, e As EventArgs )
    btnCounter.Text += 1
    End Sub
    </Script>
    <html>
    <head><title>Button.aspx</title></head>
    <body>
    <form Runat="Server">
    <asp:Button
    id="btnCounter"
    text="1"
    OnClick="btnCounter_Click"
    Runat="Server"/>
    </form>
    </body>
    </html>
    

If you need to add multiple buttons to a form, and you want to pass different information depending on which buttons are clicked, you can use the Command event instead of the Click event. For example, the page in Listing 2.7 displays different values depending on which buttons are clicked.

Listing 2.7 ButtonCommand.aspx
<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>
    

When you click the first button in Listing 2.7, the value of the Label control named lblCounter is incremented by five. The CommandName property of the first button has the value Add, and the CommandArgument property has the value 5.

When you click the second button, the value of the Label control named lblCounter is decremented by 10. The CommandName property of the second button has the value Subtract, and the CommandArgument property has the value 10.

The values of both the CommandName and CommandArgument properties are retrieved from the CommandEventArgs parameter (the second parameter passed to the Button_Command subroutine).

You can assign anything you please to either the CommandName or CommandArgument properties. These properties accept any string value.

转载于:https://www.cnblogs.com/mephisto/articles/753756.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值