用asp.net怎样实现两个DropDownList连动,不刷新页面 ,最好有例子(C#)
谢谢
谢谢
解决方案 »
- CAD(mentor公司的cad文件格式val格式)
- 【老大帮忙】HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl);
- JavaScript脚本问题
- tabControl的标签问题
- Socket Receive中创建的Buffer最大能设为多大
- 关于连数据库取得数据 呈饼图显示一个小问题 50分
- 类似01背包问题..
- 水晶报表使用了插入---汇总功能,如果没有数据就会什么也不显示,怎么处理
- 如何解决SQL中的排序问题
- 请教有关ADO。NET的问题.解决了马上给分,结贴
- 到底市场上,哪种语言是使用最广泛的????
- C#中如何实现Panel随着控件的增多而自动滚动?
ASP.NET给我们带了了事件模型的编程机制,这使得我们将所有的任务都放在服务器上执行哪怕是一个小小变动,其实这到不是什么问题,可是有一点我们无法忍受,如果我们改变某一个输入框中的内容页面要刷新,改变DropDownlist的选择项需要更新另一个Dropdownlist需要刷新,真是郁闷。 下面我将描述一种原始的方法,之所以说它原是是因为这种方法在ASP.NET之前就已经有了,我想这两者之间的关系我不必详细描述,我们今天要说的是如何不刷新页面更新DropDownList,该方法旨在抛砖引玉,其实使用该方法可以实现许多不刷新网页就和后台交互的应用,好了废话就不说了,看看我们的例子吧,首先我们需要一个放置两个DropDownList的页面,假如它叫WebForm2.aspx,页面的代码如下: <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WebApptest1.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<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");
for(var i = 0;i<=drp2.options.length -1;i++){
drp2.remove(i);
}
var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
var oDoc = new ActiveXObject("MSXML2.DOMDocument");
oHttpReq.open("POST", "webform6.aspx?state="+state, false);
oHttpReq.send("");
result = oHttpReq.responseText;
oDoc.loadXML(result);
items = oDoc.selectNodes("//CITY/Table");
for (var item = items.nextNode(); item; item = items.nextNode()){
var city = item.selectSingleNode("//city").nodeTypedValue;
var newOption = document.createElement("OPTION");
newOption.text = city;
newOption.value = city;
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> 上面的页面中有两个DropDownList和一段js脚本,该脚本可以直接写在页面也可以写在后台在Regeist到页面上(后者更灵活一些)该页的后台代码如下所示,在Page_Load里面写如下的代码:
if(!this.IsPostBack){
SqlConnection con = new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=sa;");
SqlDataAdapter da = new SqlDataAdapter("select state from authors group by state",con);
DataSet ds = new DataSet();
this.DropDownList1.DataTextField = "State";
this.DropDownList1.DataValueField = "State";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onchange","load(this.options[this.selectedIndex].innerText)");
} 在上面的代码中我们做了两件事情: 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代码如下: private void Page_Load(object sender, System.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=localhost;database=pubs;uid=sa;pwd=sa;");
SqlDataAdapter da = new SqlDataAdapter("select city from authors where state = '"+state+"'",con);
DataSet ds = new DataSet("CITY");
da.Fill(ds);
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();
} 该方法得到用户选择的state通过查询以后得到一个DataSet对象,使用该对象的WriteXML方法直接将内容写到Response.OutputStream里面然后传递到客户端,客户端的load方法通过result =oHttpReq.responseText;句话得到一个XML字符串,最后解析此串。 该方法可以实现无刷新的联动DropDownList,数据是从后台的数据库中得到的,希望可以起到抛砖引玉的作用,对文章有什么意见或者看法可以发邮件到[email protected]或者在CSDN中给我留短信我的ID是cuike519!谢谢阅读!
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;namespace issuebase.zjxx
{
/// <summary>
/// zj_xl_dl 的摘要说明。
/// </summary>
public class zj_xl_dl : System.Web.UI.Page
{
protected DBOperate mydb=new DBOperate();
private void Page_Load(object sender, System.EventArgs e)
{ if(this.Request["id"]!=null)
{
int state = Convert.ToInt32(this.Request["id"]); string param=state.ToString();
string s = "Select zj.id as id,zj.name as name,pet_x_class.id as id_x_class,pet_x_class.x_class as x_class,pet_x_class.f_id as id_d_class from zj left join pet_x_class on pet_x_class.id=zj.x_class where zj.id="+param+"";// where x_class="+param+""; //where id="+param+"";
mydb.Datacokis2(s,2); XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.IndentChar = ' ';
mydb.ds2.WriteXml(writer);
writer.Flush();
Response.End();
writer.Close();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}
function load(ClassID){ //ClassID为接收传递的大类编号
var drp2 = document.getElementById("DropDownList2");
function RemoveAll(oElem) { //清除DropDownList2的所有项
var i = 0;
for (i = oElem.length; i >= 0; i--){
oElem.options.remove(i);
}
}
RemoveAll(drp2)
var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
var oDoc = new ActiveXObject("MSXML2.DOMDocument");
oHttpReq.open("POST", "DropChild.aspx?id="+ClassID, false); //调用读取小类数据的页面,将大类
// 编号值传递过去
oHttpReq.send("");
result = oHttpReq.responseText;
oDoc.loadXML(result);
items1 = oDoc.selectNodes("//pet_x_class/Table/x_class"); //读取所有请求大类所属小类的类名
items2 = oDoc.selectNodes("//pet_x_class/Table/id"); //读取所有请求大类所属小类的编号
var select0 = document.createElement("OPTION");
select0.text="请选择";
select0.value="0";
drp2.options.add(select0);
var itemsLength=items1.length;
for(i=0;i<itemsLength;i++) //将小类的类名和编号赋予DropDownList2
{
var newOption = document.createElement("OPTION");
newOption.text=items1[i].text;
newOption.value=items2[i].text;
drp2.options.add(newOption);
}
}
function zj_load(ClassID){ //ClassID为接收传递的大类编号
var drp2 = document.getElementById("DropDownList3");
function RemoveAll(oElem) { //清除DropDownList2的所有项
var i = 0;
for (i = oElem.length; i >= 0; i--){
oElem.options.remove(i);
}
}
RemoveAll(drp2)
var oHttpReq_zj = new ActiveXObject("MSXML2.XMLHTTP");
var oDoc_zj = new ActiveXObject("MSXML2.DOMDocument");
oHttpReq_zj.open("POST", "../zj_DropChild.aspx?id="+ClassID, false); //调用读取小类数据的页面,将大类
// 编号值传递过去
// alert(ClassID);
oHttpReq_zj.send("");
result_zj = oHttpReq_zj.responseText;
oDoc_zj.loadXML(result_zj);
items1_zj = oDoc_zj.selectNodes("//goods_x_class/Table/name"); //读取所有请求大类所属小类的类名
items2_zj = oDoc_zj.selectNodes("//goods_x_class/Table/id"); //读取所有请求大类所属小类的编号
// alert(items2_zj[1].text);
//alert(items1_zj[0].text);
var select_zj0 = document.createElement("OPTION");
select_zj0.text="请选择";
select_zj0.value="0";
drp2.options.add(select_zj0);
var itemsLength_zj=items1_zj.length;
//alert(itemsLength); for(i=0;i<itemsLength_zj;i++) //将小类的类名和编号赋予DropDownList2
{
var newOption_zj = document.createElement("OPTION");
newOption_zj.text=items1_zj[i].text;
newOption_zj.value=items2_zj[i].text;
drp2.options.add(newOption_zj);
}
}
留下邮箱,给你个现成地
[email protected]
[email protected]
好多网上都用到这样的代码
比如点省份,另一个出现城市的那种就可以了
谢谢!!
[ 发送成功 ]
邮件已通过反病毒引擎扫描 您已经成功将信发送到: [email protected]