Question

Our users will:
1.open one tab do some tasks
2.open another tab change customerId and do some other task
3.return to the previous tab and attempt to continue with the old customerId
The session state will have changed to include the new customerId. I have been working on a control that will check that the customerId has changed and prompt the user which Id they would like to continue with.

Code Behind

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>

I currently am trying to access the control's ViewState data in the Page_InitComplete but it appears to be empty. According to http://msdn.microsoft.com/en-us/library/ie/ms178472.aspx the ViewState should be accessible when at InitComplete. I need to prevent prevent some other actions from happening before Page_Load. How can I access the controls ViewState in a post back befoer Page_Load?

Extra Credit: If you would like to offer suggestion on how to accomplish the overall task I would be interested.

Was it helpful?

Solution

You should use the Page.PreLoad event. It's raised after the values from the ViewState are loaded into the control hierarchy.

Or, to quote msdn:

Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance.

OTHER TIPS

I don't think you read the documentation correctly:

During page initialization, controls on the page are available and each control's UniqueID property is set......If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.

Try the Page_Load event.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top