求实现无刷新的DropdownList联动效果的好方法

xiezhi 2005-01-19 03:07:14
CSDN上有一个实现无刷新的DropdownList联动效果的方法,觉得很陋,还到处被转载。有DataSet干吗要查两次数据库,用DataView就完全能分清父子关系,而且页面也就只要一个就好了,多弄一个页面浪费。哪位高手弄个高明点的写法,我这送分了!

只说大话不办实事的家伙滚一边去。
欢迎能解决问题和帮顶的兄弟!
...全文
520 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
辉说慧语 2005-09-05
  • 打赏
  • 举报
回复
mark
雄牛 2005-08-15
  • 打赏
  • 举报
回复
CSDN上有一个实现无刷新的DropdownList联动效果的方法,觉得很陋,还到处被转载。
----------------------------
憑這句話,就足於證明你不是程序員,起碼只能算是個垃圾程序員!
有本事,你也寫一個出來給大家看看,你鳥什麼.....
danshengshi 2005-02-16
  • 打赏
  • 举报
回复
C# 呢?
minghui000 2005-02-16
  • 打赏
  • 举报
回复
up
xiezhi 2005-01-24
  • 打赏
  • 举报
回复
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();


楼上诸兄哪位知道怎么把这个XML写成一个文本的形式?打出来看看
programmer11 2005-01-21
  • 打赏
  • 举报
回复
帮你顶
xiezhi 2005-01-21
  • 打赏
  • 举报
回复
楼上能不能说具体点!
fanghaifei 2005-01-21
  • 打赏
  • 举报
回复
xmlhttp呀,也是可以访问数据库,但不刷新.
ar7_top 2005-01-21
  • 打赏
  • 举报
回复
想要不刷新
只能用客户端脚本
只要访问服务器,就肯定要刷新
anjan 2005-01-21
  • 打赏
  • 举报
回复
JS
dandantree 2005-01-21
  • 打赏
  • 举报
回复
Use SQL XML Config IIS,then use URL Query into a XMLDOM, you can implement that
xiezhi 2005-01-21
  • 打赏
  • 举报
回复
谢谢啦!
linlinunix 2005-01-20
  • 打赏
  • 举报
回复

数据太多的话只能查两次数据库 应为一点页面render dataset对象就不存在了 这个不是winform
xiezhi 2005-01-20
  • 打赏
  • 举报
回复
顶!
cpio 2005-01-20
  • 打赏
  • 举报
回复
哎,.net都把人搞成这样了

难道都不用JS了吗?
kenMoxi 2005-01-20
  • 打赏
  • 举报
回复
你可以使用以前ASP上面的方式,就是动态产生JS数组!
xiezhi 2005-01-20
  • 打赏
  • 举报
回复
楼上的这个就是CSDN上有一个实现无刷新的DropdownList联动效果
http://dev.csdn.net/article/40/40293.shtm
我要的不是这个,见我楼顶的贴子!
softye 2005-01-20
  • 打赏
  • 举报
回复
注明一下,是转贴
softye 2005-01-20
  • 打赏
  • 举报
回复
在做一个文章添加功能时,想在选择大类后,自动将其所属二级小类显示出来,使用DropDownList的SelectedIndexChanged事件可以很容易实现,但每次选择后页面总要刷新一次,让人感觉很不爽。为实现DropDownList无刷新二级联动,这几天在网上找了些资料,但都无法达到我想要的效果,经过反复调试,现已基本实现了此功能,现将代码附下。

一、数据库设计:
字段名 数据类型 说明
ClassID 自动编号 类编号
ClassName varchar(8) 类名
UpClassID int(4) 上级类编号
ClassLevel int(4) 类级别,1为大类,2为小类

二、设计步骤:
1、首先,我们新建一个页面DropTest.aspx,在其中放入两个DropDownList控件:DropDownList1和DropDownList2,其完整代码如下:
<%@ Page language="c#" Codebehind="DropTest.aspx.cs" AutoEventWireup="false" Inherits="studyWEB.DropTest" %>
<!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(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 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>
<asp:TextBox id="TH" runat="server" BorderStyle="None" ForeColor="White" BorderColor="White"></asp:TextBox>
<asp:Label id="Label1" runat="server"></asp:Label>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>
</body>
</HTML>
该页面的后台文件(DropDownList1.aspx.cs)中Page_Load内的代码如下:
if(!this.IsPostBack)
{
SqlConnection con = new SqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname 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,但如果大类为中文,则调用小类时出现无法显示的问题
// this.DropDownList2.Attributes.Add("onChange","javascript:document.Form1.TH.value=this.options[this.selectedIndex].value;"); //读取DropDownList2的值,将其赋给一个TextBox控件TH,以获取DropDownList2的值,为获取DropDownList2的值,网上有人说可通过使用隐藏的TextBox控件来获取,我未能实现,因为在客户端隐藏的TextBox控件也是不可用脚本来访问的,没法给其赋值,我只能通过将其样式、字体颜色设于背景相同来达到隐藏效果,这是一个很笨的方法,有谁有好的方法,请帮我。
}
此页面实现如下功能:首先从数据库内读取所有类级别为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=localhost;database=gswebDB;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname 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();
该方法得到用户选择的大类的编号,通过查询以后得到一个DataSet对象,使用该对象的WriteXML方法直接将内容写到Response.OutputStream里面然后传递到客户端,客户端的load方法通过result =oHttpReq.responseText;句话得到一个XML字符串,最后解析此串。
另外,测试获取DropDownList2值,添加了TextBox控件TH,当点击Button时,处理事件代码如下:
private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text=TH.Text;
}
xiezhi 2005-01-20
  • 打赏
  • 举报
回复
楼上的说什么呢,能明白一点么?
加载更多回复(4)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧