Ajax linkage between provinces, cities, and counties

Source: Internet
Author: User

HTML code

 

  1. <Form ID = "form1" runat = "server">
  2. <Div>
  3. <Div id = "DV">
  4. <Table>
  5. <Tr>
  6. <TD> <asp: dropdownlist id = "ddlprovince" runat = "server" onchange = "provincechange ()"> </ASP: dropdownlist> </TD>
  7. <TD> <select id = "ddlcity" runat = "server" onchange = "citychange ()"> </SELECT> </TD>
  8. <TD> <select id = "ddlarea" runat = "server"> </SELECT> </TD>
  9. </Tr>
  10. </Table>
  11. <Br/>
  12. <Asp: button id = "button1" runat = "server" onclick = "button?click1" text = ""/> </div>
  13. <SCRIPT>
  14. Function provincechange ()
  15. {
  16. VaR OBJ = Document. getelementbyid ("ddlprovince"). Options [document. getelementbyid ("ddlprovince"). selectedindex];
  17. VaR city = Document. getelementbyid ("ddlcity ");
  18. VaR area = Document. getelementbyid ("ddlarea ");
  19. VaR strcitys = _ default. getparent (obj. Value). value;
  20. If (strcitys. indexof (",")> 0)
  21. {
  22. City. Length = 0;
  23. City. Options [0] = New Option ("select", "-1 ");
  24. Area. Length = 0;
  25. Area. Options [0] = New Option ("select", "-1 ");
  26. VaR citys = strcitys. Split ("-");
  27. For (VAR I = 0; I <Citys. length; I ++)
  28. {
  29. City. options [I + 1] = New Option (citys [I]. split (",") [0], citys [I]. split (",") [1]);
  30. }
  31. }
  32. Else
  33. {
  34. City. Length = 0;
  35. City. Options [0] = New Option ("select", "-1 ");
  36. }
  37. }
  38. Function citychange ()
  39. {
  40. VaR OBJ = Document. getelementbyid ("ddlcity"). Options [document. getelementbyid ("ddlcity"). selectedindex];
  41. VaR city = Document. getelementbyid ("ddlarea ");
  42. VaR strcitys = _ default. getparent (obj. Value). value;
  43. If (strcitys. indexof (",")> 0)
  44. {
  45. City. Length = 0;
  46. City. Options [0] = New Option ("select", "-1 ");
  47. VaR citys = strcitys. Split ("-");
  48. For (VAR I = 0; I <Citys. length; I ++)
  49. {
  50. City. options [I + 1] = New Option (citys [I]. split (",") [0], citys [I]. split (",") [1]);
  51. }
  52. }
  53. Else
  54. {
  55. City. Length = 0;
  56. City. Options [0] = New Option ("select", "-1 ");
  57. }
  58. }
  59. </SCRIPT>
  60. </Div>
  61. </Form>

 

 

CS code

 

 

  1. Public sqldmo = new sqldmo ();
  2. Protected void page_load (Object sender, eventargs E)
  3. {
  4. If (! Ispostback)
  5. {
  6. Setprovince ();
  7. Listitem listcity = new listitem ("select", "-1 ");
  8. Ddlcity. Items. insert (0, listcity );
  9. Listitem listarea = new listitem ("select", "-1 ");
  10. Ddlarea. Items. insert (0, listarea );
  11. }
  12. Ajaxpro. Utility. registertypeforajax (typeof (_ default ));
  13. }
  14. Private void setprovince ()
  15. {
  16. Datatable dtprovince = sqldmo. Query ("select ass_id as ID, ass_dis as name from assort where ass_name = 'province '");
  17. Ddlprovince. datasource = dtprovince;
  18. Ddlprovince. datatextfield = "name ";
  19. Ddlprovince. datavaluefield = "ID ";
  20. Ddlprovince. databind ();
  21. Listitem listprovince = new listitem ("select", "-1 ");
  22. Ddlprovince. Items. insert (0, listprovince );
  23. }
  24. [Ajaxpro. ajaxmethod]
  25. Public String getparent (string parentid)
  26. {
  27. Datatable dtcity = sqldmo. Query ("select ass_id as ID, ass_dis as name from assort where ass_fid = '" + parentid + "'");
  28. String strcity = "";
  29. For (INT I = 0; I <dtcity. Rows. Count; I ++)
  30. {
  31. Strcity + = dtcity. rows [I] ["name"]. tostring (). trim () + "," + dtcity. rows [I] ["ID"]. tostring (). trim () + "-";
  32. If (I = dtcity. Rows. Count-1) strcity = strcity. substring (0, strcity. Length-1 );
  33. }
  34. Return strcity;
  35. }
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.