事件处理程序不在WebForms中触发

时间:2011-06-09 11:16:26

标签: asp.net vb.net event-handling

我有一个页面在加载时动态创建链接,在我点击链接后它应该在数据库中循环获取所有记录并显示另一组链接,然后当我点击这些链接时它应该给我关于这个特定记录的所有信息,就像这个

Q:当页面加载时,它会创建与事件处理程序关联的第一个链接,它会触发第一个事件处理程序(Getname),但它不会触发第二个事件处理程序(GetnameDetails)

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        If ViewState.Item("nameload") IsNot Nothing Then
            If ViewState.Item("nameload").ToString = "True" Then
                Getname(Me, New EventArgs)
            ElseIf ViewState.Item("Getnameload").ToString = "True" Then
                GetnameDetails(Me, New EventArgs)
            Else
                CreateNamesButton()
            End If
        Else
            CreateNamesButton()
        End If

页面加载和名称链接创建

Private Sub CreateNamesButton()
        Dim btnName As New LinkButton
        btnName .Text = "Name"
        Panel1.Controls.Add(btnName)
        AddHandler btnName .Click, AddressOf Getname
        ViewState.Item("nameload") = False.ToString
    End Sub

它触发了nameload事件处理程序

Private Sub Getname(ByVal sender As Object, ByVal e As EventArgs)
        ' get products list here
        ' and set handlers for each link

        Dim testDb As New customer
        Dim arr As ArrayList = testDb.DbLoop()
        Dim ObjCustList As CustomerBo
        Dim CustInt As Integer
        Dim str As String

        Dim link As LinkButton

        For Each ObjCustList In arr
            CustInt = ObjCustList.Cus_Id
            link = New LinkButton
            Panel1.Controls.Add(New LiteralControl("<br />"))
            link.ID = CustInt
            link.Text = ObjCustList.FirstName
            link.CommandArgument = CustInt
            str = link.CommandArgument
            AddHandler link.Click, AddressOf GetnameDetails
            Me.Panel1.Controls.Add(link)

        Next ObjCustList

        ViewState.Item("nameload") = True.ToString

    End Sub

它没有开火

Private Sub GetnameDetails(ByVal sender As Object, ByVal e As EventArgs)

        Dim getRecord As New CustomerBo
        Dim lbLink As LinkButton = CType(sender, LinkButton)
        Dim CustomerID As String = lbLink.CommandArgument
        Dim getLink As String = lbLink.Text
        Dim SendCustID As Integer
        SendCustID = CInt(CustomerID)
        getRecord.Cus_Id = SendCustID

        Dim getAllcatInfo As New customer
        getRecord = getAllcatInfo.R_List(getRecord)
        Panel1.Controls.Add(New LiteralControl("<table><tr>"))
        Panel1.Controls.Add(New LiteralControl("<td colspan=2 align=center    valign=middle >"))
        Panel1.Controls.Add(New LiteralControl(getRecord.FirstName))
        Panel1.Controls.Add(New LiteralControl("</td>"))
        Panel1.Controls.Add(New LiteralControl("<td>"))
        Panel1.Controls.Add(New LiteralControl(getRecord.LastName))
        Panel1.Controls.Add(New LiteralControl("</td>"))
        Panel1.Controls.Add(New LiteralControl("<td>"))
        Panel1.Controls.Add(New LiteralControl(getRecord.Telephone))
        Panel1.Controls.Add(New LiteralControl("</td></tr></table>"))
        ViewState.Item("Getnameload") = True.ToString

        'get product details here
    End Sub

做错了什么,谢谢

1 个答案:

答案 0 :(得分:1)

因为你的其他条件没有被执行,请在代码中阅读我的评论。

 If ViewState.Item("nameload").ToString = "True" Then  // this is true
        Getname(Me, New EventArgs) // Executed
    ElseIf ViewState.Item("Getnameload").ToString = "True" Then // this is not executed and not attached to the handler
        GetnameDetails(Me, New EventArgs) // not executed
    Else