如何:响应 Button Web 服务器控件事件
更新:2007 年 11 月
单击 Button、LinkButton 或 ImageButton Web 服务器控件时,当前页被提交给服务器并在那里进行处理。
响应按钮事件
为下列事件之一创建事件处理程序:
页的 Page_Load 事件。因为按钮总是将页发送给服务器,所以该方法总是在运行。如果只提交相应窗体,单击哪个按钮并不重要,则使用 Page_Load 事件。
按钮的 Click 事件。当了解哪个按钮被单击很重要时,编写该事件的事件处理程序。
说明: 如果使用的是 ImageButton 控件,并且想知道用户单击位置的 x 坐标和 y 坐标,则必须为该事件创建一个事件处理程序。有关详细信息,请参见如何:确定 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."; } }
下面的示例显示一个具有四项功能的简单的整数计算器。通过将所有按钮(“加”、“减”、“乘”和“除”)绑定到同一方法,可以在一个位置处理全部计算,从而避免重复代码。将这些按钮绑定到 Calculate 方法是通过使用 Visual Basic 中的 AddHandler 方法和 C# 中的 += 运算符完成的。为确保输入值为整数,可以将错误处理代码添加到 Calculate 方法,或者使用可用于 Web 窗体的验证控件。
' 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(); }