I have a usercontrol that calls a save method from the parent page, before redirecting to a new page.
If an error or exception is throw in the parent save method it seems to be ignored and the child still redirects. Is there a way to listen for exceptions or someway to prevent the redirect is the save method didn't fully execute.
Here's the usercontrol code in question.
this.Page.GetType().InvokeMember(stepGuide.SaveMethod, System.Reflection.BindingFlags.InvokeMethod, null, this.Page, null);
AppPageMethods.RedirectToPage(button.CommandArgument);
I appreciate the responses, but I was hoping to keep this as modular as possible...
Currently the user control is being used on about 15 pages, so I've added the user control to the Master page. These pages function completely on their own and the user control just acts as an added layer (guided tour) which is only used for first time users. The usercontrol is evoked only by an argument in the query string, and all information about the current parent page is loaded from a row in the database.
I believe this gives me more flexibility moving forward as this control will be added to more pages down the road. To add this user control functionality to a new page I only have to add a new row in the db and ensure the parent's save method is public, which is what I really like about this approach.
Is there any way to handle/listen for parent ApplicationException from the usercontrol with little to no changes to each parent page?
Inside your parent page (called function) make sure that the exception is not being swallowed, that way you can catch the error from the caller and handle it appropriately.
try{
this.Page.GetType().InvokeMember(stepGuide.SaveMethod, System.Reflection.BindingFlags.InvokeMethod, null, this.Page, null);
AppPageMethods.RedirectToPage(button.CommandArgument);
}
catch(Exception e)
{
//Do something else than redirect here
}
Also, if you are specific exceptions, than catching and handling those would be ideal as appose to the general Exception.