使用UpdatePanel中的JQuery按需加载

使用UpdatePanel中的JQuery按需加载,jquery,asp.net,updatepanel,Jquery,Asp.net,Updatepanel,我的问题是关于控件更新面板的 UpdatePanel:基本上,我的页面上没有几个UpdatePanel,它们加载的数据对网络来说非常昂贵,所以有时候我不需要加载所有内容,这就是为什么我想了解如何按需加载UpdatePanel中的内容 使用JQuery:UpdatePanel的加载应该通过JQuery函数调用进行,所以我不知道如何进行,但是使用JQuery我应该能够说“LoadUpdatePanel(“idOfUpdatePanel”)”并且它应该加载它的内容 你知道如何通过使用UpdatePan

我的问题是关于控件更新面板的

UpdatePanel:基本上,我的页面上没有几个UpdatePanel,它们加载的数据对网络来说非常昂贵,所以有时候我不需要加载所有内容,这就是为什么我想了解如何按需加载UpdatePanel中的内容

使用JQuery:UpdatePanel的加载应该通过JQuery函数调用进行,所以我不知道如何进行,但是使用JQuery我应该能够说“LoadUpdatePanel(“idOfUpdatePanel”)”并且它应该加载它的内容


你知道如何通过使用UpdatePanel和JQuery来解决这个问题,或者我应该朝哪个方向去研究吗?

你不能简单地用UpdatePanel来解决这个问题。UpdatePanel会自动连接所有post数据,包括viewstate和代码

您不能只为获取一个UpdatePanel而运行代码,必须运行整个周期

你能做什么 通过检查请求是否来自同一个UpdatePanel,可以避免在某些函数后面的代码上运行

例如,假设您有4个更新面板,并且更新面板2被触发回发。然后在页面加载的rest更新面板上,您可以执行以下操作

protected void Page_Load(object sender, EventArgs e)
{
    if (IsUpdatePanelInRendering(Page, upUpdatePanelId))
    {
        // run the code for update panel 1, me
        // ...
    }
}
其中IsUpdatePanelInRendering:

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;
    }
}
相对:

直接ajax调用 更好的解决方案是删除UpdatePanel,并使用ajax调用手动执行更新,这是一个困难的解决方案


在这种情况下,您可以使用jQuery和部分发送、部分更新页面上的任何内容,数据发送和操作的成本最低,但需要更多的代码和设计。

非常感谢您的解释