Sam
Sam

Reputation: 2792

Retaining Label and Literal controls after post back fails

I am dealing with a very unusual situation through which made me go through the effort of putting up for some answers.

I am trying to regenerate some controls based on the following document,

http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

This approach works TextBox controls but does not seem to respond to Label, Literal, UploadFile or other controls that I'm not aware of.

I do understand this not working for UploadFile due to security reasons, but why not for other non TextBox controls?

The above article suggests that if we maintain the ID of a control, we can retain them after post back but in the following implementation I only get the TextBox responding to this solution. The "Label" and "Literal" controls in this situation are lost after PostBack which is undesirable given that I pretty much follow the recipe line by line.

Can someone please have a look at the following implementation and see where I'm going wrong or if I got the whole concept wrong in a way?

This is a counter to keep track of the number of control sets generated,

protected int NumberOfControls
{
    get { return (int)ViewState["NumControls"]; }
    set { ViewState["NumControls"] = value; }
}

This is the Page_Load event that fetches the previous slides from the DB on initial page load and regenerates the same page on Postback. AddSlide is a "Placeholder" control where I post other Controls into.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.NumberOfControls = PopulateCarouselSettingFields(AddSlides);

    }
    else
    {
        this.GenerateControls();
    }
}

This is the definition for "PopulateCarouselSettingsFields".

public static int PopulateCarouselSettingFields(PlaceHolder _AddSlides)
{
    string result = string.Empty;
    int counter = 0;

    string CS = ConfigurationManager.ConnectionStrings["someconn"].ConnectionString;

    using (SqlConnection conn = new SqlConnection(CS))
    {
        SqlCommand SqlCmd = new SqlCommand();
        conn.Open();

        SqlCmd.Connection = conn;
        SqlCmd.CommandType = CommandType.StoredProcedure;
        SqlCmd.CommandText = "storedprocedure";

        SqlDataReader dReader;

        dReader = SqlCmd.ExecuteReader();

        while (dReader.Read())
        {
            Literal lit1 = new Literal();
            lit1.ID = "Lit1_" + counter;
            lit1.Text = "<div class=\"controls controls-row\"><div class=\"span3\">";
            _AddSlides.Controls.Add(lit1);

            Label CarouselTextLabel = new Label();
            CarouselTextLabel.ID = "CarouselTextLabel" + counter;
            CarouselTextLabel.Text = "Carousel Text";
            CarouselTextLabel.Font.Bold = true;
            CarouselTextLabel.CssClass = "control-label";
            _AddSlides.Controls.Add(CarouselTextLabel);

            TextBox CarouselText = new TextBox();
            CarouselText.ID = "CarouselText" + counter;
            CarouselText.TextMode = TextBoxMode.MultiLine;
            CarouselText.Height = 50;
            CarouselText.Text = dReader["CarouselText"].ToString();
            _AddSlides.Controls.Add(CarouselText);

            Literal Lit2 = new Literal();
            Lit5.ID = "Lit2_" + counter;
            Lit5.Text = "</div></div></div><br />";
            _AddSlides.Controls.Add(Lit2);

            counter++;

            }
    }
    return counter;
}

This is supposed to regenerate or re-state all the controls using their IDs upon PostBack called from Page_Load event.

protected void GenerateControls()
{
    int count = this.NumberOfControls;

    for (int i = 0; i < count; i++)
    {
        Literal lit1 = new Literal();
        lit1.ID = "Lit1_" + i.ToString();
        AddSlides.Controls.Add(lit1);

        Label CarouselTextLabel = new Label();
        CarouselTextLabel.ID = "CarouselTextLabel" + i.ToString();
        AddSlides.Controls.Add(CarouselTextLabel);

        TextBox CarouselText = new TextBox();
        CarouselText.ID = "CarouselText" + i.ToString();
        AddSlides.Controls.Add(CarouselText);

        Literal Lit2 = new Literal();
        Lit2.ID = "Lit2_" + i.ToString();
        AddSlides.Controls.Add(Lit2);
    }
}

The following piece of code adds new set of controls to the Placeholder "AddSlides" container.

protected void AddMoreSlidesToCarousel(object sender, EventArgs e)
{
    Literal lit1 = new Literal();
    lit1.ID = "Lit1_" + NumberOfControls.ToString();
    lit1.Text = "<div class=\"controls controls-row\"><div class=\"span3\">";
    AddSlides.Controls.Add(lit1);

    Label CarouselTextLabel = new Label();
    CarouselTextLabel.ID = "CarouselTextLabel" + NumberOfControls.ToString();
    CarouselTextLabel.Text = "Carousel Text";
    CarouselTextLabel.Font.Bold = true;
    CarouselTextLabel.CssClass = "control-label";
    AddSlides.Controls.Add(CarouselTextLabel);

    TextBox CarouselText = new TextBox();
    CarouselText.ID = "CarouselText" + NumberOfControls.ToString();
    CarouselText.TextMode = TextBoxMode.MultiLine;
    CarouselText.Height = 50;
    AddSlides.Controls.Add(CarouselText);

    Literal Lit2 = new Literal();
    Lit2.ID = "Lit2_" + NumberOfControls.ToString();
    Lit2.Text = "</div></div></div><br />";
    AddSlides.Controls.Add(Lit2);

    this.NumberOfControls++;
}

Upvotes: 1

Views: 1358

Answers (1)

Alexander
Alexander

Reputation: 2477

I'm sorry to put it like this, but the article you are relying on is doing it wrong.

Dynamically created controls MUST be created in Page_Init, so that they exist before any ViewState stuff. Also, dynamically controls must be recreated each and every time the Page is initialized, PostBack or not.

ViewState/PostBack do not "retain" controls, only the state of such controls.

Please read this article: ASP.NET Page Life Cycle Overview

Upvotes: 1

Related Questions