Remove Dynamically Created Controls from ViewState

Hello

How do i remove each dynamically created control (individually) from the ViewState?

Thank you
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Dim intCounter As Integer
 
        If Not IsPostBack Then
            ViewState("ProductFieldCount") = 1
        ElseIf ViewState("ProductFieldCount") > 1 Then
            For intCounter = 2 To ViewState("ProductFieldCount")
                AddProductField(intCounter)
            Next
        End If
    End Sub
 
    Sub AddProductFieldCount_btnClick(ByVal s As Object, ByVal e As EventArgs)
        ViewState("ProductFieldCount") += 1
        AddProductField(ViewState("ProductFieldCount"))
    End Sub
 
    Sub AddProductField(ByVal strFieldNum As String)
 
        Dim litLabel As New LiteralControl()
        Dim txtTextBox As New TextBox()
        Dim urlTextBox As New TextBox()
 
        plhProductFields.Controls.Add(litLabel)
        litLabel.Text = "<p><b>Product " + strFieldNum + ":</b> "
 
 
        plhProductFields.Controls.Add(txtTextBox)
        txtTextBox.ID = "txtProduct" + strFieldNum
 
    End Sub
 
    Sub DeleteProductField(ByVal strFieldNum As String)
        'Code?
    End Sub

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.

TechTiger007Commented:
You can set the "EnableViewState" property of the control to false before adding it to the Controls list. You wont have to remove it on post back.
0
TechTiger007Commented:
something like this

Dim txtTextBox As New TextBox()
txtTextBox.EnableViewState = false
plhProductFields.Controls.Add(txtTextBox)


0
miguel_jAuthor Commented:
thank you for your help tiger, much appreciated

Im adding controls via a button click

and removing controls via a button click

I just reversed the add control method (see below)

is this the correct way of doing it? or is there another easier way?
Can this coding cause any other complications?

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Dim intCounter As Integer
 
        If Not IsPostBack Then
            ViewState("ProductFieldCount") = 1
        ElseIf ViewState("ProductFieldCount") > 1 Then
            For intCounter = 2 To ViewState("ProductFieldCount")
                AddProductField(intCounter)
            Next
        End If
    End Sub
 
    Sub AddProductFieldCount(ByVal s As Object, ByVal e As EventArgs)
        ViewState("ProductFieldCount") += 1
        AddProductField(ViewState("ProductFieldCount"))
    End Sub
 
    Sub AddProductField(ByVal strFieldNum As String)
        Dim litLabel As New LiteralControl()
        Dim txtTextBox As New TextBox()
        Dim urlTextBox As New TextBox()
 
        ' Add Literal Control
        plhProductFields.Controls.Add(litLabel)
        litLabel.Text = "<p><b>Product " + strFieldNum + ":</b> "
        litLabel.ID = "litLabel" + strFieldNum
 
 
        ' Add TextBox Control
        plhProductFields.Controls.Add(txtTextBox)
        txtTextBox.ID = "txtProduct" + strFieldNum
    End Sub
 
    Sub RemoveProductFieldCount(ByVal s As Object, ByVal e As EventArgs)
        RemoveProductField(ViewState("ProductFieldCount"))
    End Sub
 
    Sub RemoveProductField(ByVal strFieldNum As String)
        Dim deltext As TextBox = plhProductFields.FindControl("txtProduct" + strFieldNum)
 
        Dim dellabel As LiteralControl = plhProductFields.FindControl("litLabel" + strFieldNum)
 
        plhProductFields.Controls.Remove(deltext)
        plhProductFields.Controls.Remove(dellabel)
 
        ViewState("ProductFieldCount") -= 1
    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
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.