?
Solved

How to programatically add label and maintain its viewstate?

Posted on 2009-04-29
6
Medium Priority
?
417 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:tangteng78
  • 3
  • 2
6 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1600 total points
ID: 24262257
0
 

Author Comment

by:tangteng78
ID: 24262327
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
 
LVL 14

Assisted Solution

by:GiftsonDJohn
GiftsonDJohn earned 400 total points
ID: 24262454
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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 24262632
have you tried to host your label on the dynamic controls place holder?
0
 

Author Comment

by:tangteng78
ID: 24266094
Emoreau,
you mean, creating a custom user control with the label in a place holder? And calling the user control programatically?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 24266132
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

840 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