how to prevent user controls code behind running in async postbacks?
I have开发者_StackOverflow中文版 a aspx page which contains dozens of custom controls issue is i have a asp.net Ajax update panel which update small area of the page which other user controls lives in the page.(user controls are outside the update panel and update panel has UpdateMode="Conditional") Is there a way of programatically prevent running code behind of custom controls, Since this seriosly degrade performace/page load time. I have a check for IsAsyncPostback for script manager to prevent unnecessary calls to DAL. Is there a way to prevent running custom control code behind according to following conditions,
- cannot touch custom control code behind,ascx page file etc
- cannot use caching
- Any change can be done to the aspx page and its code behind where I am referring above.
- Cannot integrate Jquery or javascript frameworks(wish i could) its too late to do that now.
Slightly off-topic, but do you have any freedom to explore methods that do not include the use of an UpdatePanel
? They are known to have poor performance and should be used sparingly.
With that said, the page lifecycle methods for the UserControl
s must fire for them to be rendered again. The UpdatePanel
methodology isn't what you would expect for an "AJAX" solution, because technically when an UpdatePanel
updates your entire page is re-rendered, but only the parts that you asked to change are returned and redrawn in the UI.
What you might be able to do is check if you're in the middle of an AJAX postback via:
ScriptManager.IsInAsyncPostBack
Then you can prevent the code from running in your UserControl's methods if that property evaluates to true
.
You can use Page.LoadControl
to load them conditionally. In practice, this is a big pain in the neck, and adds much complexity to an already complex architecture.
Other than that, you're looking at conditional checks in the page event code. Since you are already using IsInAsyncPostback
to prevent data accesses, why don't you just skip out of all code execution under the same conditions?
here is the code I use to eliminate the non used render on many custom controls that are inside update panel
<asp:UpdatePanel ID="updPnlUserAct" runat="server" RenderMode="Block" UpdateMode="Conditional" >
<ContentTemplate>
on the code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsUpdatePanelInRendering(Page, updPnlUserAct))
return ;
and this is the code that make the check
public static bool IsUpdatePanelInRendering(Page page, UpdatePanel panel)
{
Debug.Assert(HttpContext.Current != null, "Where are you called ? HttpContext.Current is null ");
Debug.Assert(HttpContext.Current.Request != null, "Where are you called HttpContext.Current.Request is null ");
// if not post back, let it render
if (false == page.IsPostBack)
{
return true;
}
else
{
try
{
// or else check if need to be update
ScriptManager sm = ScriptManager.GetCurrent(page);
if (sm != null && sm.IsInAsyncPostBack)
{
Debug.Assert(HttpContext.Current.Request.Form != null, "Why forms are null ?");
string smFormValue = HttpContext.Current.Request.Form[sm.UniqueID];
if (!string.IsNullOrEmpty(smFormValue))
{
string[] uIDs = smFormValue.Split("|".ToCharArray());
if (uIDs.Length == 2)
{
if (!uIDs[0].Equals(panel.UniqueID, StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
}
}
}
}
catch (Exception x)
{
Debug.Fail("Ops, what we lost here ?");
}
return true;
}
}
精彩评论