Access Controls in ViewState List

Hello

I want to add multiple dynamic controls via the button onclick event

How do i maintain the viewstate of all the controls?

how do i identify each ctlID in my control list so i can create the controls after postback?

for the example below im unable to recreate the label control
Imports System.Collections.Generic
 
Partial Public Class cms_test
 
    Inherits System.Web.UI.Page
    Public controlCounter As Integer = 0
    Public myControlList As New List(Of String)()
 
    Protected Sub addControlButton_Click(ByVal sender As Object, ByVal e As EventArgs)
 
        controlCounter += 1
 
        Dim label As New Label()
        label.Text = "Label: "
        label.ID = "LabelID" + controlCounter.ToString()
 
        Dim textbox As New TextBox()
        textbox.Text = "text in textbox" + controlCounter.ToString()
        textbox.ID = "textbox" + controlCounter.ToString()
 
        Dim lineBreak As New LiteralControl("<br />")
 
        controlHolder.Controls.Add(label)
        controlHolder.Controls.Add(textbox)
        controlHolder.Controls.Add(lineBreak)
 
        myControlList.Add(textbox.ID)
        myControlList.Add(label.ID)
 
        ViewState("myControlList") = myControlList
 
    End Sub
 
    Protected Overloads Overrides Sub LoadViewState(ByVal savedState As Object)
        MyBase.LoadViewState(savedState)
        myControlList = DirectCast(ViewState("myControlList"), List(Of String))
 
        For Each ctlID As String In myControlList
 
            controlCounter += 1
 
            Dim textbox As New TextBox()
            textbox.ID = ctlID
            controlHolder.Controls.Add(textbox)
            Dim lineBreak As New LiteralControl("<br />")
            controlHolder.Controls.Add(lineBreak)
 
        Next
 
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            myControlList = New List(Of String)()
            ViewState("myControlList") = myControlList
        End If
    End Sub
 
End Class

Open in new window

miguel_jAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag ThakurTechnical ManagerCommented:
does the following link from 4GuysFromRolla any helpful
Dynamic Web Controls, Postbacks, and View State - http://aspnet.4guysfromrolla.com/articles/092904-1.aspx
0
jinn_hnnlCommented:
When you add controls to the page controls hierarchy dynamically, you have to add them on every post back. As soon as the controls are added to the page, its viewstate restored.

JINN


0
miguel_jAuthor Commented:
Thanks for your help

I was wondering how to identify each control from the list of have created in my code [line 38]

line 27 and 28 add the unique id to the list, but how do i distinguish each one when i recreate the controls after line 38

i have manged to restore the viewstate successfully when managing just one dynamic control (a texbox) but have trouble when i want to add multiple dynamic controls at a time
0
jinn_hnnlCommented:
Actually what you are doing in the override LoadViewState method is adding new control, and try to restore it's previous set ID which stored in a List.

Your problem seem to be when you have multiple type of control then you dont know how you distinguish them, whether one is Label or whether 1 is TextBox right?

There are 2 possible workarounds I think. One is, if the procedure how you add these controls are going to be the same. Then create it as a method, and call them again, when the page is postback. (the onlything you remember in a custom ViewState variable is controlCounter. Base on this number the method will be defined

Look at my example (I am not sure if the syntax is right).

2. The second workaround, is keeping it your way, but you have to base on the ID to create type of control. Then you need strong naming convention for it.
Let say the first 3 chars will be Type:

label.ID = "LBL" + controlCounter.ToString()
textbox.ID = "TBX" + controlCounter.ToString()
dropdownList.ID = "DDL"+ controlCounter.ToString()

Then in your:
Protected Overloads Overrides Sub LoadViewState(ByVal savedState As Object)
        MyBase.LoadViewState(savedState)
        myControlList = DirectCast(ViewState("myControlList"), List(Of String))
 
        For Each ctlID As String In myControlList
 
            controlCounter += 1
            Dim type As String
           type = ctlID.SubString(0, 3)
           
          switch type
                 case "LBL" ...

//now create Label or textbox accordingly

Hope this helps

JINN
           


Public Property ControlCounter() As Integer
    Get
        Return ViewState("controlCounter") 
    End Get
    Protected Set(ByVal value As Integer)
        ViewState("controlCounter")  = value
    End Set
End Property
 
Protected Sub addControlButton_Click(ByVal sender As Object, ByVal e As EventArgs)
	AddControlToPage(ControlCounter);  
  End Sub
  
  Protected Sub AddControlToPage(Byval currentCounter As Integer)
	Dim controlCounter AS Integer = currentCounter + 1
	Dim label As New Label()
        label.Text = "Label: "
        label.ID = "LabelID" + controlCounter.ToString()
 
        Dim textbox As New TextBox()
        textbox.Text = "text in textbox" + controlCounter.ToString()
        textbox.ID = "textbox" + controlCounter.ToString()
 
        Dim lineBreak As New LiteralControl("<br />")
 
        controlHolder.Controls.Add(label)
        controlHolder.Controls.Add(textbox)
        controlHolder.Controls.Add(lineBreak)
 
        myControlList.Add(textbox.ID)
        myControlList.Add(label.ID)
 
        ViewState("controlCounter") = controlCounter
  End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
miguel_jAuthor Commented:
Jinn

thank you for your patience and help

i have managed to approach you second workout around but now having trouble determining why its incrementing by 2... Ive looked at the code and it might be a little thing but im stumped!

Thank you
Public controlCounter As Integer = 0
 
    Public myControlList As New List(Of String)()
 
    Protected Sub addControlButton_Click(ByVal sender As Object, ByVal e As EventArgs)
 
        controlCounter += 1
 
        Dim lbl As New Label()
        lbl.Text = "label" + controlCounter.ToString()
        lbl.ID = "LBL" + controlCounter.ToString()
 
        Dim txt As New TextBox()
        txt.Text = "text in textbox" + controlCounter.ToString()
        txt.ID = "TXT" + controlCounter.ToString()
 
        Dim lineBreak As New LiteralControl("<br />")
 
        controlHolder.Controls.Add(lbl)
        controlHolder.Controls.Add(txt)
        controlHolder.Controls.Add(lineBreak)
 
        myControlList.Add(lbl.ID)
        myControlList.Add(txt.ID)
 
        ViewState("myControlList") = myControlList
 
    End Sub
 
 
    Protected Overloads Overrides Sub LoadViewState(ByVal savedState As Object)
        MyBase.LoadViewState(savedState)
        myControlList = DirectCast(ViewState("myControlList"), List(Of String))
 
        For Each ctlid As String In myControlList
 
            controlCounter += 1
 
            Dim type As String
 
            type = ctlid.Substring(0, 3)
 
            Select Case type
 
                Case "LBL"
                    Dim lbl As New Label()
                    lbl.Text = "label" + controlCounter.ToString()
                    lbl.ID = ctlid
                    controlHolder.Controls.Add(lbl)
                    Exit Select
 
                Case "TXT"
                    Dim txt As New TextBox()
                    txt.ID = ctlid
                    Dim br As New LiteralControl("<br />")
                    controlHolder.Controls.Add(txt)
                    controlHolder.Controls.Add(br)
                    Exit Select
 
            End Select
        Next
    End Sub

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.