1

我有一个动态添加到Panel. 我在 aspx 页面中还有一些其他功能,例如搜索,它位于更新面板内,导致页面回发。从我目前所读到的,动态控制需要在每个页面加载时绑定。这工作正常,但问题是用户控件需要一些时间(如 3 秒),因此所有请求操作都需要更长的时间,因为每次都绑定用户控件。

如果我在Page.IsPostBack条件内加载用户控件并仅在页面加载时加载它,则用户控件在回发时可见,但用户控件中的所有关联事件都不会触发。

那么,有没有一种方法可以将用户控件数据存储在会话中,然后在我知道数据不会更改时绑定它,从而减少加载用户控件的 3 秒延迟。

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 :(  
         }

用于加载用户控件的方法是

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;
        }

所以我尝试将加载的控件保存在会话中并添加到占位符面板中

 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"]);
           }
    }

这也不能按预期工作。我没有得到用户控件中存在的任何数据。

用户控件有 2RadGrid和 2 RadHTMLChart。我不想添加它,因为这已经是一个巨大的帖子。用户控件中控件的数据也存储在会话中以再次绑定。但我无法找到一种使用绑定数据动态添加用户控件的方法。

4

0 回答 0