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

ASP_NET实现界面无刷新的DropdownList两级联动效果的更多相关文章

  1. ajax实现无刷新两级联动DropDownList

    ajax实现的无刷新三级联动 http://zhangyu028.cnblogs.com/articles/310568.html 本文来自小山blog:http://singlepine.cnblo ...

  2. Combobox下拉框两级联动

    下拉框的两级联动是我们开发中经常遇到一种情况.比如一个学生管理系统中,根据年级.科目及姓名查询学生考试成绩,年级和科目都是硬盘中的有限数据(数据库)而学生则可以有用户手动指定,这时在数据库中有年级和科 ...

  3. JS练习:两级联动

    代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...

  4. jquery完成界面无刷新加载登陆注册

    昨天公司说官网的登陆注册每次要跳转到另一个界面,能不能做一个简单的,在界面弹出一个框框登陆,我想了想做了这么一个案例,大家来看看成不成 贴上代码,实现了在同一个弹出窗上加载了登陆注册功能!可自由点击! ...

  5. Jquery实现两级联动

    最后结果如下: 关键代码如下: <select name="customerCondition['credibilityBegin']" id="credibili ...

  6. asp.net DropDownList实现二级联动效果

    1.在aspx页面中,拖入两个DroDownList控件,代码如下: <div>   <asp:DropDownList ID="s1" runat=" ...

  7. JS 省市两级联动(不带地区版本)

    基于网上找的一个版本改造,因为项目需求不需要地区只要省.市,所以做了改版,两个input上直接取出了数据 <html> <head> <script src=" ...

  8. jquery easyui Combobox 实现 两级联动

    具体效果如下图:

  9. 如何根据Jquery实现两级联动

    <script language="javascript" type="text/javascript" > $(function (){      ...

随机推荐

  1. SNF快速开发平台MVC-瀑布式分页组件

    1.   瀑布式分页 目前已经比较流行了,以往的这种点击分页已经不能满足广大网民的需求了.像百度图片等等,网站都有滚动滚轮直接分页的功能,这样体验也确实好了不少,所以我们也决定在我们的框架内进行集成此 ...

  2. T-Pot平台Honeytrap蜜罐TCP/UDP服务攻击探测及实现自动化邮件告警

    T-pot平台的Honeytrap可观察针对TCP或UDP服务的攻击,作为一个守护程序模拟一些知名的服务,并能够分析攻击字符串,执行相应的下载文件指令,当不产生TCP或者UDP协议的时候Honeytr ...

  3. 修改ip导致服务不可用

    修改ip导致服务不可用 1.修改hostsvi /etc/hosts 修改ip地址 2.lsnrctl start 后会发现The listener supports no services,解决方案 ...

  4. Pytest运行测试用例的多种方式和调试

    测试用例上方使用多个fixtures叠加时,是从下往上进行fixtures调用的.如果是 @pytest.mark.usefixtures('action','a','action2')这种形式,是从 ...

  5. JQuery Easyui引入easyui-lang-zh_CN.js后出现乱码的问题解决方法

    最近使用Easyui做项目,发现引入easyui-lang-zh_CN.js单元后页面会出现乱码,无论设置<meta>.还是Response都不能解决问题.用记事本打开easyui-lan ...

  6. 仿迅雷播放器教程 -- duilib界面(13)

    经过了这么多篇文章的讲解,相信大家也对界面库有一定了解了,用一个新的界面库,肯定要对它进行全方位考察.鉴于公司目前所有的产品都是MFC做的,全部转换成duilib肯定不现实,并且公司的很多项目逻辑和界 ...

  7. duilib进阶教程 -- TreeView控件的不足 (7)

    上一个教程中,虽然播放列表的框架和迅雷一样了,但是字体大小.文字居中还没有解决.如果是刚学duilib,搞定这个可不容易,因为在有了入门教程的指导后,很容易就想到去看[属性列表.XML],但是当你试了 ...

  8. Tetrahedron based light probe interpolation(基于四面体的Light Probe插值)

    在当前的游戏引擎中,使用Light Probe来计算全局环境光对于动态物体的影响是一种很主流的方法.在预处理阶段生成完场景的Light Probe之后,传统的方法采用查找最近的8个相邻的Probe然后 ...

  9. ios 消除 字符串 首尾空格

    本文转载至 http://blog.csdn.net/reylen/article/details/8233353 (1)系统去首尾空格方法,使用NSString中的str = [str string ...

  10. [原]openstack-kilo--issue(八)NovaException: Unexpected vif_type=binding_failed

    2016-12-06 11:11:22.593 1505 INFO nova.scheduler.client.report [req-43897fe4-800f-436a-a13b-1a0098c8 ...