I had this idea of registering an event in a base custom page on every button. I can check if it was a save or edit and then do an authorization and validation. Derived pages would still handle the click event to do the specifics for saving on that page. I wasn't sure which events would happen first. (but it probably is in order of wireup) If the parent page runs first, I can get expected behavior by setting an IsAuthorized or IsValidated property and checking that in each derived page's click handler.
Anyhow, is there a way to ensure the base page's event handler runs and completes first, or is there any way to salvage this?
EDIT: Am I overlooking a simpler design that doesn't require adding boiler plate code to every edit/save/update button hander in the application? And what is the right way for one event hander to communicate with another? For example the base page needs to communicate success or failure validation, the derived class needs to communicate success or failure of the save so it can be recorded in the audit.
//Base page-- seems like this will work if events happen in order of wireup.
protected override void OnInit(EventArgs e)
{
foreach (Control possibleButton in Controls)
{
if(possibleButton is Button)
{
Button button = (Button) possibleButton;
button.Command += ButtonCommandPreconditions;
}
}
base.OnInit(e);
foreach (Control possibleButton in Controls)
{
if(possibleButton is Button)
{
Button button = (Button) possibleButton;
button.Command += ButtonCommandPostconditions;
}
}
}
void ButtonCommandPreconditions(object sender, CommandEventArgs e)
{
if(e.CommandName=="Save" || e.CommandName=="Edit")
{
//Stuff that needs to happen before other handler
//Validate, display failures-- maybe set IsValdated property
//Check for POST/GET, throw exception on GET.
//Check for ID, throw exception on anonymous user
//Check for authorization
//Display authorization failures-- maybe set IsAuthorized property
}
}
void ButtonCommandPostconditions(object sender, CommandEventArgs e)
{
if(e.CommandName=="Save" || e.CommandName=="Edit")
{
//Stuff that needs to happen *after* other handler
//Log save
}
}
Edit: Modified code to reflect that event handers are supposed to be handled in order of wireup.