0

I have the following function that updates the UpdatePanel content by adding/loading an ascx custom usercontrol in the placeholder that is in default.aspx:

 protected void NavigationTab_Click(string ascxpath)
                {           
                        Control ctrl = LoadControl(ascxpath);
                        //cphmaincontent is my asp ContenPlaceHoderId in masterpage
                        PlaceHolder phmaincontent = (PlaceHolder)cphmaincontent.FindControl("phmaincontent");
                        phmaincontent.Controls.Clear();
                        phmaincontent.Controls.Add(ctrl);
                        upmaincontent.Update();            
                }

Masterpage UpdatePanel:

<asp:UpdatePanel ID="upmaincontent" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:Label ID="lbmsg" runat="server" Text=""></asp:Label>
                        <asp:ContentPlaceHolder ID="cphmaincontent" runat="server">                       
                        </asp:ContentPlaceHolder>                         
                    </ContentTemplate>
                </asp:UpdatePanel>            

I am calling NavigationTab_Click from my navigation control that is another custom ascx control, my ctrl Control object that is loading dynamically on each has button and label when I click the button it simply reassigns some text to the label.

and I have this following code on my masterpage just to get the ascx control path:

protected override void OnInit(EventArgs e)
        {           
               //raising an event to set ascx path
                mainmenu.NavigatePath += new usercontrols.mainmenu.NavigationHandler(NavigationTab_Click);

                base.OnInit(e);          
        } 

so far everything works good, after loading my ctrl object by calling NavigationTab_Click function I see my ctrl in the placeholder and has the button and the label but the issue is this if I click this button it should reassign the label to some text but instead the whole ctrl control content disappears, please help.

Nazo Tajrian
  • 131
  • 2
  • 14

1 Answers1

0

When you're adding controls dynamically you must ensure that it gets recreated on every postback. You also have to ensure that you assign the same ID as before, otherwise events will not be triggered correctly and values cannot be reloaded from ViewState. This must be done it Page_Load at the latest(better in Page_Init).

That's the reason why you should avoid dynamical controls whenever possible.

So you can add controls in event-handlers like you've done. But they must be recreated on the next Postback. So you need to store somewhere what(f.e. IDs) or how many controls are already created. That can be done for example in ViewState or Session. Then you can assign appropriate IDs to the controls(for example with the index or ID suffixed).

Here are some additional informations on this subject:

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • if I make my classes serializable then I dont think I need to create ViewState for each control I have I just save the class object then I cast it back and assign them to the controls right? – Nazo Tajrian Mar 13 '12 at 21:19
  • Also, I added this "ViewState["ASCXPATH"] = ascxpath;" in my "NavigationTab_Click()" which they all in masterpage.cs , then I checked in Page_Load() if ViewState["ASCXPATH"] != null then call the same function again "NavigationTab_Click(ViewState["ASCXPATH"].ToString())" but I still cant make it to load because when I debugged it the ViewState["ASCXPATH"] was still null which I think it shouldn't! – Nazo Tajrian Mar 13 '12 at 21:31
  • @NazoTajrian: Don't store the control instance in the ViewState but only that information you need to recreate them correctly. Actually i yet didn't undetand why you're adding them dynamically to the page instead of switching their `Visible` property. You have a static number of NavigationTabs, haven't you? If you can answer this with yes, don't add your UserControl dynamically. – Tim Schmelter Mar 13 '12 at 21:47
  • Tim, sorry i did not mean to store control instance in the ViewState yes just the information as a class object. NavigationTabs are created dynamically from database and they have the path of each ascx control, so i dont have just a few ascx controls to hide and show, if you remember this was related to my UpdatePanel question. http://stackoverflow.com/questions/9674874/how-to-create-updatepanel-triggers-dynamically/9675045#comment12298183_9675045 – Nazo Tajrian Mar 13 '12 at 22:29
  • @NazoTajrian: The easiest would be load necessary informations from dbms on every postback and load the UserControls accordingly. Or you could use any [Data-Bound Web Server Control](http://msdn.microsoft.com/en-us/library/ms228214.aspx) as "PlaceHolder" for your UserControls. – Tim Schmelter Mar 13 '12 at 22:46
  • the problem I am having now is this, I am getting hard time to figure it out how to store the ascx path that was clicked to reload it on Page_load of the masterpage, all those codes above are in masterpage. When I did some test just to try hard coded one of the path and call NavigationTab_Click("mypath.ascx") on Page_Load it was working good but it was working normal on the second click of the button that is in this dynamically loaded control – Nazo Tajrian Mar 14 '12 at 14:57
  • never mind I think I figured out by using Session to keep the path, thanks. – Nazo Tajrian Mar 14 '12 at 15:27