惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
Microsoft Azure Blog
Microsoft Azure Blog
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
博客园_首页
月光博客
月光博客
N
News | PayPal Newsroom
The Cloudflare Blog
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
量子位
G
Google Developers Blog
T
Troy Hunt's Blog
博客园 - Franky
腾讯CDC
S
Security Affairs
J
Java Code Geeks
aimingoo的专栏
aimingoo的专栏
S
Security @ Cisco Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
D
DataBreaches.Net
Recorded Future
Recorded Future
H
Heimdal Security Blog
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
D
Docker
P
Proofpoint News Feed
V
V2EX
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Secure Thoughts
Engineering at Meta
Engineering at Meta
PCI Perspectives
PCI Perspectives
宝玉的分享
宝玉的分享
The Hacker News
The Hacker News
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cloudbric
Cloudbric
Microsoft Security Blog
Microsoft Security Blog
G
GRAHAM CLULEY
MyScale Blog
MyScale Blog
L
LINUX DO - 热门话题
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary

博客园 - 莫相会

ASP.NET数据绑定表达式语法 微软Office Live WorkSpace正式向全球用户开放 Google Talk的小徽章Chatback 使用UpdatePanel时脚本错误sys 未定义的解决办法 - 莫相会 - 博客园 用户定义函数建议 SQL SERVER临时表的使用 Active Report for .net"在VS中正常显示在IIS中无法显示"的问题之解决办法 SQL匹配 Visual Studio 快捷键收藏 在Source Safe 采用tag 创建服务器控件 XtraGrid如何合并列或横向合并单元格? 关于WebService传输对象数组的问题 SQL Server2000中死锁经验总结 《个人信息管理器》C#源代码下载 sql server数据库定时自动备份 VSS(Visual SourceSafe)使用入门 MDI程序中反射生成菜单并与权限控制的结合(利用XML将模块组件装配与卸载) 《设计模式迷你手册》与《C#设计模式》下载
二级联动
莫相会 · 2007-05-18 · via 博客园 - 莫相会

原文:实现无刷新DropdownList联动效果
asp.net给我们带了了事件模型的编程机制,这使得我们将所有的任务都放在服务器上执行哪怕是一个小小变动,其实这到不是什么问题,可是有一点我们无法忍受,如果我们改变某一个输入框中的内容页面要刷新,改变dropdownlist的选择项需要更新另一个dropdownlist需要刷新,真是郁闷。

  下面我将描述一种原始的方法,之所以说它原是是因为这种方法在asp.net之前就已经有了,我想这两者之间的关系我不必详细描述,我们今天要说的是如何不刷新页面更新dropdownlist,该方法旨在抛砖引玉,其实使用该方法可以实现许多不刷新网页就和后台交互的应用,好了废话就不说了,看看我们的例子吧,首先我们需要一个放置两个dropdownlist的页面,假如它叫webform2.aspx,页面的代码如下:


 1<%@ page language="c#" codebehind="webform2.aspx.cs" autoeventwireup="false" inherits="webapptest1.webform2" %>
 2 2  <!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
 3 3  <html>
 4 4  <head>
 5 5  <title>webform2</title>
 6 6  <meta content="microsoft visual studio .net 7.1" name="generator">
 7 7  <meta content="c#" name="code_language">
 8 8  <meta content="javascript" name="vs_defaultclientscript">
 9 9  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">
1010  <script>
1111  function load(state){
1212  var drp2 = document.getelementbyid("dropdownlist2");
1313  for(var i = 0;i<=drp2.options.length -1;i++){
1414  drp2.remove(i);
1515  }

1616  var ohttpreq = new activexobject("msxml2.xmlhttp");
1717  var odoc = new activexobject("msxml2.domdocument");
1818  ohttpreq.open("post""webform6.aspx?state="+state, false);
1919  ohttpreq.send("");
2020  result = ohttpreq.responsetext;
2121  odoc.loadxml(result);
2222  items = odoc.selectnodes("//city/table");
2323  for (var item = items.nextnode(); item; item = items.nextnode()){
2424  var city = item.selectsinglenode("//city").nodetypedvalue;
2525  var newoption = document.createelement("option");
2626  newoption.text = city;
2727  newoption.value = city;
2828  drp2.options.add(newoption);
2929  }

3030  }

3131  </script>
3232  </head>
3333
3434
3535
3636
3737  <body ms_positioning="flowlayout">
3838  <form id="form1" method="post" runat="server">
3939  <asp:dropdownlist id="dropdownlist1" runat="server"></asp:dropdownlist>
4040  <asp:dropdownlist id="dropdownlist2" runat="server"></asp:dropdownlist>
4141  </form>
4242  </body>
4343  </html>
4444

  上面的页面中有两个dropdownlist和一段js脚本,该脚本可以直接写在页面也可以写在后台在regeist到页面上(后者更灵活一些)该页的后台代码如下所示,在page_load里面写如下的代码:


 1if(!this.ispostback){
 
2  sqlconnection con = new sqlconnection("server=localhost;database=pubs;uid=sa;pwd=sa;");
 
3  sqldataadapter da = new sqldataadapter("select state from authors group by state",con);
 
4  dataset ds = new dataset();
 
5  this.dropdownlist1.datatextfield = "state";
 
6  this.dropdownlist1.datavaluefield = "state";
 
7  this.dropdownlist1.databind();
 
8  this.dropdownlist1.attributes.add("onchange","load(this.options[this.selectedindex].innertext)");
 
9  }

10

  在上面的代码中我们做了两件事情:

  1、帮定其中一个dropdownlist(你也可以同时绑定两个)。

  2、指定该控件的客户端脚本。下面我们详细介绍一下上面的js代码,首先得到页面上要联动的dorpdownlist对象,将他的options清空,再创建两个客户端对象ohttpreq和odoc对象,其中一个负责发送请求另一个负责得到响应结果,我们将用户选择的state发送到名为webform6.aspx的页面,该页面将处理这个请求并返回一个响应,该响应的结果是一个xml文件,稍候介绍webform6.aspx里面的代码。我们将返回的结果使用loadxml方法load到odoc对象里面,然后就可以使用selectnodes方法得到所有的city节点,接着循环这些节点在客户端创建option对象,最后将这些option对象add到dropdwonlist2里面去。

  下面我们看看webfowm6.aspx都做了些什么事情,该页面的html页面是一个除了包括<@page>指令意外什么都没有的页面,后台的page_load代码如下:


 1private void page_load(object sender, system.eventargs e){
 
2  // put user code to initialize the page here
 3  if(this.request["state"]!=null){
 
4  string state = this.request["state"].tostring();
 
5  sqlconnection con = new sqlconnection("server=localhost;database=pubs;uid=sa;pwd=sa;");
 
6  sqldataadapter da = new sqldataadapter("select city from authors where state = '"+state+"'",con);
 
7  dataset ds = new dataset("city");
 
8  da.fill(ds);
 
9  xmltextwriter writer = new xmltextwriter(response.outputstream, response.contentencoding);
10  writer.formatting = formatting.indented;
11  writer.indentation = 4;
12  writer.indentchar = ' ';
13  ds.writexml(writer);
14  writer.flush();
15  response.end();
16  writer.close();
17  }


  该方法得到用户选择的state通过查询以后得到一个dataset对象,使用该对象的writexml方法直接将内容写到response.outputstream里面然后传递到客户端,客户端的load方法通过result =ohttpreq.responsetext;句话得到一个xml字符串,最后解析此串。