Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Remove Dynamically Created Controls from ViewState

Posted on 2008-11-08
3
Medium Priority
?
826 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:miguel_j
  • 2
3 Comments
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22911896
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
 
LVL 13

Assisted Solution

by:TechTiger007
TechTiger007 earned 800 total points
ID: 22911905
something like this

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


0
 

Accepted Solution

by:
miguel_j earned 0 total points
ID: 22914699
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question