共用方式為


HOW TO:回應 Button Web 伺服器控制項事件

更新:2007 年 11 月

當按一下 ButtonLinkButtonImageButton Web 伺服器控制項時,則目前網頁會被送至伺服器中進行處理。

若要回應按鈕事件

  • 建立以下任一事件的事件處理常式:

    • 網頁的 Page_Load 事件。由於按鈕會固定將網頁張貼至伺服器,因此每次都會執行這個方法。在不需知道所按的按鈕是哪一個,而只要送出表單的情況下,請使用 Page_Load 事件。

    • 按鈕的 Click 事件。如果需要知道所按的按鈕是哪一個,則寫入這個事件的事件處理常式。

      注意事項:

      如果您使用的是 ImageButton 控制項,且需要知道使用者所按位置的 x 及 y 座標,則您必須建立這個事件的事件處理常式。如需詳細資訊,請參閱 HOW TO:判斷 ImageButton Web 伺服器控制項中的座標

    下列範例說明如何在使用者按一下 Button Web 伺服器控制項時進行回應。這個方法可在 Label Web 伺服器控制項中顯示訊息。

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Label1.Text="You clicked a button"
    End Sub
    
    public void Button1_Click (object sender, System.EventArgs e)
    {
       Label1.Text="You clicked a button.";
    }
    

    下列範例說明如何在 Page_Load 事件處理常式中,按一下按鈕時進行回應。這個方法會測試網頁的 IsPostBack 屬性來判斷這是否是第一次處理網頁,或是否是按一下按鈕所送出的。

    Private Sub Page_Load(ByVal Sender As System.Object, ByVal e _
        As System.EventArgs) Handles MyBase.Load
        If Not IsPostback Then
            ' This is called the first time the page has loaded.
            ' The user will not have been able to click any buttons yet.
        Else
            ' This is called if the form has been posted back, possibly
            ' by a button click.
            Me.Label1.Text = "You clicked a button."
        End If
    End Sub
    
    private void Page_Load(object sender, System.EventArgs e)
    {
       if (!IsPostBack)
       {
          // Evals true first time browser hits the page.
       }
       else
       {
          // This is called if the form has been posted back, possibly
          // by a button click.
          this.Label1.Text = "You clicked a button.";
       }
    }
    

    以下範例說明一個簡單的四則運算整數計算器。藉由將所有按鈕 (AddSubtractMultiplyDivide) 繫結至相同的方法,您可以一次處理所有計算並避免重複的程式碼。將按鈕繫結至 Calculate 方法是,利用 Visual Basic 中的 AddHandler 方法及 C# 中的 += 運算子來完成。若要確保輸入值都是整數,您可以將錯誤處理程式碼加入至 Calculate 方法,或者使用 Web Form 可用的驗證控制項。

    ' Set the CommandName property of the buttons to "Add", 
    ' "Subtract", "Multiply", and "Divide".
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       AddHandler Me.btnAdd.Click, AddressOf Calculate
       AddHandler Me.btnSubtract.Click, AddressOf Calculate
       AddHandler Me.btnMultiply.Click, AddressOf Calculate
       AddHandler Me.btnDivide.Click, AddressOf Calculate
    End Sub
    
    Public Sub Calculate(ByVal sender As Object, ByVal e As System.EventArgs)
       Dim op1 As Integer = CType(Me.TextBox1.Text, Integer)
       Dim op2 As Integer = CType(Me.TextBox2.Text, Integer)
       Dim result As Integer
       Select Case CType(sender, Button).CommandName
          Case "Add"
             result = op1 + op2
          Case "Subtract"
             result = op1 - op2
          Case "Multiply"
             result = op1 * op2
          Case "Divide"
             ' Divide two numbers and return an integer result.
             If op2 > 0 Then
                   result = op1 \ op2
             Else
                   result = 0
             End If
          Case Else
             ' Error handling code here.
       End Select
       Label1.Text = result.ToString()     
    End Sub
    
    // Set the CommandName property of the buttons to "Add", _
    // "Subtract", "Multiply", and "Divide".
    protected void Page_Load(object sender, EventArgs e)
    {
       btnAdd.Click += new System.EventHandler(this.Calculate);
       btnSubtract.Click += new System.EventHandler(this.Calculate);
       btnMultiply.Click += new System.EventHandler(this.Calculate);
       btnDivide.Click += new System.EventHandler(this.Calculate);
    }
    
    protected void Calculate (object sender, System.EventArgs e)
    {
       int op1 = Convert.ToInt16(TextBox1.Text);
       int op2 = Convert.ToInt16(TextBox2.Text);
       int result = 0;
       switch(((Button)sender).CommandName)
       {
          case "Add" :
             result = op1 + op2;
             break;
          case "Subtract" :
             result = op1 - op2;
             break;
          case "Multiply" :
             result = op1 * op2;
             break;
          case "Divide" :
             // Integer division.
             if (op2 > 0) 
                   result = op1 / op2;
             else
                   result = 0;
             break;
          default:
             // Error handling code here.
             break;
       }
       Label1.Text = result.ToString();
    }
    

請參閱

參考

Button Web 伺服器控制項概觀