I have a control that should prompt the user to choose either the session’s customerId or the page’s old ViewState customerId. If the value in the viewstate does not match the value in session a jQuery modal dialog will appear. The dialog has two buttons, one should display the ViewState value and the other should display the session value. The button with the ViewState value also has a OnClientClick property. I have tested this code with a simple test page that includes only a single button and my control. The simple test page worked. When I added this control to a an existing page I could get the dialog to show but button.text (assigned in the code behind) was empty and the function for OnClientClick would not fire.
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>
code behind
public partial class CustomerChanged : System.Web.UI.UserControl
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Page.PreLoad += Page_PreLoad;
}
void Page_PreLoad(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState.Add("CustID", Globals.CurrentCust.CustId);
}
if (IsPostBack)
{
Page.Validate();
ValidateCustomerId();
}
}
protected void ValidateCustomerId()
{
if (Convert.ToInt32(ViewState["CustID"]) != Globals.CurrentCust.CustId)
{
//button properties assignments
btnOldCustId.Text += "Old AccountId\n" + ViewState["CustID"].ToString();
btnNewCustId.Text += "New AccountId\n" + Globals.CurrentCust.CustId.ToString();
btnOldCustId.OnClientClick = string.Format("return changeCustomer({0},'{1}');", ViewState["CustID"].ToString(), Globals.GeneralSiteUrl);
CustomValidator err = new CustomValidator();
err.IsValid = false;
err.ErrorMessage = "The customer has changed.";
Page.Validators.Add(err);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (!Page.IsValid)
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CustomerChangedModalDialog", "ShowCustomerChangedModalDialog();", true);
}
}
}
protected void btnNewCustId_Click(object sender, EventArgs e)
{
Response.Redirect(Request.RawUrl);
}
}
Any help would be appreciated.
I stopped trying to use server side code and switched to javascript to assign the button text and to reloading the page with the new customerId. This doesn’t answer my question as to why the server side properties where not behaving as I expected but it gets the job done for now.