簡體   English   中英

在用戶控件中檢查Page.IsPostBack

[英]Checking Page.IsPostBack in user controls

是否建議在用戶控件Page_Load事件中檢查Page.IsPostBack

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {


        }


    }

我得到了奇怪的結果

編輯〜這是東西。 加載主表單后,我使用Request.QueryString來獲取客戶ID,然后將其放在SESSION變量中。

在控件Load事件中,我讀取了SESSION變量以獲取該客戶的數據。 因此,我需要在控件級別檢查PostBack嗎?

編輯〜這是控件的加載事件

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Getting and storing the customer account number 
            if (string.IsNullOrEmpty((string)Session["CustomerNumber"]))
            {
                Session["CustomerNumber"] = cust.GetCustomerNumber(myHelper.GetCustomerIDFromQueryString());
                LoadProductData();
            }
        }

    }

這是myHelper類

static class myHelper
{
    public static Guid GetCustomerIDFromQueryString()
    {
        //Getting the GUID (used as customerid in CRM) from the URL request of the selected account.  
        return Sql.ToGuid(System.Web.HttpContext.Current.Request["ID"]);
    }

}

}

只是無緣無故地檢查它? 絕對不。 如果您只在第一次加載時執行某項操作,而不在隨后的回發中執行某項操作,則應使用此模式。

您確定在到達頁面時,會話中始終已經已經存儲了“ CustomerNumber”嗎? 關於何時獲取數據以及何時不獲取數據,您有什么押韻或理由嗎?

如果在頁面加載中使用“!IsPostBack”,則當用戶單擊其他控件時,它會執行postBack,因此不會獲取數據。 希望對您有所幫助。

暫無
暫無

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

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