与AddHandler添加时无法触发CLICK事件 [英] Cannot get CLICK event to fire when added with AddHandler

查看:360
本文介绍了与AddHandler添加时无法触发CLICK事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我要在页面上添加未知数量的按钮,并试图使它们全部具有相同的click事件.下面是精简后的代码,该代码可以一直正常运行,直到显示按钮为止.一旦显示它们,其中之一就是 点击后,点击事件(OnClickLink)不会触发,并且页面会重置.任何帮助将不胜感激.

I am adding an unknown amount of button to a page and trying to get them all to have the same click event.  Below is the stripped down code, which runs fine right up until the buttons are displayed.  Once they are displayed and one of them are clicked, the click event (OnClickLink) does not fire and the page resets.  Any help would be appreciated.

Imports System.Data.Odbc
Imports System.Globalization

Partial Class shop_contractor
    Inherits System.Web.UI.Page

    Private Sub AddTableRecord(inCode As Long, inCon As String, inAd1 As String, inAd2 As String, inPhone As String)

        Dim row As HtmlTableRow
        Dim cell As HtmlTableCell
        Dim RowCount As Long

        RowCount = annuity.Rows.Count + 1

        row = New HtmlTableRow()

        If (RowCount Mod 2) = 1 Then
            'row.BgColor = "Gray"
            'row.BgColor = "#A7C942"
            row.BgColor = "#EAF2D3"
        End If

 
        cell = New HtmlTableCell()
        Dim lnkButton = New Button()
        lnkButton.ID = "edt" & inCode
        lnkButton.Text = inCode
        lnkButton.Width = 75
        lnkButton.CausesValidation = False

        AddHandler lnkButton.Click, AddressOf OnClickLink
        cell.Controls.Add(lnkButton)
        row.Cells.Add(cell)

        row.Cells.Add(cell)

        cell = New HtmlTableCell()
        cell.Controls.Add(New LiteralControl(inCon))
        row.Cells.Add(cell)

        dsp.Rows.Add(row)

    End Sub
   
    Public Sub SetCon()

        Dim querystring As String = ""
        Dim reader As OdbcDataReader

        querystring = "SELECT * FROM infile"
        Using connection As New OdbcConnection(ConfigurationManager.ConnectionStrings("FileConnection").ConnectionString)
            Dim command As New OdbcCommand(querystring, connection)
            connection.Open()
            reader = command.ExecuteReader
            While reader.Read
                AddTableRecord(reader("code"), reader("name"))
            End While
        End Using

    End Sub

    Protected Sub OnClickLink(ByVal sender As Object, ByVal e As EventArgs)

        Dim hldRow As String
        Dim hldRow1 As Integer
        Dim hldRow2 As Integer

        hldRow1 = 4
        hldRow2 = Len(sender.id) - 3

        hldRow = Mid(sender.id, hldRow1, hldRow2)

        Session("selectedrow") = hldRow

        Response.Redirect("nextpage.aspx")


    End Sub


End Class 





推荐答案

建议您将此选项添加到代码顶部.

Suggest you add this Option to the top of your code.

Option Strict On

您可能会发现查明可能的错误会有所帮助.签名可能不匹配?

You may find it helps to pinpoint possible errors. Possibly unmatched signature?

顺便说一句:请使用工具栏中的代码阻止工具发布代码.

BTW: please use the Code Block tool from the toolbar to post code.


这篇关于与AddHandler添加时无法触发CLICK事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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