在做一个文章添加功能时,想在选择大类后,自动将其所属二级小类显示出来,使用DropDownList的SelectedIndexChanged事件可以很容易实现,但每次选择后页面总要刷新一次,让人感觉很不爽。为实现DropDownList无刷新二级联动,这几天在网上找了些资料,但都无法达到我想要的效果,经过反复调试,现已基本实现了此功能,现将代码附下。
一、数据库设计:
字段名 数据类型 说明
ClassID 自动编号 类编号
ClassName varchar(8) 类名
UpClassID int(4) 上级类编号
ClassLevel int(4) 类级别,1为大类,2为小类
实现无刷新DropdownList联动效果(2)
二、设计步骤:
1、首先,我们新建一个页面DropTest.aspx,在其中放入两个DropDownList控件:DropDownList1和DropDownList2,其完整代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropTest.aspx.cs" Inherits="DropTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript">
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?ClassID="+ClassID, false); //调用读取小类数据的页面,将大类编号值传递过去
oHttpReq.send("");
result = oHttpReq.responseText;
oDoc.loadXML(result);
items1 = oDoc.selectNodes("//CLASSNAME/Table/ClassName"); //读取所有请求大类所属小类的类名
items2 = oDoc.selectNodes("//CLASSNAME/Table/ClassID"); //读取所有请求大类所属小类的编号
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);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" /></div>
</form>
</body>
</html>
该页面的后台文件(DropTest.aspx.cs)中Page_Load内的代码如下:
if (!this.IsPostBack)
{
SqlConnection con = new SqlConnection("server=192.168.88.70;database=PEDU;uid=sa;pwd=tmdsbwbd011;");
SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from CityDB where ClassLevel=1", con);
DataSet ds = new DataSet();
da.Fill(ds);
this.DropDownList1.DataSource = ds.Tables[0].DefaultView;
this.DropDownList1.DataTextField = "ClassName";
this.DropDownList1.DataValueField = "ClassID";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onchange", "load(this.options[this.selectedIndex].value)"); //将ClassID作为参数传递给脚本函数load(ClassID),如果要传递的是ClassName,应将value改为innerText,但如果大类为中文,则调用小类时出现无法显示的问题
}
此页面实现如下功能:首先从数据库内读取所有类级别为1(即大类)的类名和类编号,绑定到DropDownList1控件上;然后通过DropDownList1的Attributes属性调用javascript函数load(ClassID);load()函数通过调用DropChild.aspx页面,读取XML流,得到大类所属小类的ClassName和ClassID。
2、新建DropChild.aspx页面文件,其中不插入任何控件和文本,只在其后台文件(DropChild.aspx.cs)中的Page_Load中加入以下代码:
if(this.Request["ClassID"]!=null)
{
int state = Convert.ToInt32(this.Request["ClassID"]);
SqlConnection con = new SqlConnection("server=192.168.88.70;database=PEDU;uid=sa;pwd=tmdsbwbd011;");
SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from CityDB where UPClassID='" + state + "'",con);
DataSet ds = new DataSet("CLASSNAME");
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();
}
别忘了加上System.Xml的声明空间引用。
该方法得到用户选择的大类的编号,通过查询以后得到一个DataSet对象,使用该对象的WriteXML方法直接将内容写到Response.OutputStream里面然后传递到客户端,客户端的load方法通过result =oHttpReq.responseText;句话得到一个XML字符串,最后解析此串。
另外,测试获取DropDownList2值,当点击Button时,处理事件代码如下:
protected void Button1_Click1(object sender, EventArgs e)
{
Label1.Text = this.Request.Form["DropDownList1"].ToString() + this.Request.Form["DropDownList2"].ToString();
}
相关推荐
c实现无刷新dropdownlist联动效果
如何实现无刷新的DropdownList联动效果
c#实现无刷新的DropdownList联动效果.pdf
该程序通过Javascript 实现无刷新的,两级和三级DDL联动
这个是ajax 实现DropDownList无刷新联动。里面有数据库结构和内容,表结构的是sql2000的, 关键代码: <SCRIPT language="javascript"> //城市------------------------------ function cityResult() ...
随着Jquery1.4的发布,Jquery运用越来越多了,让我们来实现以前经常用到的DropDownList无刷新联动。
无刷新二级联动dropdownlist菜单.rar
DropDownList无刷新三级联动实现了!!!!
年月两级联动无刷新。年月显示最近十年数据。文件形式为.net用户控价下载后直接导入到项目后用法同用户控件用法。很方便
.net MVC下,AJAX实现DropDownList数据绑定,并实现无刷新城市联动。 内含SQL Server 数据库执行语句,只要运行一下就OK了。 运行代码时注意别忘了更改成所使用数据库的ServerName,LoginName,Password。
DropDownList无刷新二级联动控件,通过ClientScript.GetCallbackEventReference实现了无刷新联动。支持样式,视图状态的保存。并将DropDownList的回发操作封装到事件中。使用控件就像使用一般的DropDownList服务器...
AJAX做的dropdownlist无刷新动态绑定
实现了DropDownList的联动效果,选择省,则显示相应的市在另外一个DropDownList,省和市分别从数据库中读取,支持局部刷新。 附件内附全国最全的省、市数据库信息,包括创建省数据表的语句和市数据表的语句,及插入...
dropdownlist 省市二级联动 无刷新
使用DropDownList控件实现省市信息联动,使用ajax技术,实现无刷新操作,欢迎下载!
DropDownList省市区Ajax无刷新三级联动 带最新省市区数据库脚本 可以直接拿来用 学习
ASP.NET DropDownList 下拉菜单,联动时页面无刷新
一个无刷新三级联动关联DropDownList小例子,一般都能用得到实用的是xml数据源
商业编程-源码-XML无刷新三级联动下拉DropDownList示例.zip