Code Implementation Ajax asynchronous return (Javascript script popped up), asynchronous update error handling

Source: Internet
Author: User

-------------------------------- Code implementation asynchronous return --------------------------------------------
Key page code

<Body>
<Form ID = "form1" runat = "server">
<Asp: scriptmanager id = "scriptmanager1" runat = "server">
</ASP: scriptmanager>

<Asp: updatepanel id = "updatepanel1" runat = "server" updatemode = "Conditional">
<Contenttemplate>
<% = Datetime. Now %>
<Asp: button id = "button2" runat = "server" text = "button"/>
</Contenttemplate>
</ASP: updatepanel>
<Asp: button id = "button1" runat = "server" text = "button" onclick = "button#click"/>
</Form>
</Body>
Background code
Public partial class demo2_api: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

   // Enable asynchronous callback of button1 (and button1 is not in updatepanel)
Scriptmanager. getcurrent (this). registerasyncpostbackcontrol (this. button1 );

 

   // Enable button2 to trigger synchronous return (and button2 is in updatepanel)
Scriptmanager. getcurrent (this). registerpostbackcontrol (this. button2 );

}

Protected void button#click (Object sender, eventargs E)
{
This. updatepanel1.update (); // update the content in updatepanel1.
}
}

 

-------------------- Javascript script --------------------------- pops up during asynchronous callback -----------------------
Key page code

<Body>
<Form ID = "form1" runat = "server">
<Asp: scriptmanager id = "scriptmanager1" runat = "server">
</ASP: scriptmanager>

<Asp: updatepanel id = "updatepanel1" runat = "server">
<Contenttemplate>
<% = Datetime. Now %>
<Asp: button id = "button1" runat = "server" text = "button" onclick = "button#click1"/>
</Contenttemplate>
</ASP: updatepanel>
<Asp: updatepanel id = "updatepanel2" runat = "server">
<Contenttemplate>
<% = Datetime. Now %>
</Contenttemplate>
</ASP: updatepanel>
</Form>
</Body>

Background code:

Public partial class demo5_scriptmanagerreg: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}

Protected void button1_click1 (Object sender, eventargs E)
{

   // The Javascript script pops up when updatepanel1 is updated.
Scriptmanager. registerstartupscript (this. updatepanel1, this. GetType (), "updatepanel1", "alert (1)", true );

   // The Javascript script pops up when updatepanel2 is updated.
Scriptmanager. registerstartupscript (this. updatepanel2, this. GetType (), "updatepanel2", "alert (2)", true );

}
}

 

----------------------- Use the code (create updatepanel) to achieve Ajax effect -----------------------------
Page code:
<Body>
<Form ID = "form1" runat = "server">
<Asp: scriptmanager id = "scriptmanager1" runat = "server">
</ASP: scriptmanager>

</Form>
</Body>

Background code:

Public partial class demo7_programicupdatepanel: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Updatepanel up = new updatepanel ();
Up. ID = "updatepanel1 ";
This. Form. Controls. Add (up );

Literalcontrol lc = new literalcontrol (datetime. Now. tostring ());
Up. contenttemplatecontainer. Controls. Add (LC );

Button BTN = new button ();
BTN. Text = "refresh ";
Up. contenttemplatecontainer. Controls. Add (BTN );
}
}

 

------------------ Ajax asynchronous update error handling -----------------------------------------------------

You can use the onasyncpostbackerror event and asyncpostbackerrormessage attribute of the scriptmanager control to capture and set the error message during callback.

Example:

 

 

Page code:

<Body> <br/> <Form ID = "form1" runat = "server"> <br/> <div> <br/> <asp: scriptmanager id = "scriptmanager1" runat = "server" onasyncpostbackerror = "scriptmanagerpolicasyncpostbackerror"> <br/> </ASP: scriptmanager> </P> <p> </div> <br/> <asp: updatepanel id = "updatepanel1" runat = "server"> <br/> <contenttemplate> <br/> <asp: textbox id = "textbox1" runat = "server" width = "56px"> </ASP: textbox> <br/> <asp: label id = "label1" runat = "server" forecolor = "darkviolet" text = "+" width = "13px"> </ASP: Label> <br/> <asp: textbox id = "textbox2" runat = "server" forecolor = "darkorchid" width = "67px"> </ASP: textbox> <br/> <asp: button id = "button1" runat = "server" onclick = "button#click" text = "computing"/> <br/> <asp: label id = "label2" runat = "server" forecolor = "#0000c0" text = "label"> </ASP: label> <br/> </contenttemplate> <br/> </ASP: updatepanel> <br/> </form> <br/> </body>

Background code:

Public partial class updatepanelerrormessage: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}
Protected void button#click (Object sender, eventargs E)
{
Try
{
Double A = convert. todouble (textbox1.text. Trim ());
Double B = convert. todouble (textbox2.text. Trim ());
Double res = A * B;
Label2.text = res. tostring ("F3 ");//Control the output string format
}
Catch (exception ex)
{
If (textbox1.text. length> 1 & textbox2.text. length> 1)
{
Ex. Data ["CuO"] = "the two numbers cannot be multiplied! ";
}
Throw ex;
}
}
Protected void scriptmanagerpolicasyncpostbackerror (Object sender, asyncpostbackerroreventargs E)
{
If (E. Exception. Data ["CuO"]! = NULL)
{
Scriptmanager1.asyncpostbackerrormessage = E. Exception. Data ["CuO"]. tostring ();
}
Else
{
Scriptmanager1.asyncpostbackerrormessage = E. Exception. message;
}
}
}

 

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.