首页 > 编程 > .NET > 正文

asp.net中使用AJAX实现连动dropDownList连动

2024-07-10 13:06:03
字体:
来源:转载
供稿:网友

以下代码绝对可以使用,也可以实现多级菜单。。。。

  <script language="javascript">
   function load(state)
   {
    var drp2 = document.getelementbyid("dropdownlist2");
    for(var i=0;i<=drp2.options.length-1;i++)
    {
     drp2.remove(i);
    }
    var ohttpreq = new activexobject("msxml2.xmlhttp");    //发送请求
    var odoc = new activexobject("msxml2.domdocument");    //响应结果
    var state=document.getelementbyid("dropdownlist1").value;
    ohttpreq.open("post","webform2.aspx?state="+state, false);
    ohttpreq.send("");
    result = ohttpreq.responsetext;
    odoc.loadxml(result); 
   
   // items = odoc.selectnodes("//city/table");
       items = odoc.selectnodes("//address/table");
     for (var item = items.nextnode();item;item = items.nextnode())
     { 
      var city = item.selectsinglenode("//address").nodetypedvalue;
      var newoption = document.createelement("option"); 
      newoption.text = city; 
      newoption.value = city; 
      drp2.options.add(newoption); 
      } 
   }
  </script>

 

 

============================

webform1.aspx

private void page_load(object sender, system.eventargs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!ispostback)
   {
    sqlconnection con = new sqlconnection("packet size=4096;user id=sa;data source=server;persist security info=false;initial catalog=pubs");
    sqldataadapter da = new sqldataadapter("select state from authors group by state",con);
    dataset ds = new dataset("state");
    da.fill(ds);
    this.dropdownlist1.datasource = ds.tables[0];
    this.dropdownlist1.datatextfield = "state";
    this.dropdownlist1.datavaluefield = "state"; 
    this.dropdownlist1.databind();  
    this.dropdownlist1.attributes.add("onchange","load()");  
    //dropdownlist1.
   }
 
  }

 

webform2.aspx

 private void page_load(object sender, system.eventargs e)
  {
   // 在此处放置用户代码以初始化页面
   if(this.request["state"]!=null)
   {  
    string state = this.request["state"].tostring();  
    sqlconnection con = new sqlconnection("packet size=4096;user id=sa;data source=server;persist security info=false;initial catalog=pubs");
    sqldataadapter da = new sqldataadapter("select address from authors where state = '"+state+"'",con);  
    dataset ds = new dataset("address");  
    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();  
   }
  }

 


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