zoukankan      html  css  js  c++  java
  • 再谈 N 个 ASP.net DropDownList 无刷新联动菜单

    再谈 N 个 ASP.net DropDownList 无刷新联动菜单,牺牲N-1个ASPX来实现自动更新机制,避免刷新的闪动!!

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

      <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();  
       }
      }
    作者Blog:http://blog.csdn.net/yangyifan0/

    antony
    :antony1029@163.com
    :http://antony1029.cnblogs.com
  • 相关阅读:
    Redis简单梳理及集群配置
    PDF.js 详情解说
    基于SMS短信平台给手机发送短信
    linux装OpenOffice后传---中文乱码的解决
    NodeJs小试牛刀--聊天室搭建
    算法之路--最小代价生成树
    Linux系统上安装OpenOffice
    项目部署之工具使用心得
    H5 Handlebars的简单使用
    JavaWeb国际化
  • 原文地址:https://www.cnblogs.com/antony1029/p/309552.html
Copyright © 2011-2022 走看看