我们的用户将: 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 之前发生一些其他操作。如何在 Page_Load 之前的回发中访问控件 ViewState?
额外积分:如果您想就如何完成整体任务提供建议,我会感兴趣。