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 (){ ...
随机推荐
- webpack 使用别名(resolve.alias)解决scss @import相对路径导致的问题
webpack.conf.js 中 resolve.alias 配置 resolve: { extensions: ['.js', '.vue'], alias: { '@': path.resolv ...
- Spark Scheduler内部原理剖析
文章正文 通过文章“Spark 核心概念RDD”我们知道,Spark的核心是根据RDD来实现的,Spark Scheduler则为Spark核心实现的重要一环,其作用就是任务调度.Spark的任务调度 ...
- js正则表达式验证身份证号和密码
//验证身份证号只能输入15位或者18位的身份证号 /^\d{14}[X|\d]{1}$|^\d{18}$/ig //验证只能输入字母和数字组合6到16位 /^[a-z][a-z0-9]{6,16}$ ...
- 【spark 深入学习 03】Spark RDD的蛮荒世界
RDD真的是一个很晦涩的词汇,他就是伯克利大学的博士们在论文中提出的一个概念,很抽象,很难懂:但是这是spark的核心概念,因此有必要spark rdd的知识点,用最简单.浅显易懂的词汇描述.不想用学 ...
- “RESOURCE MONITOR“CPU占用特别高
背景: SQL Server 2008 R2 10.50.1600 没有设置页面文件,内存为64G,数据库分配50G cpu使用占了50%以上,平时只有10-20%,某台服务器“RESOURCE MO ...
- GNU make使用(一)
[时间:2017-01] [状态:Self] [关键词:makefile,gcc,编译,动态库,静态库,可执行文件,shell命令] 引言 前段时间在Linux下编写一个可测试的程序发现,我对make ...
- c++默认参数函数注意事项
再有默认参数的函数中,一般我们都把默认参数放在声明处而不是定义处. 如果声明和定义都有默认参数,编译器将会报错. 调用含有默认实参的函数时,我们可以包含参数,也可以省略. 有默认参数的函数,我们可以不 ...
- linux 常用命令1【转】
1.1. 关机 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours:minutes & 按预定 ...
- 分布式系统CAP理论与CA选择
总结: CAP指的是数据一致性.服务可用性.分区容错性:(这里的一致性指的是强一致性,又叫原子性或线性一致性:可用性指的是所有读写操作都要能终止,没有时延上的要求) 分布式系统中P是必选项:在P必选的 ...
- 错误代码CS0051可访问性不一致_解决方案
一.问题的出现 用C#在写多线程时报错 二.解决方案 1,分析思路 本来对BaseStruct设置为私有访问,但调用时又想公开化,从而造成了编译错误. 2,解决 将红色部分改为公有 3,总结 注意pu ...