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 (){ ...
随机推荐
- R8500 MPv2 版本 刷 Kong编译的 ddwrt 后,使用Entware-ng 安装opkg安装第三方软件
先说R8500吧. 由于Netgear网件的问题导致R8500在去年双11前夕出现了全球范围的Boot Loop的问题,现象为新设备开机一段时间后,路由器进入不停重启的状态,电源灯桔黄色.在和网件工程 ...
- Django Mysql SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED
Django 执行makemigrations 的时候报错: django.db.utils.ProgrammingError: (1064, "You have an error in ...
- Atitit 列出wifi热点以及连接
Atitit 列出wifi热点以及连接 配置命令 >netsh wlan /?1 显示已经有的配置netsh wlan show profiles1 C:\Users\Administrato ...
- [ci]jenkins-slave-ssh docker容器化-自动注入key
jenkins server 再启动slave时候,动态的注入sshkey 只要slave有ssh+jdk即可.无需事先预置用户名密码给slave. 配置 inject ssh key 配置项目 执行 ...
- 【Tomcat】Tomcat 系统架构与设计模式,第 1 部分: 工作原理
这个分为两个部分的系列文章将研究 Apache Tomcat 的系统架构以及其运用的很多经典设计模式.本文是第 1 部分,将主要从 Tomcat 如何分发请求.如何处理多用户同时请求,还有它的多级容器 ...
- [Big Data - Suro] Netflix开源数据流管理器Suro
Netflix近日开源了一个叫做Suro的工具,公司可以利用它来做数据源主机到目标主机的实时定向.它不只在Netflix的数据管道上扮演重要角色,大规模下的应用场景同样令人印象深刻. Netflix各 ...
- 【emWin】例程十一:GIF图像显示
介绍: 本例程介绍gif格式图像显示的方法以及在GMT70,iCore3_ADP,7寸液晶模块.4.3寸液晶模块, VGA模块上的移植. 实验指导书及代码包下载: 链接:http://pan.baid ...
- headfirst python 05, 06
处理数据 with open('james.txt') as jaf: data = jaf.readLine() james = data.strip().split(',') #先去掉空格而否有, ...
- python加快数据处理的方法
1.一切数据库操作最好使用内网连接, 2.使用批量操作接口操作数据库,而不是多线程频繁操作单条数据 3.如果python进程的cpu使用率达到100%了,需要开启多进程.java单个进程cpu使用率在 ...
- [React] 11 - Redux: redux
Ref: Redux中文文档 Ref: React 讀書會 - B團 - Level 19 Redux 深入淺出 Ref: React+Redux 分享會 Ruan Yifeng, Redux 架构: ...