Search code examples
asp.netpostbackviewstateaddhandlerpage-init

ASP VB Stuck on Dynamic Controls, Viewstate, and Postback. Could really use some help to get back on track


I've been reading posts and articles and just getting a little confused and consequently burning through time I don't have at the moment

Can someone look at my code and tell me where I've gone wrong?

    Partial Class PayerContacts
    Inherits System.Web.UI.Page

    Dim connStrDRContacts As String = ConfigurationManager.ConnectionStrings("DRContacts_SQL").ConnectionString

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        navBuild()
    End Sub

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

        If IsPostBack Then
            LoadContacts(ViewState("objSender"))
        End If

    End Sub

    Private Function navBuild() As Integer

        Dim SQLstrDRs As String = "SELECT * FROM DRList"
        Dim DbConnDRs As SqlConnection = New SqlConnection(connStrDRContacts)
        DbConnDRs.Open()
        Dim dtDRsTemp As New DataTable
        Dim SQLAdapterDRs As New SqlDataAdapter(SQLstrDRs, DbConnDRs)
        SQLAdapterDRs.Fill(dtDRsTemp)

        'Loop through each row of the DataView to create HTML table data

        Dim NewTableRow As New TableRow

        For Each row As DataRow In dtDRsTemp.Rows

    'CREATE table with button to display contacts related to client (one to many)   
            Dim NewTableButton As LinkButton = New LinkButton
            NewTableButton.ID = "btnDRName" & NewTableText
            NewTableButton.ViewStateMode = UI.ViewStateMode.Enabled
            AddHandler NewTableButton.Click, AddressOf LoadContacts

        Next

        Return 0

    End Function

    Protected Sub LoadContacts(sender As Object, e As EventArgs)

        Dim LoopCount As Integer = 0

        Dim SQLstrLoadTable As String = "SELECT * FROM ContactList WHERE DRVendor = '" & sender.Text.ToString & "'"
    and so on....
        SQLAdapterLoadTable.Fill(dtLoadTableTemp)

        Dim NewTableRow As New TableRow

        For Each row As DataRow In dtLoadTableTemp.Rows

            'CREATE Accordion to display data
            NewAccordion.ID = "ContactAccordion" & LoopCount
            NewAccordion.Visible = True
    blah, blah...

            'SET Pane
            NewAccordionPane.HeaderContainer.ID = "PaneHeader" & LoopCount
            NewAccordionPane.ContentContainer.ID = "PaneContent" & LoopCount

    'CREATE button to open ModalPopup to EDIT each record
            Dim imgGear As New ImageButton
            imgGear.ID = "btnGear" & row!ID.ToString
            imgGear.ViewStateMode = UI.ViewStateMode.Enabled
            AddHandler imgGear.Click, AddressOf EditRecord

            'LOAD Pane
            NewAccordionPane.HeaderContainer.Controls.Add(NewHeaderTable)
            NewAccordionPane.ContentContainer.Controls.Add(New LiteralControl(NewTableText))


        ViewState("objSender") = sender

    End Sub

    Protected Sub EditRecord(ByVal sender As Object, ByVal e As EventArgs)
        'Open ModalPopup to edit record
        popup.Show()
        pnlAddEdit.Visible = True

    End Sub

End Class

Solution

  • The Infinities Loop articles on ViewState and Dynamic Controls should really be read by every Webforms developer: -

    http://mocha.mojoskins.com/SharedFiles/Download.aspx?pageid=566&mid=786&fileid=38

    http://weblogs.asp.net/infinitiesloop/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_

    The examples are in C# but you should be able to figure out what's going on, it's the same base class library after all.