miguel_j
asked on
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
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
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
JINN
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
Dynamic Web Controls, Postbacks, and View State - http://aspnet.4guysfromrolla.com/articles/092904-1.aspx