首页 > 编程 > .NET > 正文

ASP.net dropDownList 无刷新联动!

2024-07-10 12:55:52
字体:
来源:转载
供稿:网友

 <title>webform2</title>

  <script language='javascript'>
  function load(){
    var drp2 = document.getelementbyid("dropdownlist2");
    drp2.innertext="";
    for(var i = 0;i<=drp2.options.length -1;i++){
     drp2.remove(i);
    }
    var drp3 = document.getelementbyid("dropdownlist3");
    for(var i = 0;i<=drp3.options.length -1;i++){
     drp3.remove(i);
    }
    var xmlhttp = new activexobject("msxml2.xmlhttp");
    var odoc = new activexobject("msxml2.domdocument");
    var state=document.getelementbyid("dropdownlist1").value;
    xmlhttp.open("post", "webform1.aspx?id="+state,false);
    xmlhttp.send("");
    var res=odoc.loadxml(xmlhttp.responsetext);
    var naitems = odoc.selectnodes("//market/table/maname");
    var iditems = odoc.selectnodes("//market/table/maid");
    var item;
    var id;
    for (item = naitems.nextnode(),id=iditems.nextnode(); item&&id; item = naitems.nextnode(),id=iditems.nextnode()){
     var nastr = item.nodetypedvalue;
     var idstr = id.nodetypedvalue;
     var newoption = document.createelement("option");
     newoption.text =nastr;
     newoption.value = idstr;
     
     
     drp2.options.add(newoption);
    }
    load2();
  
  }
  function load2(){
    var drp2 = document.getelementbyid("dropdownlist3");
    drp2.innertext="";
    for(var i = 0;i<=drp2.options.length -1;i++){
     drp2.remove(i);
    }
    var xmlhttp = new activexobject("msxml2.xmlhttp");
    var odoc = new activexobject("msxml2.domdocument");
    var state=document.getelementbyid("dropdownlist2").value;
    xmlhttp.open("post", "webform3.aspx?id="+state,false);
    xmlhttp.send("");
    var res=odoc.loadxml(xmlhttp.responsetext);
    var naitems = odoc.selectnodes("//market/table/maname");
    var iditems = odoc.selectnodes("//market/table/maid");
    var item;
    var id;
    for (item = naitems.nextnode(),id=iditems.nextnode(); item&&id; item = naitems.nextnode(),id=iditems.nextnode()){
     var nastr = item.nodetypedvalue;
     var idstr = id.nodetypedvalue;
     var newoption = document.createelement("option");
     newoption.text =nastr;
     newoption.value = idstr;
     
     
     drp2.options.add(newoption);
    }
  
  }
  </script>
 </head>
 <body ms_positioning="gridlayout" >
  <form id="form1" method="post" runat="server">
   <asp:dropdownlist id="dropdownlist1" runat="server"></asp:dropdownlist>
   <asp:dropdownlist id="dropdownlist2" runat="server"></asp:dropdownlist>
   <asp:dropdownlist id="dropdownlist3" runat="server"></asp:dropdownlist>
  </form>
 </body>
</html>
-----------------------
namespace dropdown
{
 /// <summary>
 /// webform2 的摘要说明。
 /// </summary>
 public class webform2 : system.web.ui.page
 {
  protected system.web.ui.webcontrols.dropdownlist dropdownlist1;
  protected system.web.ui.webcontrols.dropdownlist dropdownlist3;
  protected system.web.ui.webcontrols.dropdownlist dropdownlist2;
 
  private void page_load(object sender, system.eventargs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!this.ispostback)
   {
    sqlconnection con = new sqlconnection("server=localhost;database=star;uid=sa;pwd=");
    sqldataadapter da = new sqldataadapter("select maid,maname from tabmarket  where macls=0",con);
    dataset ds = new dataset();
    da.fill(ds,"op");
    this.dropdownlist1.datasource=ds.tables[0];
    this.dropdownlist1.datatextfield = "maname";
    this.dropdownlist1.datavaluefield = "maid";
    this.dropdownlist1.databind();
    this.dropdownlist1.attributes.add("onchange","load()");
    this.dropdownlist2.attributes.add("onchange","load2()");
   }


  }

 }
}
-----------------------
public class webform1 : system.web.ui.page
 {
 
  private void page_load(object sender, system.eventargs e)
  {
   // 在此处放置用户代码以初始化页面
   // put user code to initialize the page here
   //if(this.request["state"]!=null)
   //{ 
     string id =this.request["id"];
    sqlconnection con = new sqlconnection("server=localhost;database=star;uid=sa;pwd=;");
    sqldataadapter da = new sqldataadapter("select maname,maid  from tabmarket where [email protected] and mast=1 ",con);
       da.selectcommand.parameters.add("@id",id);
    dataset ds = new dataset("market");
    da.fill(ds);
   
       ds.writexml(response.outputstream);
       response.flush ();
       response.end ();
      
   //}
  }
       }
-----------------------------
 public class webform3 : system.web.ui.page
 {
  private void page_load(object sender, system.eventargs e)
  {
   // 在此处放置用户代码以初始化页面
   string id =request["id"];
   sqlconnection con = new sqlconnection("server=localhost;database=star;uid=sa;pwd=;");
   sqldataadapter da = new sqldataadapter("select maname,maid  from tabmarket where [email protected] and mast=1",con);
   da.selectcommand.parameters.add("@id",id);
   dataset ds = new dataset("market");
   da.fill(ds);
   
   ds.writexml(response.outputstream);
   response.flush ();
   response.end ();
  }
       }
------------------


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表