Search code examples
asp.netvb.netpostbackviewstate

Getting values of dynamically created controls created AFTER PageLoad/Init - ASP.NET


I have a panel with a form that has a drop down list with values 1-10. When a button on the panel is pressed, that panel is made invisible and a second made visible. On the second panel I have rows of controls being generated dynamically. The number of rows is determined by the selecteditem on the drop down list.

My problem is that when I want to carry out validation and then storing of the values of the contents of controls that are dynamically created, the controls are removed.

I understand this is to do with the lifecycle of the page and the persistence of the dynamically created controls but I have no idea to get round the problem. I have read many other similar questions on here and few have working examples of who to solve this so I am none the wiser. This includes suggestions that the dynamically created controls need to be created on pageload or pageinit, how can this be since we don't know how many to make?

To recap: these controls are NOT addedd when the page loads, these controls are only added AFTER a user has pressed a button. We don't know how many controls to make until the user has selected how many they want.

My code so far:

Partial Class View
Inherits SeatPlannerModuleBase
Implements IActionable
Dim valid As String = "success"

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
    Try

        Dim n As Integer = 0

        ' now, create n TextBoxes, adding them to the PlaceHolder TextBoxesHere
        For i As Integer = 0 To n - 1
            res_Placeholder.Controls.Add(New Label())
            booking_Placeholder.Controls.Add(New Label())

            title_Placeholder.Controls.Add(New TextBox())
            firstname_Placeholder.Controls.Add(New TextBox())
            surname_Placeholder.Controls.Add(New TextBox())
            ticketNum_Placeholder.Controls.Add(New TextBox())

        Next

        ' now, set the Text property of each TextBox
        IterateThroughBooking(Me)
        IterateThroughReservation(Me)
        IterateThroughTitle(Me)
        IterateThroughFirstname(Me)
        IterateThroughSurname(Me)
        IterateThroughTicketNum(Me)

        If Not Page.IsPostBack Then

        End If

    Catch exc As Exception        'Module failed to load
        ProcessModuleLoadException(Me, exc)
    End Try
End Sub

Protected Sub IterateThroughTitle(ByVal parent As Control)
    Dim count As Integer = 1

    For Each c As Control In title_Placeholder.Controls
        If c.[GetType]().ToString().Equals("System.Web.UI.WebControls.TextBox") AndAlso c.ID Is Nothing Then
            'DirectCast(c, TextBox).Text = "TextBox_Title " + count.ToString()
            DirectCast(c, TextBox).ID = "TextBox_Title_" + count.ToString()
            DirectCast(c, TextBox).CssClass = "DYN_TextBox"
            count += 1
        End If

        If c.Controls.Count > 0 Then
            IterateThroughTitle(c)
        End If
    Next
End Sub

Protected Sub CreateTextBoxes()

    Dim n As Integer = number_of_tickets_Ddl.SelectedValue

    ' now, create n TextBoxes, adding them to the PlaceHolder TextBoxesHere
    For i As Integer = 0 To n - 1
        res_Placeholder.Controls.Add(New Label())
        booking_Placeholder.Controls.Add(New Label())

        title_Placeholder.Controls.Add(New TextBox())
        firstname_Placeholder.Controls.Add(New TextBox())
        surname_Placeholder.Controls.Add(New TextBox())
        ticketNum_Placeholder.Controls.Add(New TextBox())

    Next

    ' now, set the Text property of each TextBox
    IterateThroughBooking(Me)
    IterateThroughReservation(Me)
    IterateThroughTitle(Me)
    IterateThroughFirstname(Me)
    IterateThroughSurname(Me)
    IterateThroughTicketNum(Me)

    Retreive_first_row()

End Sub

Protected Sub next_submit_Btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles next_submit_Btn.Click

    Step1_Pnl.Visible = False
    Step2_Pnl.Visible = True
    CreateTextBoxes()
    Button1.Attributes.Add("OnClick", "validate_step2(); return false;")

End Sub

Protected Sub next_submit2_Btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles next_submit2_Btn.Click

    step2_error_Lbl.Text = CType(Me.FindControl("TextBox_Title_2"), TextBox).Text

End Sub

End Class       

Solution

  • The dynamic created controls don't persist after postback, you have to recreate them everytime, it's a technological restriction. To know how many controls create, you could save a variable in session/viewstate/whatever and if you name them just like the previous state, the viewstate will be recovered though.

    Check these links for more information:

    http://weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_.aspx

    ASP.NET dynamically created controls and Postback