簡體   English   中英

在UpdatePanel中使用JQuery按需加載

[英]Load on demand with JQuery inside UpdatePanel

我的問題是關於控件UpdatePanel。

UpdatePanel :基本上,我的頁面上只有幾個UpdatePanels,這些負載對netword數據加載非常昂貴,因此有時我不需要加載所有內容,這就是為什么我想了解如何按需加載UpdatePanel內部內容的原因。

使用JQuery :UpdatePanels的加載應通過JQuery函數調用來完成,所以我不知道如何,但是使用JQuery我應該可以說“ LoadUpdatePanel(“ idOfUpdatePanel”)“,並且應該加載其中的內容。

任何想法如何通過使用UpdatePanel和JQuery來解決此問題,或者應該朝哪個方向進行調查?

您簡單無法使用UpdatePanel做到這一點。 UpdatePanel的工作會自動與所有發布數據(包括viewstate)和后面的代碼關聯。

您不能僅獲得一個UpdatePanel就運行后面的代碼,必須運行整個周期。

你可以做什么

您可以通過檢查請求是否來自同一UpdatePanel來避免在某些功能后面的代碼上運行。

例如,假設您有4個更新面板,並且更新面板2被觸發回發。 然后,在頁面加載的其余“更新”面板上,您可以執行以下操作

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

相對的:
如何限制UpdatePanel上的發布值數量?
如何防止用戶控件代碼在異步回發中運行?

直接ajax調用

更好的解決方案(但困難的一個)是刪除UpdatePanels,並使用ajax調用手動進行更新。

在這種情況下,您可以使用jQuery,以及部分發送,部分更新頁面上的所有內容,同時以最小的數據發送和操作成本-但具有更多的代碼和設計。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM