Click 和Command

来源:互联网 发布:录像软件bandicam 编辑:程序博客网 时间:2024/05/22 09:05

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.

0 0