AJAX 三及联动

Posted on 2008-04-08 18:16  xiao zhou  阅读(179)  评论(0)    收藏  举报
   <script type="text/javascript">
     function changsheng(va)
     {       
      var list=WebApplication1.test.getSheng(va).value; 
      alert(list);  
      var f=document.getElementById("shi");
      var classList=list.split("|");
               f.options.length=1;
               for(var i=0;i<classList.length;i++){
                var tmp=classList[i].split(",");
                f.add(new Option(tmp[1],tmp[0]));
               }
     }  
     
 function chengshi(va)
 {
   var list=WebApplication1.test.getqu(va).value;
   var f=document.getElementById("xian");
      var classList=list.split("|");
               f.options.length=1;
               for(var i=0;i<classList.length;i++){
                var tmp=classList[i].split(",");
                f.add(new Option(tmp[1],tmp[0]));
               }
  
 }
   </script>


<div>
        <select id="sheng" style="width: 150px" runat="server" onchange="changsheng(this.value)">
            <option selected="selected"></option>
        </select>
        <select id="shi" style="width: 127px" onchange="chengshi(this.value)">
            <option selected="selected"></option>
        </select>
        <select id="xian" style="width: 140px">
            <option selected="selected"></option>
        </select>
    </div>

后台代码:

 public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(test));
            DataSet ds = SqlDataAccess.GetProvinceInfo();
            string code, name;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {

                code = ds.Tables[0].Rows[i]["code"].ToString();
                name = ds.Tables[0].Rows[i]["name"].ToString();
                this.sheng.Items.Add(new ListItem(name, code));
            }
          
        }

      [AjaxPro.AjaxMethod]
        public string getqu(string shi)
        {
            DataSet ds = SqlDataAccess.GetAreaInfo(shi);
            string str = "";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (i == ds.Tables[0].Rows.Count - 1)
                {
                    str += ds.Tables[0].Rows[i]["code"].ToString() + "," + ds.Tables[0].Rows[i]["name"].ToString();
                }
                else
                {
                    str += ds.Tables[0].Rows[i]["code"].ToString() + "," + ds.Tables[0].Rows[i]["name"].ToString() + "|";
                }
            }
       
            return str.Trim();
        }
        [AjaxPro.AjaxMethod]
        public string getSheng(string sheng)
        {
            DataSet ds = SqlDataAccess.GetCityInfo(sheng);
            string str = "";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (i == ds.Tables[0].Rows.Count - 1)
                {
                    str += ds.Tables[0].Rows[i]["code"].ToString() + "," + ds.Tables[0].Rows[i]["name"].ToString();
                }
                else
                {
                    str += ds.Tables[0].Rows[i]["code"].ToString() + "," + ds.Tables[0].Rows[i]["name"].ToString() + "|";
                }
            }
            return str.Trim();
        }

 




博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3