找回密码
 注册
搜索
热搜: 回贴

ASP.net:实现无刷新DropDownList联动效果

2009-12-13 13:28| 发布者: admin| 查看: 82| 评论: 0|原作者: 段誉

■在做一个文章添加功能时,想在选择大类后,......


  在做一个文章添加功能时,想在选择大类后,自动将其所属二级小类显示出来,使用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" %>


  
  WebForm2
  
  
  
  
  
  
  
  

  
  
  
  
  
  

  

该页面的后台文件(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的值.

最新评论

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 01:43 , Processed in 0.135883 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

返回顶部