dropdownlist无刷新联动

无刷新连动 DropDownList

       在选择后,自将其所属二类显示出来,使用DropDownListSelectedIndexChanged事件可以很容易实现,但选择要刷新一次为实现DropDownList无刷新二级联动将代附下。
一、数据库设计

字段名

数据

ClassID

动编

类编

ClassName    

varchar(8)

UpClassID

int(4)

级类编

ClassLevel

int(4)

类级别12

二、设计步骤
1
、首先,我建一个DropTest.aspx,放入两个DropDownList控件:DropDownList1DropDownList2,完整代如下:
<%@ Page language="c#" Codebehind="DropTest.aspx.cs" AutoEventWireup="false" Inherits="studyWEB.DropTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
   <HEAD>
  <title>WebForm2</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  <script>
      function load(ClassID)

{          

 //ClassID接收传递的大类编
      var drp2 = document.getElementById("DropDownList2");
      function RemoveAll(oElem)

 {             //清除DropDownList2的所有
      var i = 0;
      for (i = oElem.length; i >= 0; i--){
      oElem.options.remove(i);    

   }    }
      RemoveAll(drp2) 
       var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
       var oDoc = new ActiveXObject("MSXML2.DOMDocument");
       oHttpReq.open("POST", "DropChild.aspx?ClassID="+ClassID, false);//取小数据面,

//将大类编值传递过
       oHttpReq.send("");
       result = oHttpReq.responseText;
       oDoc.loadXML(result);
       items1 = oDoc.selectNodes("//CLASSNAME/Table/ClassName");              //取所有求大所属小
       items2 = oDoc.selectNodes("//CLASSNAME/Table/ClassID");                   //
取所有求大所属小
       var itemsLength=items1.length;
       for(i=0;i<itemsLength;i++)                                                                 //将小名和DropDownList2
   {
   

  var newOption = document.createElement("OPTION");
  
       newOption.text=items1[i].text;
  
       newOption.value=items2[i].text;
 
        drp2.options.add(newOption); 

    }      }
  </script>
 

 </HEAD>
 <body MS_POSITIONING="flowLayout">
  <form id="Form1" method="post" runat="server">
   <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
   <asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList>
   <asp:Label id="Label1" runat="server"></asp:Label>
   <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
  </form>    

 </body>   

 </HTML>
该页面的后台文件(DropDownList1.aspx.cs)中Page_Load内的代如下:
if(!this.IsPostBack)
   {
   

 SqlConnection con = new SqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");
    SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname where ClassLevel=1",con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    this.DropDownList1.DataSource=ds.Tables[0].DefaultView;
    this.DropDownList1.DataTextField = "ClassName";
    this.DropDownList1.DataValueField = "ClassID";
    this.DropDownList1.DataBind();
    
this.DropDownList1.Attributes.Add("onchange","load(this.options[this.selectedIndex].value)");  //ClassID参数传递给脚本函数load(ClassID),如果要传递的是ClassNamevalueinnerText,但如果大类为中文,则调用小类时无法示的问题   

  }
   实现如下功能:首先从数据取所有类级别为1(即大)的名和类编号,定到DropDownList1控件上;然后通DropDownList1Attributes属性javascript函数load(ClassID)load()函数通过调DropChild.aspx面,XML流,得到大所属小ClassNameClassID
2
、新建DropChild.aspx面文件,其中不插入任何控件和文本,只在其后台文件(DropChild.aspx.cs)中的Page_Load中加入以下代
if(this.Request["ClassID"]!=null)
   {
   

  int state = Convert.ToInt32(this.Request["ClassID"]);
    SqlConnection con = new SqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");
    SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname where UpClassID='"+state+"'",con);
    DataSet ds = new DataSet("CLASSNAME");
    da.Fill(ds);
    XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
    writer.Formatting = Formatting.Indented;
    writer.Indentation = 4;
    writer.IndentChar = ' ';
    ds.WriteXml(writer);
    writer.Flush();    
    Response.End();
    writer.Close();
       方法得到用户选择的大号,通过查询以后得到一个DataSet象,使用该对象的WriteXML方法直接将内容写到Response.OutputStream里面然后传递到客端,客端的load方法通result =oHttpReq.responseText;得到一个XML字符串,最后解析此串。
       另外,通Request.FormDropDownList2,当点Button理事件代如下:
private void Button1_Click(object sender, System.EventArgs e)
  {  

   Label1.Text=this.Request.Form["DropDownList2"].ToString(); 

 }

posted @ 2008-12-19 13:05  浪子の无悔  阅读(998)  评论(0编辑  收藏  举报