Example of ASP. NET Ajax in action-scriptmanager Exception Handling

Source: Internet
Author: User

Use onasyncpostbackerror = "scriptmanagerpolicasyncpostbackerror" to capture exceptions,

Use scriptmanager1.asyncpostbackerrormessage = E. exception. message; Output exception information, add sys. webforms. pagerequestmanager. getinstance (). add_endrequest (endrequesthandler); method. When the resend ends, an exception message is displayed in the div!

Similar methods include:

Add_beginrequest () add_beginrequest (handler) Variant
Add_endrequest () add_endrequest (handler) Variant
Add_initializerequest () add_initializerequest (handler) Variant
Add_pageloaded () add_pageloaded (handler) Variant
Add_pageloading () add_pageloading (handler) Variant

 

<% @ Page Language = "C #" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>

<SCRIPT runat = "server">

Protected void errorprocessclick_handler (Object sender, eventargs E)
{
// This handler demonstrates an error condition. In this example
// The server error gets intercepted on the client and an alert is shown.
Throw new argumentexception ();
}
Protected void successprocessclick_handler (Object sender, eventargs E)
{
// This handler demonstrates no server side exception.
Updatepanelmessage. Text = "the asynchronous PostBack completed successfully .";
}

Protected void scriptmanagerpolicasyncpostbackerror (Object sender, asyncpostbackerroreventargs E)
{
Scriptmanager1.asyncpostbackerrormessage = E. Exception. message;
}

</SCRIPT>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> pagerequestmanager endrequesteventargs example </title>
<Style type = "text/CSS">
Body {
Font-family: tahoma;
}
# Alertdiv {
Left: 40%; top: 40%;
Position: absolute; width: 200px;
Padding: 12px;
Border: #000000 1px solid;
Background-color: white;
Text-align: left;
Visibility: hidden;
Z-index: 99;
}
# Alertbuttons {
Position: absolute;
Right: 5%;
Bottom: 5%;
}
</Style>
</Head>
<Body id = "bodytag">
<Form ID = "form1" runat = "server">
<Div>
<Asp: scriptmanager id = "scriptmanager1" runat = "server"
Onasyncpostbackerror = "scriptmanagerpolicasyncpostbackerror">
</ASP: scriptmanager>

<SCRIPT type = "text/JavaScript" Language = "JavaScript">
VaR divelem = 'alertdiv ';
VaR messageelem = 'alertmesse ';
VaR errormessageadditional = 'Please try again .';
VaR bodytag = 'bodytag ';
SYS. webforms. pagerequestmanager. getinstance (). add_endrequest (endrequesthandler );
Function togglealertdiv (visstring)
{
If (visstring = 'ddn ')
{
$ Get (bodytag). style. backgroundcolor = 'white ';
}
Else
{
$ Get (bodytag). style. backgroundcolor = 'Gray ';

}
VaR adiv = $ get (divelem );
Adiv. style. Visibility = visstring;

}
Function clearerrorstate (){
$ Get (messageelem). innerhtml = '';
Togglealertdiv ('did ');
}
Function endrequesthandler (sender, argS)
{
If (ARGs. get_error ()! = Undefined & args. get_error (). httpstatuscode = '20140901 ')
{
VaR errormessage = args. get_error (). Message
Args. set_errorhandled (true );
Togglealertdiv ('visable ');
$ Get (messageelem). innerhtml = '"' +
Errormessage + '"' + errormessageadditional;
}

}
</SCRIPT>
<Asp: updatepanel runat = "server" updatemode = "Conditional" id = "updatepanel1">
<Contenttemplate>
<Asp: Panel id = "Panel1" runat = "server" groupingtext = "Update panel">
<Asp: Label id = "updatepanelmessage" runat = "server"/>
<Br/>
Last update:
<% = Datetime. Now. tostring () %>
.
<Br/>
<Asp: button runat = "server" id = "button1" text = "Submit successful async PostBack"
Onclick = "successprocessclick_handler" onclientclick = "clearerrorstate ()"/>
<Asp: button runat = "server" id = "button2" text = "Submit async PostBack with error"
Onclick = "errorprocessclick_handler" onclientclick = "clearerrorstate ()"/>
<Br/>
</ASP: Panel>
</Contenttemplate>
</ASP: updatepanel>
<Div id = "alertdiv">
<Div id = "alertmessage">
</Div>
<Br/>
<Div id = "alertbuttons">
<Input id = "okbutton" type = "button" value = "OK"
Runat = "server" onclick = "clearerrorstate ()"/>
</Div>
</Div>
</Form>
</Body>
</Html>

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.