当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  asp.net实现ListBox、DropDownList无刷新三级联动(xmlhttp)

 asp.net实现ListBox、DropDownList无刷新三级联动(xmlhttp)

点击次数:31 次 发布日期:2008-11-26 10:32:29 作者:源代码网
源代码网推荐     
源代码网推荐   最近正在做一个分类信息的程序,正做到实现无刷新三级联动的问题,从网上查了不少方法,最后使用选择了xmlhttp方法实现,并对代码进行了多次改进,现提供给大家参考。此为ListBox控件示例,DropDownList只需将控件名改一下就可以了。
源代码网推荐  数据库结构为
源代码网推荐  id 自动编号
源代码网推荐  oneid 数值型 一级分类id
源代码网推荐  twoid 数值型 二级分类id
源代码网推荐  threeid 数值型 三级分类id
源代码网推荐  sort 数值型 排序
源代码网推荐  classname 字符型 分类名称
源代码网推荐  
源代码网推荐  数据库下载
源代码网推荐  /Files/netshuai/class.rar
源代码网推荐  
源代码网推荐  aspx页面javascript代码
源代码网推荐  <script type="text/javascript">
源代码网推荐  <!--
源代码网推荐   function XmlPost(str)
源代码网推荐   {
源代码网推荐   var webFileUrl="";
源代码网推荐   document.all("<% =Lbx_ClassThree.ClientID %>").length=0;
源代码网推荐   if(str==1)
源代码网推荐   {
源代码网推荐   webFileUrl = "?oneid=" + document.all("<% =Lbx_ClassOne.ClientID %>").value;
源代码网推荐   document.all("<% =Lbx_ClassTwo.ClientID %>").length=0;
源代码网推荐   }
源代码网推荐   else
源代码网推荐   {
源代码网推荐   webFileUrl = "?oneid=" + document.all("<% =Lbx_ClassOne.ClientID %>").value+"&twoid="+document.all("<% =Lbx_ClassTwo.ClientID %>").value;
源代码网推荐   }
源代码网推荐  
源代码网推荐   var result = "";
源代码网推荐   var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
源代码网推荐   xmlHttp.open("Post", webFileUrl, false);
源代码网推荐   xmlHttp.send("");
源代码网推荐   result = xmlHttp.responseText;
源代码网推荐  
源代码网推荐  
源代码网推荐   if(result != "")
源代码网推荐   {
源代码网推荐   var piArray = result.split(",");
源代码网推荐   if(str==1)
源代码网推荐   {
源代码网推荐   for(var i=0;i<piArray.length;i++)
源代码网推荐   {
源代码网推荐   var ary1 = piArray[i].toString().split("|");
源代码网推荐   document.all("<% =Lbx_ClassTwo.ClientID %>").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
源代码网推荐  
源代码网推荐   }
源代码网推荐   }
源代码网推荐   else
源代码网推荐   {
源代码网推荐   for(var i=0;i<piArray.length;i++)
源代码网推荐   {
源代码网推荐   var ary1 = piArray[i].toString().split("|");
源代码网推荐   document.all("<% =Lbx_ClassThree.ClientID %>").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
源代码网推荐   }
源代码网推荐  
源代码网推荐  
源代码网推荐   }
源代码网推荐   }
源代码网推荐  
源代码网推荐  
源代码网推荐   }
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  //-->
源代码网推荐  </script>
源代码网推荐  aspx页面控件代码
源代码网推荐  <asp:ListBox ID="Lbx_ClassOne" runat="server" Height="300px" Width="150px"></asp:ListBox>
源代码网推荐  <asp:ListBox ID="Lbx_ClassTwo" runat="server" Height="300px" Width="150px"></asp:ListBox>
源代码网推荐  <asp:ListBox ID="Lbx_ClassThree" runat="server" Height="300px" Width="150px" ></asp:ListBox>
源代码网推荐  
源代码网推荐  cs页面代码
源代码网推荐   protected void Page_Load(object sender, EventArgs e)
源代码网推荐   {
源代码网推荐   string strOneid = "", strTwoid = "";
源代码网推荐   if (Request["oneid"] != null && Request["oneid"].ToString() != "")
源代码网推荐   {
源代码网推荐   strOneid = Request["oneid"].ToString();
源代码网推荐  
源代码网推荐   }
源代码网推荐  
源代码网推荐   if (Request["twoid"] != null && Request["twoid"].ToString() != "")
源代码网推荐   {
源代码网推荐  
源代码网推荐   strTwoid = Request["twoid"].ToString();
源代码网推荐   }
源代码网推荐  
源代码网推荐   if (strOneid != "")
源代码网推荐   {
源代码网推荐   Lbx_Class_Bind(strOneid, strTwoid);
源代码网推荐   }
源代码网推荐  
源代码网推荐   if (!this.IsPostBack)
源代码网推荐   {
源代码网推荐   Lbx_ClassOne_Bind();
源代码网推荐   Lbx_ClassOne.Attributes.Add("onchange", "XmlPost(1)");
源代码网推荐   Lbx_ClassTwo.Attributes.Add("onchange", "XmlPost(2)");
源代码网推荐   }
源代码网推荐   }
源代码网推荐  
源代码网推荐   private void Lbx_ClassOne_Bind()
源代码网推荐   {
源代码网推荐   string strSQL;
源代码网推荐   strSQL = "select * from nts_infoclass where oneid<>0 and twoid=0 and threeid=0 order by sort";
源代码网推荐   string ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="数据库路径"";
源代码网推荐   OleDbConnection cnn = new OleDbConnection(ConnectionString);
源代码网推荐   cnn.Open();
源代码网推荐   OleDbCommand cmd=new OleDbCommand(sql, cnn);
源代码网推荐   OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
源代码网推荐   Lbx_ClassOne.DataSource = dr;
源代码网推荐   Lbx_ClassOne.DataTextField = "classname";
源代码网推荐   Lbx_ClassOne.DataValueField = "oneid";
源代码网推荐   Lbx_ClassOne.DataBind();
源代码网推荐   }
源代码网推荐  
源代码网推荐   private void Lbx_Class_Bind(string oneid, string twoid)
源代码网推荐   {
源代码网推荐   string strSQL = "",idname="";
源代码网推荐   if (oneid != "" && twoid == "")
源代码网推荐   {
源代码网推荐   strSQL = "select * from nts_infoclass where twoid<>0 and threeid=0 and oneid=" + oneid + " order by sort";
源代码网推荐   idname = "twoid";
源代码网推荐   }
源代码网推荐   if (oneid != "" && twoid != "")
源代码网推荐   {
源代码网推荐   strSQL = "select * from nts_infoclass where threeid<>0 and oneid=" + oneid + " and twoid=" + twoid + " order by sort";
源代码网推荐   idname = "threeid";
源代码网推荐  
源代码网推荐   }
源代码网推荐   string mystr = "";
源代码网推荐   string ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="数据库路径"";
源代码网推荐   OleDbConnection cnn = new OleDbConnection(ConnectionString);
源代码网推荐   cnn.Open();
源代码网推荐   OleDbCommand cmd=new OleDbCommand(sql, cnn);
源代码网推荐   OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
源代码网推荐  
源代码网推荐  
源代码网推荐   while (dr.Read())
源代码网推荐   {
源代码网推荐  
源代码网推荐   mystr += "," + dr[idname].ToString() + "|" + dr["classname"].ToString();
源代码网推荐  
源代码网推荐   }
源代码网推荐   if (mystr != "")
源代码网推荐   {
源代码网推荐   mystr = mystr.Substring(1);
源代码网推荐   }
源代码网推荐   dr.Close();
源代码网推荐   this.Response.Write(mystr);
源代码网推荐   this.Response.End();
源代码网推荐  
源代码网推荐   }  做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华