If an error occurs when an UpdatePanel control asynchronously updates, an alert dialog box appears by default to display an error message that is unfriendly to the user, and this article looks at how to customize exception handling in server and client script to translate from official documents.
A Customizing exception handling on the server side
1. Add an ASPX page and switch to Design view.
2. In the Toolbox, under the AJAX Extensions tab, double-click the ScriptManager and UpdatePanel controls to add to the page.
3. Add two textbox, a label, a button, and some text in the UpdatePanel control, and set the button's Text property value to "Calculate".
4. Double-click the Calculate button and add the following code to event handling.
protected void Button1_Click(object sender, EventArgs e)
{
try
{
int a = Int32.Parse(TextBox1.Text);
int b = Int32.Parse(TextBox2.Text);
int res = a / b;
Label1.Text = res.ToString();
}
catch (Exception ex)
{
if (TextBox1.Text.Length > 0 && TextBox2.Text.Length > 0)
{
ex.Data["ExtraInfo"] = " You can't divide " +
TextBox1.Text + " by " + TextBox2.Text + ".";
}
throw ex;
}
}
A Try-catch statement block is included in the event handling code, a division operation is performed in a try, and if the operation fails, the ExtraInfo information is set in the catch and the exception is thrown again.
5. Switch to Design view and select the ScriptManager control.
6. In the Properties window, in the toolbar, select the Event button and double-click Asyncpostbackerror.