事件处理程序执行两次 [英] Event Handler executes twice

查看:88
本文介绍了事件处理程序执行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用visual studio 2008编写一个Web应用程序。


如果在运行应用程序时单击了发送按钮,它的事件处理程序执行两次,原因我不知道和从而返回结果两次。


任何人都可以帮助我吗?以下是事件处理程序以防万一:


 
受保护 Sub btnSend_Click( ByVal 发​​件人作为 对象 ByVal e As System.EventArgs) Handles btnSend.Click
'用于保存结果的维数组
Dim UserArray 作为用户()= 用户(){}

btnSend.Enabled = False
'Label1.Text =""

'如果没有输入departmentId,请将d值设置为零以避开错误。否则解析输入的值。
Dim deptId As 整数
如果 txtDeptID.Text<> "" 然后
deptId = 整数 .Parse(txtDeptID.Text)
Else
deptId = 0
结束 如果

尝试
UserArray = myRequests.GetUsers(txtUserID.Text,txtFName.Text,txtLName.Text,txtOName.Text,deptId)
Catch ex As 异常
MsgBox(ex.Message)
结束 尝试

'检查是否有注册用户。
如果 UserArray Nothing < span style ="color:Blue;">然后
'标签1.Text ="没有要显示的结果"
btnSend.Enabled = True
退出 Sub
End 如果

'clear gridview添加新项目
'tableUsers.Rows.Clear()

'检索当前用户的属性并添加到表
对于 每个 eachUser UserArray
'为当前正在检索的用户声明一个新的tableRow
Dim ro As New TableRow

对于 i 作为 整数 = 0 To 3
Dim celCol As TableCell = New TableCell

选择 案例 i
Case 0
celCol.Text = eachUser.userID
Case 1
celCol.Text = eachUser.firstName
Case 2
celCol.Text = eachUser.lastName
案例 3
celCol.Text = eachUser.otherName
End 选择
'将当前单元格添加到行集合中。
ro.Cells.Add(celCol)
Next
''最后,将行添加到表
tableUsers.Rows。添加(ro)
下一步

btnSend.Enabled = True
结束 Sub

解决方案

尝试搜索"btnSend.Click + =";也许您以编程方式在设计器文件或其他地方以及标记代码中注册事件。


 


 protected void Page_Load(object sender,EventArgs e)
{
this.btnSend.Click + = this.btnSend_Click;
}

protected void btnSend_Click(object sender,EventArgs e)
{
// ...
}


i am writing a web application using visual studio 2008.

if the send button is clicked while running the application, it's event handlers executes twice for reasons i dont know and thereby returns the result two times.

can anyone help me? the event handler is below in case it may help:

Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
    'dimension array to hold results
    Dim UserArray As User() = New User() {}

    btnSend.Enabled = False
    'Label1.Text = ""

    'if no departmentId is entered, set d value to zero to evade error. else parse the value entered.
    Dim deptId As Integer
    If txtDeptID.Text <> "" Then
      deptId = Integer.Parse(txtDeptID.Text)
    Else
      deptId = 0
    End If

    Try
      UserArray = myRequests.GetUsers(txtUserID.Text, txtFName.Text, txtLName.Text, txtOName.Text, deptId)
    Catch ex As Exception
      MsgBox(ex.Message)
    End Try

    'check if there are any registered users.
    If UserArray Is Nothing Then
      'Label1.Text = "There are no results to show"
      btnSend.Enabled = True
      Exit Sub
    End If

    'clear gridview to add new items
    'tableUsers.Rows.Clear()

    'retrieve the properties of the current user and add to table
    For Each eachUser In UserArray
      'declare a new tableRow for current user being retrieved
      Dim ro As New TableRow

      For i As Integer = 0 To 3
        Dim celCol As TableCell = New TableCell

        Select Case i
          Case 0
            celCol.Text = eachUser.userID
          Case 1
            celCol.Text = eachUser.firstName
          Case 2
            celCol.Text = eachUser.lastName
          Case 3
            celCol.Text = eachUser.otherName
        End Select
        'add current cell to the row collection.
        ro.Cells.Add(celCol)
      Next
      'finally, add the row to the table
      tableUsers.Rows.Add(ro)
    Next

    btnSend.Enabled = True
  End Sub

解决方案

Try to search for "btnSend.Click +=" maybe you register the event programmatically in the designer file or somewhere else and in Markup code itself.

 

protected void Page_Load(object sender, EventArgs e)
{
   this.btnSend.Click += this.btnSend_Click;
}

protected void btnSend_Click(object sender, EventArgs e)
{
  // ...
}


这篇关于事件处理程序执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆