0

I have 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.
DSharper
  • 3,177
  • 9
  • 29
  • 47
  • can you edit the code behind of your user control, because I have a code to help you , but you need to add it there. – Aristos Jul 21 '11 at 17:31
  • @Aristos, these controls are used in hundreds of pages unfortunately it will not do there is long QA cycle if i do change all pages should be tested according to that.we dont have much time to release it to live. I am realy dissapointed this cant be prevented without touching user controls. – DSharper Jul 21 '11 at 17:36

3 Answers3

0

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 UserControls 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.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • this is exactly the first thought i had but that is why i pointed the conditions i cannot add that check user controls, I already have ScriptManager.IsInAsyncPostBack conditional check in page methods ,Thanks anyway for your time – DSharper Jul 21 '11 at 17:40
0

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?

Jamie Treworgy
  • 23,934
  • 8
  • 76
  • 119
0

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;
    }
}
Aristos
  • 66,005
  • 16
  • 114
  • 150