Learning new things: Using jquery + XML to implement a simple Ajax instance

Source: Internet
Author: User

Call page triggered by event

<Asp: dropdownlist id = "ddlagentcity" runat = "server" onchange = "onagentcitychange ();"> </ASP: dropdownlist>

Corresponding function. Call the preceding page to obtain the dataset Using ajax, and locate the corresponding record.

 

  1. Function onagentcitychange ()
  2. {
  3. VaR city = $ ("# <% = ddlagentcity. clientid %>"). Val ();
  4. $. Ajax (
  5. {
  6. URL: '<% = pageutility. getrooturl () %>/order/report/XML/getcompanybycity. aspx? City = '+ city,
  7. Type: 'get ',
  8. Datatype: 'xml ',
  9. Timeout: 10000,
  10. Error: function ()
  11. {
  12. Alert ('error loading XML document ');
  13. },
  14. Success: function (XML)
  15. {
  16. VaR STR = "";
  17. $ (XML). Find ("root"). Find ("item"). Each (function ()
  18. {
  19. STR + = "<option value = '" + $ (this ). ATTR ("ID") + "'>" + $ (this ). ATTR ("name") + "</option>"
  20. });
  21. $ ("# <% = Ddlagentid. clientid %>" example .html (STR );
  22. }
  23. });
  24. }

The last sentence is direct assignment.

 

The following is a page for querying databases. A collection in XML format is returned.

Public class getcitybyarearegion: system. Web. UI. Page

Private void page_load (Object sender, system. eventargs E)
{
String strarea = request. querystring ["area"];
If (strarea = NULL)
Strarea = "";

Xmldocument Doc = new xmldocument ();
Doc. loadxml ("<? XML version =/"1.0/" encoding =/"gb2312/"?> /N <root/> ");

Ilist lst;
If (strarea. Trim () = "")
{
LST = city. getallcitys ();
}
Else
{
LST = city. getbyarearegion (strarea );
}

Stringbuilder buffer = new stringbuilder ();

Buffer. append ("<root> ");
Buffer. append ("<item code =/"/"name =/" All/"/> ");

For (INT I = 0; I <lst. Count; I ++)
{
Cityinfo info = (cityinfo) lst [I];

Buffer. appendformat ("<item code =/" {0}/"name =/" {1}/"/> ",
Info. strname, info. strcodename );
}
Buffer. append ("</root> ");

Doc. documentelement. innerxml = buffer. tostring ();

Response. contenttype = "text/XML ";
Doc. Save (response. outputstream );
Response. End ();
}

Of course, remember to introduce Js.

<Script language = "JavaScript" src = "<% = pageutilitytool. getrooturl () %>/JS/jquery. js"> </SCRIPT>

 

Jquery Chinese community http://wiki.jquery.org.cn/doku.php

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.