Event handler not firing in WebForms
i have a page which dynanically create a link when it load, after i click on the link it should loop in the database fetch all the record and display another set of link , then when i click on these link it should give me all information about this particular record it like this one
Q: when the page loads, it creates the first link which is associated with an event handler, it fire the first event handler (Getname) but it not firing the second event handler (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
page load and name link create
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
it fire nameload event handler
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
it not fire
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 Lite开发者_如何转开发ralControl(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
what am doing wrong thank
Its because your else condition is not executed, read my comments within the code.
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
精彩评论