How to programatically add label and maintain its viewstate?

Hi,
I'm trying to add a new label on every user click. It seems that the code below, on every postback the label viewstate is not maintained, thus i'm not getting the value from previous add, only on a new click from user.

However, this works on a textbox (if i replace the label with textbox).

Why is that? Please advise.

PS: I need the label to maintained it's viewstate on postback.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim txt As New Label
        txt.ID = Session("ctrl_id_increment")
        txt.Text = "this is added during page load " & Session("ctrl_id_increment")
 
        'Add in to page placeholder
        PlaceHolder1.Controls.Add(txt)
 
        'Finally add the controls to the string list.
        ctrls.Add(String.Format("{0}", Session("ctrl_id_increment")))
 
        Session("ctrl_id_increment") += 1
 
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Session("ctrl_id_increment") = 0
 
            'Set the list class
            ctrls = New List(Of String)
 
            Dim txt As New Label
            txt.ID = Session("ctrl_id_increment")
            txt.Text = "this is added during page load " & Session("ctrl_id_increment")
 
            'Add in to page placeholder
            PlaceHolder1.Controls.Add(txt)
 
            'Finally add the controls to the string list.
            ctrls.Add(String.Format("{0}", Session("ctrl_id_increment")))
 
            Session("ctrl_id_increment") += 1
 
        Else
            For Each ctrl As String In ctrls
 
                Dim txt As New Label        'Somehow, the label doesn't maintained the viewstate
                txt.ID = ctrl
 
                PlaceHolder1.Controls.Add(txt)
 
            Next
        End If
    End Sub

Open in new window

tangteng78Asked:
Who is Participating?
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
tangteng78Author Commented:
My problem is on label, the link demostrated on textbox. Like i mentioned, the code i have works fine on textbox BUT not on label.
0
 
GiftsonDJohnCommented:
Hi

The Label control does not inherit from the IPostBackDataHandler interface. So the ViewState is responsible for persisting its value across postbacks. The value gets lost while doing any postback.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Éric MoreauSenior .Net ConsultantCommented:
have you tried to host your label on the dynamic controls place holder?
0
 
tangteng78Author Commented:
Emoreau,
you mean, creating a custom user control with the label in a place holder? And calling the user control programatically?
0
 
Éric MoreauSenior .Net ConsultantCommented:
not at all. the "dynamic controls place holder" is a container control (just like the real place holder) in which you can dynamically add controls. Read the website again.
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.