• ASP_NET实现界面无刷新的DropdownList两级联动效果


    所谓DropdownList联动,也就是在选一个DropdownList的时候使另外一个DropdownList的内容更新(如选省份时显示所属城市),按常规的方法那就是在第一个DropdownList的SelectedIndexChanged事件中改变第二个DropdownList的数据源及重新绑定,但是如果这样的话在每一次的重新选择将带来一次页面的刷新,除了屏幕闪动以外,如果同页有密码框的话,内容也会清除掉.这时我们就需要无刷新实现,基本原理在选择改变时用JS向另外一个隐藏页发送请求并得到一个XML流,解析后放入相应的DropdownList中.例子如下:

    第一个页面:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="jilian1.aspx.cs" Inherits="jilian1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <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(state) {
    var drp2 = document.getElementById("DropDownList2");
    var drpleng = drp2.options.length;
    for (var i = drpleng - 1; i >=0; i--) {
    drp2.remove(i);
    }
    //新建一请求与XML文档
    var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
    var oDoc = new ActiveXObject("MSXML2.DOMDocument");
    //将请求发送到另一页,其中参数state代表DropDownList1所选值
    oHttpReq.open("POST", "jilian2.aspx?state=" + state, false);
    oHttpReq.send("");
    //取返回结果并放入XML文档中
    result = oHttpReq.responseText;
    oDoc.loadXML(result);
    //设置根结点及循环读取其内容
    items = oDoc.selectNodes("//Name/Table"); //CITY代表数据集名,Table代表数据集中的表名
    for (var item = items.nextNode(); item; item = items.nextNode()) {
    //var city = item.selectSingleNode("//city").nodeTypedValue; //city为所取字段内容
    var city = item.nodeTypedValue;
    var newOption = document.createElement("OPTION");
    newOption.text = city; //下拉框的文本,如北京
    newOption.value = city; //下拉框的值,如110000
    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>
    </form>
    </body>
    </html>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;

    public partial class jilian1 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!this.IsPostBack)
    {
    //string strsql = "select * from NodeType where [type]='Catalog' and DocTypeId='" + docTypeId + "' order by DocTypeId";
    //DataTable dt = docSystemAccess.QueryDataTable(strsql);

    SqlConnection con = new SqlConnection("server=127.0.0.1\SQLDATA;uid=sa;pwd=bkin123;database=DocSystem");
    SqlDataAdapter da = new SqlDataAdapter("select name,id from NodeType where [type]='Catalog' and DocTypeId='240a3d78-d8f1-4421-a170-9f5400e0e9ec' order by DocTypeId", con);
    DataSet ds = new DataSet("name"); //此处CITY表示数据集名,与上面取数据时一致
    da.Fill(ds);
    this.DropDownList1.DataSource = ds;
    this.DropDownList1.DataTextField = "name";
    this.DropDownList1.DataValueField = "id";
    this.DropDownList1.DataBind();
    //添加一个属性,使其选择改变时调用上面的load方法,此处为文本,传值的话将innerText改为value即可.
    this.DropDownList1.Attributes.Add("onchange", "load(this.options[this.selectedIndex].value)");
    }
    }
    }

    第二个页面:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;
    using System.Xml;

    public partial class jilian2 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    // Put user code to initialize the page here
    if (this.Request["state"] != null)
    {
    string state = this.Request["state"].ToString();
    SqlConnection con = new SqlConnection("server=127.0.0.1\SQLDATA;uid=sa;pwd=bkin123;database=DocSystem");
    SqlDataAdapter da = new SqlDataAdapter("select Name from NodeType where [type]='Catalog' and DocTypeId='240a3d78-d8f1-4421-a170-9f5400e0e9ec' and AllowAppendNodeType like '%" + state + "%' order by DocTypeId", con);
    DataSet ds = new DataSet("Name"); //此处CITY表示数据集名,与上面取数据时一致
    da.Fill(ds);

    //string strsql = "select Name from NodeType where [type]='Catalog' and DocTypeId='240a3d78-d8f1-4421-a170-9f5400e0e9ec' and AllowAppendNodeType like '%" + state + "%' order by DocTypeId";
    //DataTable dt = docSystemAccess.QueryDataTable(strsql);
    //DataSet ds = new DataSet("Name");
    //ds.Tables.Add(dt);


    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();
    }
    }
    }

  • 相关阅读:
    npm Node Sass version 7.0.1 is incompatible with ^4.0.0.
    source 1.5 中不支持 diamond 运算符
    cannot be found on object of type 'org.springframework.cache.interceptor.CacheExpressionRootObject' maybe not public
    maven报错Blocked mirror for repositories解决方案
    maven项目集成findbugs详解
    maven 配置远程仓库服务器密码
    matlab练习程序(傅里叶拟合)
    matlab练习程序(B样条曲面插值)
    matlab练习程序(数值雅克比)
    matlab练习程序(Stanley路径跟踪)
  • 原文地址:https://www.cnblogs.com/hpbkin/p/4672818.html
Copyright © 2020-2023  润新知