Penguen
Penguen

Reputation: 17268

How to load web user control dynamically?

This is really important question. this makes me crazy in 4 hours :( i can load UCAddX.ascx but if i click "Search in X" button not load UCSearchX user control. There are 3 button also there are 3 web user control. i want to load these 3 web user controls after clickEvents. But below method not working.How to load web user control dynamically? Click By Click (Like Tab control)

 public partial class MyPage: System.Web.UI.Page
    {
   protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["controlType"] = "AddX";
            if (!IsPostBack)
            {
                AddUserControl();
            }
            else
            {
                AddUserControl();
            }
        }

        protected void btnAddX_Click(object sender, DirectEventArgs e)
        {
            ViewState["controlType"] = "AddX";
            if (!IsPostBack)
                  AddUserControl();
            else
                AddUserControl();
        }

        protected void btnSearchX_Click(object sender, DirectEventArgs e)
        {
            ViewState["controlType"] = "SearchX";
            if (!IsPostBack)
                AddUserControl();
            else
                AddUserControl();

        }
        protected void btnUpdateX_Click(object sender, DirectEventArgs e)
        {

        }

        void AddUserControl()
        {
          //  plhContent1.Controls.Clear();
            if (ViewState["controlType"] != null)
            {
                if (ViewState["controlType"].ToString() == "AddX")
                {
                    UCAddX uc = (UCAddX)Page.LoadControl("~/Pages/EN/MyUserControls/UCAddX.ascx");
                    uc.ID = "ucAddX";
                    uc.Attributes.Add("runat", "Server");
                    uc.EnableViewState = true;
                    uc.Visible = true;
                    plhContent1.Controls.Add(uc);
                }
                else if (ViewState["controlType"].ToString() == "SearchX")
                {
                    UCSearchX uc = (UCSearchX)Page.LoadControl("~/Pages/EN/MyUserControls/UCSearchX.ascx");
                    uc.ID = "ucSearchX";
                    uc.Attributes.Add("runat", "Server");
                    uc.EnableViewState = true;
                    uc.Visible = true;
                    plhContent1.Controls.Add(uc);
                }


            }
        }

    }

Upvotes: 1

Views: 4097

Answers (3)

Chris
Chris

Reputation: 7359

User controls when loaded dynamically need to be loaded on every Page_Load so their view state is maintained. So you need something like:

    public string CurrentControlToLoad
    {
        get
        {
             if(ViewState["controlType"] == null)
                 return "";
             return (string)ViewState["controlType"];
        }
        set
        {
             ViewState["controlType"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if(CurrentControlToLoad != "")
           LoadControl(CurrentControlToLoad);
    }

    protected void btnAddSector_Click(object sender, DirectEventArgs e)
    {
        CurrentControlToLoad = "AddSector";
        LoadControl(CurrentControlToLoad);
    }

Upvotes: 1

hackp0int
hackp0int

Reputation: 4169

try something like this,

//to load the control
     protected void Page_Init(object sender, EventArgs e)
     {
         ViewState["controlType"] = "AddSector";
         //you don't need to check the if condiontion, cause you load every time the controls
         AddUserControl();
      }

when you need to get values from the control after postback you should get it on

protected void Page_PreRender(object sender, EventsArgs args) {
    //your placeholder that contains data
}

Upvotes: 0

Sebastian Siek
Sebastian Siek

Reputation: 2075

Use the code below to load usercontrol dynamically

var control = LoadControl(filePath) as ControlType;

then you can subscribe to events and add to control placeholder.

Hope this helps

Upvotes: 1

Related Questions