asp.net Ajax Primer Series (8): Customizing exception handling

Source: Internet
Author: User
Tags exception handling

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.