I've got a web form (set in a master page) and want to check to see if the page has any changes before exiting (either by success or redirect). If any changes have been made, a dialog is displayed and the user can click ok or cancel.
I've created a panel to act as a dialog box (change z-layer, and apply shading and opacity to background) but my problem is in handling the click events from the buttons on the dialog. For a given exit scenario, I want to do something like:
protected void btnWelcomePage_Click(object sender, ImageClickEventArgs e)
{
if (this.HasChanges())
{
lblDialogTitle.Text = "Confirm Exit";
lblDialogMessage.Text = "Are you sure? Click OK to continue and lose any data entered";
ClearEventHandlers(btnDialogOK);
btnDialogOK_Click.Click += new EventHandler(btnDialogOK_Click);
ClearEventHandlers(btDialogCancel);
btnDialogCancel_Click += new EventHandler(btnDialogCancel_Click);
pnlDialog.Visible = true;
}
else
{
Response.Redirect("~/WelcomePage.aspx");
}
}
where the event handler for the buttons will do the appropriate work to hide the dialog and redirect if required.
My problem is that the EventHandler doesn't appear to bind between postbacks - setting a breakpoint in the Click event handler doesn't do anything; the page does an empty postback.
I assumed that the postback trashes the EventHandlers so I enabled ViewState on the buttons but to no avail. Can anyone suggest a model for handling this type of change? I've been searching through google but everything appears to relate to the dynamic creation of controls rather than the dynamic assignment of EventHandlers.
Thanks in advance
Dave