簡體   English   中英

ASP.NET頁面生命周期問題。 在回發時控制Page_InitComplete中的ViewState

[英]ASP.NET page life cycle issue. Control ViewState in Page_InitComplete on postback

我們的用戶將:
1.打開一個選項卡執行某些任務
2.打開另一個標簽更改customerId並執行其他任務
3.返回上一個選項卡並嘗試繼續使用舊的customerId
會話狀態將更改為包含新的customerId。 我一直在研究一個控件,它將檢查customerId是否已更改,並提示用戶他們想要繼續使用哪個ID。

代碼背后

public partial class CustomerChanged : System.Web.UI.UserControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.Page.InitComplete += Page_InitComplete;
    }

    void Page_InitComplete(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (Convert.ToInt32(ViewState["CustID"]) != Globals.CurrentCust.CustId)
            {
                CustomValidator err = new CustomValidator();
                err.ValidationGroup = "CustomerChanged";
                err.IsValid = false;
                err.ErrorMessage = "The customer has changed.";
                Page.Validators.Add(err);
                btnOldCustId.Text = "Old CustID \n" + ViewState["CustID"].ToString();
                btnNewCustId.Text = "New CustID \n" + Globals.CurrentCust.CustId.ToString();
                btnOldCustId.OnClientClick = string.Format("return changeCustomer({0},'{1}');", ViewState["CustID"].ToString(), Globals.GeneralSiteUrl);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "CustomerChangedModalDialog", "ShowCustomerChangedModalDialog();", true);
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState.Add("CustID", Globals.CurrentCust.CustId);
        }
    }
    protected void btnNewCustId_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }
 }
}

ASCX

<script type="text/javascript">
    function ShowCustomerChangedModalDialog() {
       var dlg = $("#CustomerChangedModal").dialog({
         title: 'Select Customer ID',
         resizable: false,
         modal: true
       });
    dlg.parent().appendTo(jQuery("form:first"));
    };

    function changeCustomer(customerId, baseUrl) {
        $.ajax({ url: "/General/Accounts/change.account?id=" + customerId + "&admin=false", async: false });
        $("#CustomerChangedModal").dialog('close');
        return false;
    }
</script>  

<div id="CustomerChangedModal" style="width: 440px; height: 250px; overflow: auto; display: none;">
    The session information has changed.
    Which account ID do you wish to use?
    <br />
    <br />
    <asp:Button ID="btnNewCustId" runat="server" OnClick="btnNewCustId_Click" />
    <asp:Button ID="btnOldCustId" runat="server" />
</div>

我目前正在嘗試訪問Page_InitComplete中控件的ViewState數據,但它似乎是空的。 根據http://msdn.microsoft.com/en-us/library/ie/ms178472.aspx ,在InitComplete時應該可以訪問ViewState。 我需要防止在Page_Load之前阻止其他一些操作發生。 如何在post back befoer Page_Load中訪問控件ViewState?

額外信用:如果您想就如何完成我將感興趣的整體任務提出建議。

我認為您沒有正確閱讀文檔:

在頁面初始化期間,頁面上的控件可用,並且每個控件的UniqueID屬性都已設置......如果當前請求是回發,則尚未加載回發數據,並且控件屬性值尚未恢復為值從視圖狀態。

嘗試Page_Load事件。

您應該使用Page.PreLoad事件。 在將ViewState的值加載到控件層次結構中之后引發它。

或者,引用msdn:

在頁面加載自身和所有控件的視圖狀態之后,以及在處理Request實例包含的回發數據之后引發。

暫無
暫無

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

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