Question

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?

Was it helpful?

Solution 3

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.

OTHER TIPS

The "correct" way for a UserControl to call a method in a parent control (or page) is for the usercontrol to raise an event that the parent control listens for (subscribes to).

This is done so the usercontrol is not strongly tied to any control. For example, if you want to reuse the control in another page, you would have to add code to the usercontrol so it would know which "parent" method to call. The methodology that you are implementing can get messy very quickly.

An additional benefit would be that you would not have the problem you are facing now. If an error in thrown, you can handle it in the "normal" way.

Correct way is to bubble up the event from child to parent page. No the other way around.

So that parent page can register that event and utilizes it (if it is useful) or completely ignore it.

Something like this.

// Parent ASPX
<uc1:ChildControl runat="server" ID="cc1" OnSomeEvent="cc1_SomeEvent" />

// Parent
protected void cc1_SomeEvent(object sender, EventArgs e)
{
    // Handler event
}

// Child
public event EventHandler OnSomeEvent;

protected void ErrorOccurInControl()
{
     if (this.OnSomeEvent != null)
     {
          this.OnSomeEvent(this, new EventArgs());
     }
}

protected override void OnLoad(EventArgs e)
{
     ErrorOccurInControl();
}

Return a bool from SaveMethod and do something like the following?

try
{
    if(this.Page.GetType().InvokeMember(stepGuide.SaveMethod,
        System.Reflection.BindingFlags.InvokeMethod, null, this.Page, null))
    {
        AppPageMethods.RedirectToPage(button.CommandArgument);
    }
    else
    {
        // Do something else
    }
}
catch (Exception ex) // Better still, use specific exceptions
{
    // Handle exception
}

Also, if SaveMethod is handling its own exceptions, that's why the calling method is ignoring it. In this case, you will have to raise; in the catch block of SaveMethod (or, better still, not handle exceptions that you actually want to bubble up to the caller).

Edit: Other answers that discuss raising events are certainly better approaches, however.

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