问题
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.
来源:https://stackoverflow.com/questions/40058699/how-to-re-use-already-bound-data-in-a-user-control-on-post-back