1

I have a user control that I dynamically add to a Panel. I also have a couple of other functionality in the aspx page like search which is inside an update panel that causes the page to post back. From what I have read so far is that dynamic control needs to be bound on each page load. This works fine but the issue is that the user control takes a bit of time (like 3s) and thus all request operations takes longer because the user control is being bound every time.

If I load the user control inside the Page.IsPostBack condition and load it only on page load then user control is visible on post back but all the associated events in the user control is not fires.

So, Is there a way in which I can store the user control data in a session and then bind it if I know that the data is not going to change and hence reducing the 3s delay to load the user control.

protected void Page_Load(object sender, EventArgs e)
         {
            if (!IsPostBack)
                {
                    LoadUserControl();     // Only loaded on page load.Faster, but user control
                                           //functionalities break on post back. Maybe 
                                           // because it is not loaded again.
                }

                    //LoadUserControl();   // User control loads fine. But even a totally unrelated 
                                           // post back causes the already loaded User Control to load
                                           // again. 3s delay :(  
         }

The method used to load the user control is

private void LoadUserControl()
        {
            var control = LoadControl("A Dynamic ascx page");
            control.ID = "contentControl";        
            panel1.Controls.Clear(); //panel1 is the placeholder in aspx page
            panel1.Controls.Add(control);
            Session["LoadedControls"] = control;
        }

So I tried to save the loaded control in session and add in to the placeholder panel as such

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
           {
                 LoadUserControl();     // Only loaded on page load.Faster, but user control
                                        //functionalities break on post back. Maybe 
                                        // because it is not loaded again.
           }

       if ((Control)Session["LoadedControls"] != null)
           {
                panel1.Controls.Clear();
                panel1.Controls.Add((Control)Session["LoadedControls"]);
           }
    }

this does not work as expected either. I do not get any of the data present in the user control.

The user control has 2 RadGrid and 2 RadHTMLChart. I did not want to add it since this is already a huge post. the data for the controls in user controls is also being stored in session for binding again. But I am unable to find a way to add the user control dynamically with the bound data.

thebenman
  • 1,621
  • 14
  • 35
  • You need to cache the content of the Control, not the Control itself. – VDWWD Oct 16 '16 at 18:52
  • @vdwwd Could you provide the asker with an example on how to procede to help him understand ? I like your idea but don't know how to do it either. – Dart Feld Oct 17 '16 at 12:54
  • 1
    @Rolland I wouldn't mind, but there are many possible ways to cache the contents. It depends on the type of data, if it user specific or global etc etc. There is not enough info available to give a good answer. – VDWWD Oct 17 '16 at 13:10
  • @VDWWD I have made a slight edit to the question. The real issue is the I have certain control inside an update panel. So when I click on these ones the page gets posted back and I re bind the controls again, which takes a long time. Thus every small operation results in the entire user control being loaded. I could cache the contents of the user control like you said. But, I was thinking if there is a way that I need to add user control only on page load. Something like a update panel for the user control or such. – thebenman Oct 18 '16 at 07:33
  • Rebinding of the control will always be nessecary. Show us what is inside `A Dynamic ascx page` that takes 3 seconds to load (database, external source etc.). All the code you posted so far is irrelevant. – VDWWD Oct 18 '16 at 08:46
  • @VDWWD If rebinding the user control is always necessary then I don't think there is anything that can be done other that caching which is what I am doing now. But the issue is that `A Dynamic ascx page` actually refers to around 8 user controls that gets dynamically added based on the selected menu item. Saving the entire contents in Session/Cache didn't look nice. And that is why I was wondering if it would be possible to put inside a loading panel and theby calling it only on page load.Thanks for your inputs :) – thebenman Oct 18 '16 at 10:22

0 Answers0