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();
}
}
}
ASP_NET实现界面无刷新的DropdownList两级联动效果的更多相关文章
- ajax实现无刷新两级联动DropDownList
ajax实现的无刷新三级联动 http://zhangyu028.cnblogs.com/articles/310568.html 本文来自小山blog:http://singlepine.cnblo ...
- Combobox下拉框两级联动
下拉框的两级联动是我们开发中经常遇到一种情况.比如一个学生管理系统中,根据年级.科目及姓名查询学生考试成绩,年级和科目都是硬盘中的有限数据(数据库)而学生则可以有用户手动指定,这时在数据库中有年级和科 ...
- JS练习:两级联动
代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...
- jquery完成界面无刷新加载登陆注册
昨天公司说官网的登陆注册每次要跳转到另一个界面,能不能做一个简单的,在界面弹出一个框框登陆,我想了想做了这么一个案例,大家来看看成不成 贴上代码,实现了在同一个弹出窗上加载了登陆注册功能!可自由点击! ...
- Jquery实现两级联动
最后结果如下: 关键代码如下: <select name="customerCondition['credibilityBegin']" id="credibili ...
- asp.net DropDownList实现二级联动效果
1.在aspx页面中,拖入两个DroDownList控件,代码如下: <div> <asp:DropDownList ID="s1" runat=" ...
- JS 省市两级联动(不带地区版本)
基于网上找的一个版本改造,因为项目需求不需要地区只要省.市,所以做了改版,两个input上直接取出了数据 <html> <head> <script src=" ...
- jquery easyui Combobox 实现 两级联动
具体效果如下图:
- 如何根据Jquery实现两级联动
<script language="javascript" type="text/javascript" > $(function (){ ...
随机推荐
- ab压力测试工具的简单使用
ab是一种用于测试Apache超文本传输协议(HTTP)服务器的工具.apache自带ab工具,可以测试 apache.IIs.tomcat.nginx等服务器 但是ab没有Jmeter.Loadru ...
- iOS多个storyboard间跳转
Stroyboard 可以被看作一个管理View画面的集合.也就是说一个iOS专案裡面并没有限制只能有一个Storyboard.所以在你的APP专案中,你可以把功能相近的View放到同一个APP之中, ...
- C# 重启exe
休夸此地分天下 c# 关闭和重启.exe程序 Process[] myprocess = Process.GetProcessesByName("a"); )//判断如果存在 { ...
- 【6集iCore3_ADP触摸屏驱动讲解视频】6-6 底层驱动之触摸操作
源视频包下载地址: 链接:http://pan.baidu.com/s/1skQlWAT 密码:ymn7 银杏科技优酷视频发布区: http://i.youku.com/gingko8
- 提一下InfoQ
昨天在微信读书中整理了一个"架构师"清单,把InfoQ中文社区这两年发布的电子书整理到了一起,分享给了团队成员. 如果你去研究InfoQ中文社区,就会发现其中一个人与之因缘际会的相 ...
- 【javascript】console 让 js 调试更简单
浏览器的控制台(console)是最重要的面板,主要作用是显示网页加载过程中产生各类信息. 显示信息 console.log('hello world'); console.debug('debug' ...
- Top useful .Net extension methods
Special extension methods were released in C# 3.0. Developers have continuously been looking for way ...
- python风格的抽象工厂模式
抽象工厂模式: 提供一个接口,用户创建多个相关或依赖对象,而不需要指定具体类. 原则: 依赖抽象,不依赖具体类. 实例: 用不同原材料制作不同口味的披萨,创建不同原材料的工厂,不同实体店做出口味不同的 ...
- EF5+MVC4系列(12) 在主视图中直接用RenderAction调用子Action,并返回视图(Return View)或者分部视图(Return PartialView); 从主Action传值到子Action使用TempData传值;TempData高级用法
结论: ViewData 适用于 在一次请求中 传递数据 . 比如我们从 主Action 到 主视图, 然后在 主视图中 用 RenderAction 请求子Action的时候,就是算作 一次请求 ...
- Linux里的2>&1究竟是什么
我们在Linux下经常会碰到nohup command>/dev/null 2>&1 &这样形式的命令.首先我们把这条命令大概分解下首先就是一个nohup表示当前用户和系统 ...