Customizing Error Handling for ASP.NET UpdatePanel Controls

Introduction

When an error occurs during partial-page updates in UpdatePanel controls, the default behavior is that a browser message box is displayed with an error message. This tutorial shows you how to customize how the error is presented to the user and what information the message contains.

You can see the code in action in this tutorial by clicking the Run It buttons. To implement the procedures in your own development environment you need:

The code contains a try-catch statement. In the try block, the code performs division on the values entered into the text boxes. If the operation fails, code in the catch block adds the extra string information in ExtraInfo to the exception and then re-throws the exception without handling it.

The code checks whether the ExtraInfo data item is defined for the exception. If it is, the AsyncPostBackErrorMessage property is set to the string value. Otherwise, a default error message is created.

Save your changes and then press CTRL+F5 to view the page in a browser.

Add a number greater than zero to each text box and then click the Calculate button to demonstrate a successful postback.

Change the second text box value to 0 and then click the Calculate button to create an error condition.

The browser displays a message box that contains the message that was set in the server code.

note

The style of the message box depends on what browser you are using, but the message is the same in all browsers.

Using Client Script to Customize Error Handling

The preceding procedure demonstrated how to customize errors during partial-page rendering by setting properties of the ScriptManager control. The following procedure builds on the customization by using the client PageRequestManager class to display the error message in a <div> element instead of in a browser message box.

VB

The markup includes elements that you can use to display partial-page rendering errors. It defines a <div> element named AlertDiv that contains two other <div> elements. One of the nested <div> elements contains an <input> control that will enable users to hide the <div> element.

Review

This tutorial showed ways that you can extend error handling during partial-page rendering. In server code you can customize error handling by setting the AsyncPostBackErrorMessage property and handling the AsyncPostBackError event of the ScriptManager control. In client code you can customize error handing by handling the endRequest event of the PageRequestManager class.