当前位置:首页 > asp.net实现DropDownList、ListBox无刷新三级联动的方法

asp.net实现DropDownList、ListBox无刷新三级联动的方法

点击次数:1289  更新日期:2010-12-30
\n

asp.net实现DropDownList、ListBox无刷新三级联动的方法

前台代码:
<head runat=”server”>
<title>asp.net实现DropDownList、ListBox无刷新三级联动的两种方法 http://www.dwww.cn </title>
<SCRIPT LANGUAGE=”JavaScript”>
<!–
//以XML求取ListBox2的数据
function XmlPost2(obj)
{
   var svalue = obj.value;
   var webFileUrl = “?povinceid=” + 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=” + 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);
   }
}
//–>
</SCRIPT>


\n

  
</head>
<body>
<form id=”Form1″ method=”post” runat=”server”>
<asp:ListBox id=”ListBox1″ style=”Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 25px” runat=”server”
Height=”432px” Width=”168px”></asp:ListBox>
<asp:ListBox id=”ListBox2″ style=”Z-INDEX: 102; LEFT: 271px; POSITION: absolute; TOP: 23px” runat=”server”
Height=”432px” Width=”168px”></asp:ListBox>
<asp:ListBox id=”ListBox3″ style=”Z-INDEX: 103; LEFT: 508px; POSITION: absolute; TOP: 25px” runat=”server”
Height=”432px” Width=”168px”></asp:ListBox>
</form>


\n

</body>
后台代码:


\n

DB cn = new DB();


\n

Page_Load#region Page_Load
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
this.down1_bind();
this.ListBox1.Attributes.Add(“onchange”, “XmlPost2(this);”);
this.ListBox2.Attributes.Add(“onchange”, “XmlPost3(this);”);
}
if (povinceid != “”)
{
this.down2_bind(povinceid);
}
if (cityid != “”)
{
this.down3_bind(cityid);
}
}


\n

#endregion


\n

property#region property
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;
}
}
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


\n

down2_bind#region down2_bind
private void down2_bind(string id)
{
string mystr = “”;
string sql = “select cityID,city from city where father = ‘” + id + “‘”;
DataTable dt = cn.ReturnTable2(sql);
if (dt.Rows.Count != 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
mystr += “,” + dt.Rows[i][0].ToString() + “|” + dt.Rows[i][1].ToString();
}
mystr = mystr.Substring(1);
}
this.Response.Write(mystr);
this.Response.End();
}
#endregion


\n

down3_bind#region down3_bind
private void down3_bind(string id)
{
string mystr = “”;
string sql = “select areaID,area from area where father = ‘” + id + “‘”;
DataTable dt = cn.ReturnTable2(sql);
if (dt.Rows.Count != 0)
{
for (int i = 0; i <dt.Rows.Count; i++)
{
mystr += “,” + dt.Rows[i][0].ToString() + “|” + dt.Rows[i][1].ToString();
}
mystr = mystr.Substring(1);
}
this.Response.Write(mystr);
this.Response.End();
}


\n

#endregion


\n

down1_bind#region down1_bind
private void down1_bind()
{
string sql = “select provinceID,province from povince”;
DataTable dt = cn.ReturnTable2(sql);
this.ListBox1.DataSource = dt.DefaultView;
this.ListBox1.DataValueField = “provinceID”;
this.ListBox1.DataTextField = “province”;
this.ListBox1.DataBind();
}


\n

#endregion
以上是实现listbox的方法,DropDownList的方法与它一样,只要替换下就可以了.


\n

数据库这里上传不了,我把它放到我的资源里,你们要是想要的话,就到我的资源里去下载下来,或者你们自己建表看效果.地址是:http://download.csdn.net/user/ainir1314520;数据库是access的,你们转换下就可以了

来源:csdn

\n