如何防止用户控件代码在异步回发中运行? [英] how to prevent user controls code behind running in async postbacks?

查看:81
本文介绍了如何防止用户控件代码在异步回发中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数十个自定义控件的aspx页面,问题是我有一个asp.net Ajax更新面板,该面板会更新页面的小区域,其他用户控件位于该页面中.更新面板具有UpdateMode ="Conditional")是否可以通过编程方式防止在自定义控件后面运行代码,因为这会严重降低性能/页面加载时间.我为脚本管理器检查了IsAsyncPostback,以防止对DAL进行不必要的调用.有没有一种方法可以防止根据以下条件在后面运行自定义控制代码,

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,

  • 无法触摸背后的自定义控制代码,ascx页面文件等
  • 无法使用缓存
  • 可以对aspx页面及其上面我后面提到的代码进行任何更改.
  • 无法集成Jquery或javascript框架(希望我可以)现在为时已晚.

推荐答案

这是我用来消除更新面板内部许多自定义控件上未使用的渲染的代码

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>   

后面的代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsUpdatePanelInRendering(Page, updPnlUserAct))
        return ;

这是进行检查的代码

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

这篇关于如何防止用户控件代码在异步回发中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆