User Control Disappears After PostBack

when i try to load dynamically user control to my page there disappear , i need to keep the control and execute user control actions
//default page
 
using System;
using System.Configuration;
using System.Data;
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;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    private void LoadMain(string controle)
    {
        UserControl uc = (UserControl)LoadControl(controle);
        Session["control"] = uc;
        main.Controls.Clear();
        main.Controls.Add(uc);
    }
    protected void bt_c1_Click(object sender, EventArgs e)
    {
        LoadMain("c1.ascx");
    }
    protected void bt_c2_Click(object sender, EventArgs e)
    {
        LoadMain("c2.ascx");
    }
}

Open in new window

cristianormoraesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sanjeewajCommented:
When you make a post back after dynamically loading the user control, the control needs to be loaded again to the control tree.  You can keep the active control name in view state and load it during page load if a control name value has already been saved to the view state.  


protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            string controlName = string.Empty;
            if (null != ViewState["controlname"])
            {
                controlName = ViewState["controlname"].ToString();
                UserControl uc = (UserControl)LoadControl(controlName);
                this.PlaceHolder1.Controls.Add(uc);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        UserControl uc = (UserControl)LoadControl("WebUserControl.ascx");
        ViewState["controlname"] = "WebUserControl.ascx";
        this.PlaceHolder1.Controls.Clear();
        this.PlaceHolder1.Controls.Add(uc);
 
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        UserControl uc = (UserControl)LoadControl("WebUserControl2.ascx");
        ViewState["controlname"] = "WebUserControl2.ascx";
        this.PlaceHolder1.Controls.Clear();
        this.PlaceHolder1.Controls.Add(uc);
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YurichCommented:
Hello,
I'd suggest to have a some sort of collection (ArrayList or List<Button> or whatever you want) and populate it as you add dynamic controls to your page, saving it to your session or viewstate as you go and then load them all in your page load (checking of course whether it's empty or not) using this collection. The sample code is attached.
	public partial class _Default : System.Web.UI.Page
	{
		List<TextBox> textboxes = new List<TextBox>();
 
		protected void Page_Load( object sender, EventArgs e )
		{
			if( IsPostBack )
			{
				if( Session[ "textboxes" ] != null )
				{
					textboxes = ( List<TextBox> )Session[ "textboxes" ];
					foreach( TextBox tbox in textboxes )
					{
						Panel1.Controls.Add( tbox );
					}
				}
			}			
 
		}
 
		protected void Button1_Click( object sender, EventArgs e )
		{
			TextBox tbox = new TextBox();
			Panel1.Controls.Add( tbox );
			textboxes.Add( tbox );
			Session[ "textboxes" ] = textboxes;
		}
 
 
	}

Open in new window

0
cristianormoraesAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

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.