急求无刷新dropdownlist代码 代码越简单越好要一个页面中的多个dropdownlist都可以使用的先谢了急急急 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <SCRIPT LANGUAGE = JavaScript>var s=["s1","s2","s3"];var opt0 = ["省份","地级市","市、县级市、县"];function setup(){ for(i=0;i<s.length-1;i++) document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")"); change(0);}//--></SCRIPT>部分代码! 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字符串,最后解析此串。 明白思路就不难了!var ProductID = document.all.txtProductID.value; var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //alert("AccountPrice.aspx?strPrice="+str+"&ProductID="+ProductID+"&temp=" + Math.random()); //加了个随机参数temp防止叶面缓存 xmlhttp.open("get","AccountPrice.aspx?strPrice="+str+"&ProductID="+ProductID+"&temp=" + Math.random(), false); xmlhttp.send(); var result; result= xmlhttp.responseText; xmlhttp = null; strPrice是前台传到后台的参数,后台request["xxxx"]就可以了result就是后台传过来的结果. http://dev.csdn.net/develop/article/33/33880.shtm如果需要源代码工程可以给我发消息留下email! 我要这个无刷新的dropdownlist代码,谢谢[email protected] http://www.cnblogs.com/freehorse/articles/noFreshOne.html asp.net后台如何获得前台所有HTML控件的值,不用runat=“server” 问一个表格导出至EXCEL的问题 关于取前3个字符的SQL语句 求图片上传代码 又是treeview的问题? 看不懂,还请指教!!! 奇怪,我的TEXTBOX为什么不能在Panel上随意位置摆放,放上去就紧帖左上角,不能动弹~~ 时间转换问题,求大神指教.... select from tb where 条件1或条件2 请高手指点GetProfiles()函数的用法 如何选中DropDownList中的相关项 谁来帮帮我。。。谢谢...有点复杂(对于我) 也有点简单(对于你)
var s=["s1","s2","s3"];
var opt0 = ["省份","地级市","市、县级市、县"];
function setup()
{
for(i=0;i<s.length-1;i++)
document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")");
change(0);
}
//-->
</SCRIPT>
部分代码!
下面我将描述一种原始的方法,之所以说它原是是因为这种方法在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字符串,最后解析此串。
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//alert("AccountPrice.aspx?strPrice="+str+"&ProductID="+ProductID+"&temp=" + Math.random());
//加了个随机参数temp防止叶面缓存
xmlhttp.open("get","AccountPrice.aspx?strPrice="+str+"&ProductID="+ProductID+"&temp=" + Math.random(), false);
xmlhttp.send();
var result;
result= xmlhttp.responseText;
xmlhttp = null;
strPrice是前台传到后台的参数,
后台request["xxxx"]就可以了
result就是后台传过来的结果.
如果需要源代码工程可以给我发消息留下email!
[email protected]