ListBox三级联动

Default.aspx中文件

 

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>asp.net实现DropDownList、ListBox无刷新三级联动的两种方法 </title> <mce:script language="JavaScript"><!-- //以XML求取ListBox2的数据 function XmlPost2(obj) {    var svalue = obj.value;    var webFileUrl = "?povinceid=" +escape(svalue);       var result = "";    var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");    xmlHttp.open("POST", webFileUrl, false);    xmlHttp.send("");    result = xmlHttp.responseText;       if(result != "")    {      document.all("ListBox2").length=0;      var piArray = result.split(",");//两次分割字符串来实现      for(var i=0;i<piArray.length;i++)      {        var ary1 = piArray[i].toString().split("|");        document.all("ListBox2").options.add(new Option(ary1[1].toString(),ary1[0].toString()));      }    }    else    {      alert(result);    } } //以XML求取ListBox3的数据 function XmlPost3(obj) {    var svalue = obj.value;    var webFileUrl = "?cityid=" +escape(svalue);    var result = "";    var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");    xmlHttp.open("POST", webFileUrl, false);    xmlHttp.send("");    result = xmlHttp.responseText;       if(result != "")    {      document.all("ListBox3").length=0;      var piArray = result.split(",");      for(var i=0;i<piArray.length;i++)      {        var ary1 = piArray[i].toString().split("|");        document.all("ListBox3").options.add(new Option(ary1[1].toString(),ary1[0].toString()));      }    }    else    {      alert(result);    } } // --></mce:script> <mce:style><!-- .Class { z-index: 101; left: 40px; position: absolute; top: 25px; Height=432px; Width=168px; } .Class2 {z-index: 102; left: 271px; position: absolute; top: 23px; Height:432px; Width:168px;} .Class3 { z-index: 103; left: 508px; position: absolute; top: 25px; Height:432px; Width:168px;} --></mce:style><style mce_bogus="1"> .Class { z-index: 101; left: 40px; position: absolute; top: 25px; Height=432px; Width=168px; } .Class2 {z-index: 102; left: 271px; position: absolute; top: 23px; Height:432px; Width:168px;} .Class3 { z-index: 103; left: 508px; position: absolute; top: 25px; Height:432px; Width:168px;}</style> </head> <body> <form id="Form1" method="post" runat="server"> <asp:ListBox ID="ListBox1" CssClass="Class" runat="server" Height="428px" Width="135px" ></asp:ListBox> <asp:ListBox ID="ListBox2" CssClass="Class2" runat="server" ></asp:ListBox> <asp:ListBox ID="ListBox3" CssClass="Class3" runat="server" ></asp:ListBox> </form> </body> </html>

 

 

Default.aspx.cs中代码

using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; using System.Text; public partial class _Default : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { //绑定省份 PovinceBind(); //给【省】【市】 ListBox添加事件 this.ListBox1.Attributes.Add("onchange", "XmlPost2(this);"); this.ListBox2.Attributes.Add("onchange", "XmlPost3(this);"); } if (povinceid != "") { CityBind(povinceid); } if (cityid != "") { AreaBind(cityid); } } #region 设置或获取选中的 选中【省】 值 private string povinceid { get { if (ViewState["povinceid"] != null && ViewState["povinceid"].ToString() != "") { return ViewState["povinceid"].ToString(); } else { if (Request["povinceid"] != null && Request["povinceid"].ToString() != "") { return Request["povinceid"]; } else { return ""; } } } set { ViewState["povinceid"] = value; } } #endregion #region 设置或获取选中的 选中【市】 值 private string cityid { get { if (ViewState["cityid"] != null && ViewState["cityid"].ToString() != "") { return ViewState["cityid"].ToString(); } else { if (Request["cityid"] != null && Request["cityid"].ToString() != "") { return Request["cityid"]; } else { return ""; } } } set { ViewState["povinceid"] = value; } } #endregion #region 绑定【省】 private void PovinceBind() { //string sql = "select provinceId,provinceCode,province from province";ParentAreaCode string sql = "select AreaCode,AreaName,ParentAreaCode from TB_Area where ParentAreaCode='0001'"; DataTable dt = OledbHelper(sql); this.ListBox1.DataSource = dt.DefaultView; this.ListBox1.DataValueField = "AreaCode"; this.ListBox1.DataTextField = "AreaName"; this.ListBox1.DataBind(); ListBox1.SelectedIndex = 0; ListBox3.ClearSelection(); } #endregion #region 绑定【市】 private void CityBind(string code) { string str = ""; //StringBuilder mystr = new StringBuilder(); string sql = "select AreaCode,AreaName,ParentAreaCode from TB_Area where ParentAreaCode='" + code + "'"; DataTable dt = OledbHelper(sql); if (dt.Rows.Count != 0) { for (int i = 0; i < dt.Rows.Count; i++) { str += "," + dt.Rows[i]["AreaCode"].ToString() + "|" + dt.Rows[i]["AreaName"].ToString(); } str = str.Substring(1); } this.Response.Write(str); this.Response.End(); } #endregion #region 绑定【区】 private void AreaBind(string code) { string mystr = ""; string sql = "select AreaCode,AreaName,ParentAreaCode from TB_Area where ParentAreaCode='" + code + "'"; DataTable dt = OledbHelper(sql); if (dt.Rows.Count != 0) { for (int i = 0; i < dt.Rows.Count; i++) { mystr += "," + dt.Rows[i]["AreaCode"].ToString() + "|" + dt.Rows[i]["AreaName"].ToString(); } mystr = mystr.Substring(1); } this.Response.Write(mystr); this.Response.End(); } #endregion #region 访问数据库 protected static DataTable OledbHelper(string sql) { StringBuilder sqlString = new StringBuilder(); sqlString.Append(System.Configuration.ConfigurationManager.ConnectionStrings["OleDbConnectionString1"].ConnectionString); sqlString.Append(System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.ConnectionStrings["OleDbConnectionString2"].ConnectionString)); OleDbConnection conn = new OleDbConnection(sqlString.ToString()); OleDbDataAdapter da = new OleDbDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds); DataTable dt = ds.Tables[0]; return dt; } #endregion }

 

 

 

Web.Config中

<connectionStrings>
  <add name="OleDbConnectionString1" connectionString="Provider=Microsoft.Jet.OleDb.4.0;Data Source="/>
  <add name="OleDbConnectionString2" connectionString="~/App_Data/China.mdb"/>
 </connectionStrings>

 

数据库是Access的,放在App_Data项目文件夹下

 

 

第一次开博不知道怎么上传数据库,有需要的跟帖留下邮箱

你可能感兴趣的:(sql,数据库,String,server,Class,dataset)