Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6129
  • Last Modified:

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

0
cristianormoraes
Asked:
cristianormoraes
1 Solution
 
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
 
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now