ajax省市縣聯動

來源:互聯網
上載者:User

 HTML代碼

 

  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="Button1_Click1" Text="查看省市縣" /></div>
  13.     <script>
  14.         
  15.         function ProvinceChange()
  16.         {
  17.             var obj = document.getElementById("ddlProvince").options[document.getElementById("ddlProvince").selectedIndex];
  18.             var city = document.getElementById("ddlCity");
  19.             var area = document.getElementById("ddlArea");
  20.             var strcitys =  _Default.getParent(obj.value).value;
  21.             if(strcitys.indexOf(",")>0)
  22.             {
  23.                 city.length=0;
  24.                 city.options[0] = new Option("請選擇","-1");
  25.                 area.length=0;
  26.                 area.options[0] = new Option("請選擇","-1");
  27.                 var citys = strcitys.split("-");
  28.                 for(var i=0;i<citys.length;i++)
  29.                 {
  30.                     city.options[i+1] = new Option(citys[i].split(",")[0], citys[i].split(",")[1]);
  31.                 }
  32.             }
  33.             else
  34.             {
  35.                 city.length=0;
  36.                 city.options[0] = new Option("請選擇","-1");
  37.             }
  38.         }
  39.         
  40.          function CityChange()
  41.         {
  42.             var obj = document.getElementById("ddlCity").options[document.getElementById("ddlCity").selectedIndex];
  43.             var city = document.getElementById("ddlArea");
  44.             var strcitys =  _Default.getParent(obj.value).value;
  45.             if(strcitys.indexOf(",")>0)
  46.             {
  47.                 city.length=0;
  48.                 city.options[0] = new Option("請選擇","-1");
  49.                 var citys = strcitys.split("-");
  50.                 for(var i=0;i<citys.length;i++)
  51.                 {
  52.                     city.options[i+1] = new Option(citys[i].split(",")[0], citys[i].split(",")[1]);
  53.                 }
  54.             }
  55.             else
  56.             {
  57.                 city.length=0;
  58.                 city.options[0] = new Option("請選擇","-1");
  59.             }
  60.         }
  61.     </script>
  62.     </div>
  63.     </form>

 

 

CS代碼

 

 

  1. public sqlDMO sqldmo = new sqlDMO();
  2.     protected void Page_Load(object sender, EventArgs e)
  3.     {
  4.         
  5.         if (!IsPostBack)
  6.         {
  7.             setProvince();
  8.             ListItem listCity = new ListItem("請選擇", "-1");
  9.             ddlCity.Items.Insert(0, listCity);
  10.             ListItem listArea = new ListItem("請選擇", "-1");
  11.             ddlArea.Items.Insert(0, listArea);
  12.         
  13.         }
  14.         AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
  15.     }
  16.     private void setProvince()
  17.     {
  18.         DataTable dtProvince = sqldmo.Query("select ass_id as id,ass_dis as name from assort where ass_name='province'");
  19.         ddlProvince.DataSource = dtProvince;
  20.         ddlProvince.DataTextField = "name";
  21.         ddlProvince.DataValueField = "id";
  22.         ddlProvince.DataBind();
  23.         ListItem listProvince = new ListItem("請選擇", "-1");
  24.         ddlProvince.Items.Insert(0, listProvince);
  25.     }
  26.     [AjaxPro.AjaxMethod]
  27.     public string getParent(string parentID)
  28.     {
  29.         DataTable dtCity = sqldmo.Query("select ass_id as id,ass_dis as name from assort where ass_fid='" + parentID + "'");
  30.         string strCity = "";
  31.         for (int i = 0; i < dtCity.Rows.Count; i++)
  32.         {
  33.             strCity += dtCity.Rows[i]["name"].ToString().Trim() + "," + dtCity.Rows[i]["id"].ToString().Trim() + "-";
  34.             if (i == dtCity.Rows.Count - 1) strCity = strCity.Substring(0, strCity.Length - 1);
  35.         }
  36.     
  37.         return strCity;
  38.     }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.