1
Answer

PlaceHolder control: Dynamically adding and removing controls on the same page

Nainil

Nainil

14y
18k
1
I am trying to dynamically add/remove user controls from a PlaceHolder after capturing values in the loaded UserControl. For example I have a Default.aspx page which holds a PlaceHolder control and a Button. On first page load, it loads UserControl1 containing a RadioButtonList. On the click of Button, I need to capture the value selected from the RadioButtonList control and then unload UserControl1 and load UserControl2 which has another RadioButtonList control. The similar patter continues with UserControl2, i.e after UserControl2 has been loaded, the user will select a RadioButton. On the server, after the Button click, I need to capture the value of the RadioButtonList in UserControl2, unload it and load UserControl1.
           Only first iteration works fine. When the UserControl2 gets loaded, I cannot retrieve the RadioButtonList value on the server once the page makes post back. UserControl1 and UserControl2 holds RadioButtonList controls. Any help would be greatly appreciated. I have attached a sample project. Here is the code for Default.aspx page:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    UserControl globalUserControl;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            int count = 0;

            UserControl1 oUserControl = (UserControl1)LoadControl("UserControl1.ascx");
            globalUserControl = oUserControl;
            PlaceHolder1.Controls.Clear();
            PlaceHolder1.Controls.Add(oUserControl);

            count++;

            Session["Count"] = count;
        }
        else
        {
            int count = Convert.ToInt32(Session["Count"]);

            if (count % 2 == 0)
            {
                UserControl2 oUserControl = (UserControl2)LoadControl("UserControl2.ascx");
                globalUserControl = oUserControl;
                PlaceHolder1.Controls.Clear();
                PlaceHolder1.Controls.Add(oUserControl);
            }
            else
            {
                UserControl1 oUserControl = (UserControl1)LoadControl("UserControl1.ascx");
                globalUserControl = oUserControl;
                PlaceHolder1.Controls.Clear();
                PlaceHolder1.Controls.Add(oUserControl);             
            }

        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string lableText = "";

        int count = Convert.ToInt32(Session["Count"]);

        if (count % 2 == 0)
        {
            count++;

            foreach (Control oControl in globalUserControl.Controls)
            {
                if (oControl is PlaceHolder)
                {
                    PlaceHolder oPlaceHolder = oControl as PlaceHolder;

                    foreach (Control oControl1 in oPlaceHolder.Controls)
                    {
                        RadioButtonList oRadioButtonList = oControl1 as RadioButtonList;
                        lableText = oRadioButtonList.Text.ToString();
                    }
                }
            }

            Label1.Text = "Count is: " + lableText;
            // Now load 1

            UserControl1 oUserControl = (UserControl1)LoadControl("UserControl1.ascx");
            globalUserControl = oUserControl;
            PlaceHolder1.Controls.Clear();
            PlaceHolder1.Controls.Add(oUserControl);
        }
        else
        {
            count++;

            foreach (Control oControl in globalUserControl.Controls)
            {
                if (oControl is PlaceHolder)
                {
                    PlaceHolder oPlaceHolder = oControl as PlaceHolder;

                    foreach (Control oControl1 in oPlaceHolder.Controls)
                    {
                        RadioButtonList oRadioButtonList = oControl1 as RadioButtonList;
                        lableText = oRadioButtonList.Text.ToString();
                    }
                }
            }

            Label1.Text = "Count is: " + lableText;

            // Now load 2

            UserControl2 oUserControl = (UserControl2)LoadControl("UserControl2.ascx");
            globalUserControl = oUserControl;
            PlaceHolder1.Controls.Clear();
            PlaceHolder1.Controls.Add(oUserControl);
        }

        Session["Count"] = count;
    }
}

Answers (1)