我們的用戶:
1.open一個選項卡中執行一些任務
2.open另一個選項卡改變CustomerID和做一些其他任務
3.返回到上一個標籤,並試圖繼續舊客戶Id
會話狀態將更改爲包含新的customerId。我一直在研究一個控件,它將檢查customerId是否已更改,並提示用戶要繼續使用哪個Id。ASP.NET頁面生命週期問題。控制的ViewState在Page_InitComplete上回發
代碼隱藏
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之前發生其他一些操作。我怎樣才能訪問控件ViewState在回發befoer Page_Load?
額外的信用:如果您想提出如何完成整個任務的建議,我會感興趣。
可從Page_PreLoad訪問視圖狀態。我變得注意力,應該繼續前進。 – user1161495