Ajax.NET Professional csharp DropDownList两级联无刷新绑定

 

客户端代码
 1 <html xmlns="http://www.w3.org/1999/xhtml" >
 2 <head runat="server">
 3   <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
 4     <title>AjaxPro两级联DataTable</title>
 5       <meta name="Author" content="Geovin Du 塗聚文"/>
 6  <meta name="Keywords" content="Geovin Du 塗聚文"/>
 7   <meta name="Description" content="Geovin Du 塗聚文"/>
 8 
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12        <script language="javascript" type="text/javascript">
13     function GetCity()
14     {
15         var Provinceid = document.getElementById("ProvinceList");
16         geovindu.CityBind(Provinceid.value,CityCallBack);
17     }
18     
19     
20     function CityCallBack(response)
21     {
22         if(response.value !=null)
23         {
24         var city = document.getElementById("CityList");
25         var dt = response.value;
26         city.length = 0;
27         city.options.add(new Option("请选择","-1"));
28             for(var i=0;i<dt.Rows.length;i++)
29             {
30                 var city_text = dt.Rows[i]["city"];
31                 var city_value = dt.Rows[i]["cityID"];
32                 city.options.add(new Option(city_text,city_value));     
33             }   
34         }
35     }
36     </script>
37     <div>
38        <asp:DropDownList ID="ProvinceList" runat="server" Width="120px">  
39         </asp:DropDownList>
40          <asp:DropDownList ID="CityList" runat="server" Width="120px">
41             </asp:DropDownList>
42 
43     </div>
44     </form>
45 </body>
46 </html>

 

 

CS代码
  1        /// <summary>
  2         /// 加载页面
  3         /// 涂聚文 2010-08-27
  4         /// </summary>
  5         /// <param name="sender"></param>
  6         /// <param name="e"></param>
  7         protected void Page_Load(object sender, EventArgs e)
  8         {
  9             AjaxPro.Utility.RegisterTypeForAjax(typeof(WebForm2),this.Page);
 10 
 11             if (!IsPostBack)
 12             {
 13                 ProvinceBind();
 14                 BindToCity();
 15                 ProvinceList.Attributes.Add("onchange""GetCity()");
 16 
 17             }
 18 
 19         }
 20         /// <summary>
 21         /// 绑定省级数据
 22         /// </summary>
 23         public void ProvinceBind()
 24         {
 25 
 26             string SqlConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
 27             SqlConnection con = new SqlConnection(SqlConnectionString);
 28             con.Open();
 29             DataSet dsCourtesy = new DataSet();
 30             string sSQL = "select * from lc_province";
 31             SqlDataAdapter daCourtesy = new SqlDataAdapter(sSQL, con);  
 32             daCourtesy.Fill(dsCourtesy, "lc_province");
 33             ProvinceList.DataSource = dsCourtesy.Tables["lc_province"].DefaultView;
 34             ProvinceList.DataTextField = "province";
 35             ProvinceList.DataValueField = "provinceID";
 36             ProvinceList.DataBind();
 37             daCourtesy.Dispose();
 38             dsCourtesy.Tables.Clear();
 39             con.Close();
 40 
 41         }
 42         /// <summary>
 43         /// 绑定市级数据
 44         /// </summary>
 45         private void BindToCity()
 46         {
 47 
 48             string SqlConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
 49             SqlConnection con = new SqlConnection(SqlConnectionString);
 50             con.Open();
 51             DataSet dsCourtesy = new DataSet();
 52             string sSQL = "select * from lc_city where provinceid=" + this.ProvinceList.SelectedValue;
 53             SqlDataAdapter daCourtesy = new SqlDataAdapter(sSQL, con);
 54             daCourtesy.Fill(dsCourtesy, "lc_city");
 55             CityList.DataSource = dsCourtesy.Tables["lc_city"].DefaultView;
 56             CityList.DataTextField = "city";
 57             CityList.DataValueField = "cityID";
 58             CityList.DataBind();
 59             daCourtesy.Dispose();
 60             dsCourtesy.Tables.Clear();
 61             con.Close();
 62         }
 63         [AjaxPro.AjaxMethod]//ajaxPro申明是方法绑定市级数据
 64         public DataTable CityBind(string provinceid)
 65         {
 66             Hashtable ht = new Hashtable();
 67             string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
 68             SqlConnection conn = new SqlConnection(ConnectionString);
 69             conn.Open();
 70 
 71             string strsql = "select * from lc_city where provinceid=" + provinceid + "";//city", "cityID
 72             SqlCommand cmd = new SqlCommand(strsql, conn);
 73             cmd.CommandText = strsql;
 74             SqlDataAdapter da = new SqlDataAdapter();
 75             da.SelectCommand = cmd;
 76             DataTable dt = new DataTable();
 77             da.Fill(dt);
 78 
 79             return dt;
 80 
 81 
 82         }
 83         //[AjaxPro.AjaxMethod]//申明是DataSet ajaxPro方法
 84         //public DataSet CityBind(string provinceid)
 85         //{
 86         //    Hashtable ht = new Hashtable();
 87         //    string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
 88         //    SqlConnection conn = new SqlConnection(ConnectionString);
 89         //    conn.Open();
 90 
 91         //    string strsql = "select * from lc_city where provinceid=" + provinceid;//city", "cityID
 92         //    SqlCommand cmd = new SqlCommand(strsql, conn);
 93         //    cmd.CommandText = strsql;
 94         //    SqlDataAdapter da = new SqlDataAdapter();
 95         //    da.SelectCommand = cmd;
 96         //    DataSet dt = new DataSet();
 97         //    da.Fill(dt);
 98 
 99         //    return dt;
100         //}
101         #region DataSet客户端代码
102 
103         //<script type="text/javascript">
104             //function GetCitys()  
105             //{  
106             //  var pro =document.getElementById("ddlProvince");  
107             //  //命名空间.类名.
108             //  AjaxProDemo.WebForm1.getCityListDataSet(pro.value, getCityListDataSet_CallBack);  
109                
110             //}  
111             //function getCityListDataSet_CallBack(response)  
112             //{  
113             //      if (response.value != null)  
114             //      {   
115             //          var ds = response.value;
116             //          var ddlc=document.getElementById("ddlCity");
117             //          ddlc.length=0;
118             //          ddlc.options.add(new Option("请选择","-1")); 
119             //              for(var i=0; i <ds.Tables[0].Rows.length; i++)  
120             //              {  
121             //              var text=ds.Tables[0].Rows[i].city;  
122             //              var val=ds.Tables[0].Rows[i].cityID;  
123             //              ddlc.options.add(new Option(text,val));  
124             //              }  
125              
126             //       }  
127             //}
128              //</script>
129         #endregion 

 

Ajax.NET Professional  最新学习和下载:http://www.ajaxpro.info/

posted @ 2010-08-28 11:10  ®Geovin Du Dream Park™  阅读(711)  评论(0编辑  收藏  举报