views:

164

answers:

2

When a menu item is clicked in my menu an ASCX control is loaded via AJAX in my asp panel. To do this I have a method:

public void LoadControl(ControlDestination controlDestination, string filename)
{
    try
    {
        // Load control from file
        Control control = LoadControl(filename);

        // Check control extends BaseForm
          // Do stuff    
        }
        else
        {
            throw new Exception("Web User Control erft niet van BaseForm.");
        }
    }
    catch (ArgumentNullException e)
    {
        // Implement
    }
    catch(HttpException e)
    {
        LoadControl(ControlDestination.Menu, "Error.ascx");
        throw new Exception("User control niet gevonden: " + e.ToString());
    }
}

When I set a breakpoint at the HttpException I get there. I press F11 and the code in LoadControl is executed. Then the excetpion pops up. All this goes well, but Error.ascx is never loaded. I know the method is working because when I want to load other ASCX objects via this method it works. But When I want to load Error.ascx is goes wrong.

I can see Error.ascx if I comment out throw new Exception("User control niet gevonden" + e.ToString()); I want both lines to be executed.

EDIT:

In my master page I have this javascript code to catch some exceptions:

function pageLoad() {
    var manager = Sys.WebForms.PageRequestManager.getInstance();
    manager.add_endRequest(endRequest);
    manager.add_beginRequest(beginRequest);
}

function endRequest(sender, args){
    var Error = args.get_error();
    if (Error != null) {
        ToggleErrorOn(true);
        document.getElementById("ErrorContent").innerHTML = Error.message;
        args.set_errorHandled(true);
    }
}
A: 

An uncaught exception causes the Update Panel to fail any rendering. Instead, it relays an error code back to the AJAX handler which displays the error message.

As such, if you keep throwing that exception, your Error.ascx will never get rendered.

UPDATE:

Your javascript catches the exception too late. The UpdatePanel pipeline has already considered itself in the "errored" state. Any exception thrown by your codebehind but not caught in your codebehind will cause UpdatePanel to drop all HTML it would have rendered and return a simple error code. Which your javascript function is then handling.

JustLoren
I've added catch(Exception e){LoadControl(ControlDestination.Menu, "Error.ascx"); throw new Exception("User control niet gevonden: " + e.ToString());} But I still get the same error and my error.ascx ain't loaded
Martijn
The Exception that *you* are throwing is uncaught, and it terminates the AJAX operation.
Jeff Sternal
In my startpost I've added some javascript code which handles the exception
Martijn
Javascript cannot "handle" errors thrown by your codebehind. You'll have to catch it in the codebehind or else the UpdatePanel will abort itself.
JustLoren
A: 
Jeff Sternal
But when i am in the debugger and set a breakpoint in the exception, the code is executed. Why is that?
Martijn
And how can I catch this exception? How can I solve this?
Martijn
In my startpost I've added some javascript code which handles the exception
Martijn
What are you trying to accomplish by throwing the Exception after loading your error control?
Jeff Sternal
Thnx for the explanation. When an exception occurs I want 2 things: 1.call a method (that will take care of some UI things) and 2. pass the error to the client. I show the error in a overlaying div. I also want the error to be written in the database.
Martijn
Got it! You can accomplish both of those things inside your HttpException handler after calling LoadControl, instead of throwing an exception.
Jeff Sternal
Okay.. so after I've called LoadControl() then I do stuff in the DB and then the tricky part.. show the error in the div. How am i going to do this?
Martijn
It's probably easiest to create your overlay error div directly in your .aspx with runat='server' so that you can access it in your code-behind as an HtmlGenericControl and specify visible='false' so that it doesn't normally render. Then, in your error handler set its Visible property to true.
Jeff Sternal