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?
 
miguel_jConnect With a Mentor Author 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
 
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
 
TechTiger007Connect With a Mentor Commented:
something like this

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


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.