中国设计秀欢迎投稿
中国品牌形像设计网
平面设计 画册 VI欣赏 包装 CG-插画 搜索 个人网页 Alexa排名 CSS 建站资源 下载专区 JS特效 品牌服装 服装院校 专题欣赏 SEO 图标欣赏 专题
深圳网站建设 广州网站设计 域名注册 上海网站建设 虚拟主机 广州网站建设 广州网页设计 签名设计 虚拟主机 域名注册 品牌形象设计 设计联盟
求创科技
上海网麒科技
中国福网
中国设计秀
亿恩科技
中国设计秀
中国设计秀
当前位置:网络学院首页 >> 编程开发 >> .net >> 用Ajax.net实现的一个无刷新的多级联动下拉列表框

用Ajax.net实现的一个无刷新的多级联动下拉列表框

来源:中国设计秀    作者:febird    点击:370     加入收藏    发表评论
0
顶一下
中资源
  用Ajax.NET Ectention 实现的一个无刷新的多级动态下拉列表框,使用的3个UpdatePanel,每一个中放一个DropDownList,分别为
  
  DropDownList1、2、2,其中UpdatePanel2由UpdatePanel1触发,UpdatePanel3由UpdatePanel2和UpdatePanel1共同触发,
  
  也可以增加到很多级,只要类似的改代码就可以了。
  
  以下为源代码 
   
  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  
  <!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>
  
  </head>
  
  <body>
  
   <form id="form1" runat="server">
  
   <asp:ScriptManager ID="ScriptManager1" runat="server" />
  
   <div style="float: left">
  
   <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  
   <ContentTemplate>
  
   <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Width="184px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" OnTextChanged="DropDownList1_SelectedIndexChanged" > 
   
   
   </asp:DropDownList>
  
   </ContentTemplate>
  
   </asp:UpdatePanel>
  
   </div>
  
   <div style="float: left">
  
   <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
  
   <ContentTemplate>
  
   <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" Width="168px" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" > 
   
   
   </asp:DropDownList>
  
   </ContentTemplate>
  
   <Triggers>
  
   <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
  
   </Triggers>
  
   </asp:UpdatePanel>
  
   </div>
  
   <asp:UpdatePanel ID="UpdatePanel3" runat="server">
  
   <ContentTemplate>
  
   <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True" Width="160px"> 
   
   
   </asp:DropDownList>
  
   </ContentTemplate>
  
   <Triggers>
  
   <asp:AsyncPostBackTrigger ControlID="DropDownList2" EventName="SelectedIndexChanged" />
  
   <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
  
   </Triggers>
  
   </asp:UpdatePanel>
  
   </form>
  
  </body>
  
  </html> 
    // Default.aspx.cs文件
  
  using System;
  
  using System.Data;
  
  using System.Configuration;
  
  using System.Web;
  
  using System.Web.Security;
  
  using System.Web.UI;
  
  using System.Web.UI.WebControls;
  
  using System.Web.UI.WebControls.WebParts;
  
  using System.Web.UI.HtmlControls;
  
  using System.Data.Sql;
  
  using System.Data.SqlClient;
  
  using System.Collections; 
   
   
  public partial class _Default : System.Web.UI.Page
  
  {
  
   protected void Page_Load(object sender, EventArgs e)
  
   {
  
   if (!IsPostBack)
  
   {
  
   DropDownList1.DataSource = CreateSource(0);
  
   DropDownList1.DataTextField = "TypeName";
  
   DropDownList1.DataValueField = "TypeID";
  
   DropDownList1.DataBind();
  
   int PreID = Convert.ToInt32(DropDownList1.SelectedValue);
  
   DropDownList2.DataSource = CreateSource(PreID);
  
   DropDownList2.DataTextField = "TypeName";
  
   DropDownList2.DataValueField = "TypeID";
  
   DropDownList2.DataBind();
  
   PreID = Convert.ToInt32(DropDownList2.SelectedValue);
  
   DropDownList3.DataSource = CreateSource(PreID);
  
   DropDownList3.DataTextField = "TypeName";
  
   DropDownList3.DataValueField = "TypeID";
  
   DropDownList3.DataBind();
  
   }
  
   }
  
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  
   {
  
   int PreID = Convert.ToInt32(DropDownList1.SelectedValue);
  
   if (CreateSource(PreID) != null)
  
   {
  
   DropDownList2.DataSource = CreateSource(PreID);
  
   DropDownList2.DataTextField = "TypeName";
  
   DropDownList2.DataValueField = "TypeID";
  
   DropDownList2.DataBind();
  
   PreID = Convert.ToInt32(DropDownList2.SelectedValue);
  
   if (CreateSource(PreID) != null)
  
   {
  
   DropDownList3.DataSource = CreateSource(PreID);
  
   DropDownList3.DataTextField = "TypeName";
  
   DropDownList3.DataValueField = "TypeID";
  
   DropDownList3.DataBind();
  
   }
  
   else
  
   {
  
   DropDownList3.Items.Clear();
  
   }
  
   }
  
   else
  
   {
  
   DropDownList2.Items.Clear();
  
   DropDownList3.Items.Clear();
  
  
  
   }
  
  
  
   }
  
   protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
  
   {
  
   int PreID = Convert.ToInt32(DropDownList2.SelectedValue);
  
   if (CreateSource(PreID) != null)
  
   {
  
   DropDownList3.DataSource = CreateSource(PreID);
  
   DropDownList3.DataTextField = "TypeName";
  
   DropDownList3.DataValueField = "TypeID";
  
   DropDownList3.DataBind();
  
   }
  
   else
  
   {
  
   DropDownList3.Items.Clear();
  
   }
  
   }
  
   protected ICollection CreateSource(int preId)
  
   {
  
   SqlConnection conn = new SqlConnection();
  
   conn.ConnectionString = ConfigurationManager.ConnectionStrings["NewsReleaseConnectionString"].ConnectionString;
  
   string sqlStr="Select TypeId,TypeName From ArticleType Where PreId=' "+preId.ToString()+"'";
  
   SqlDataAdapter sda = new SqlDataAdapter(sqlStr,conn);
  
   DataSet ds = new DataSet();
  
   sda.Fill(ds);
  
   if (ds.Tables[0].Rows.Count <= 0)
  
   {
  
   return null;
  
   }
  
   else
  
   {
  
   return ds.Tables[0].DefaultView;
  
   } 
   
      }
  
  }
  
  
  
  数据库中的相应表的SQL
  
  
  
  USE [NewsRelease]
  
  GO
  
  /****** 对象: Table [dbo].[ArticleType] 脚本日期: 12/05/2007 12:49:06 ******/
  
  SET ANSI_NULLS ON
  
  GO
  
  SET QUOTED_IDENTIFIER ON
  
  GO
  
  SET ANSI_PADDING ON
  
  GO
  
  CREATE TABLE [dbo].[ArticleType](
  
   [TypeID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
  
   [TypeName] [varchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
  
   [PreID] [bigint] NOT NULL,
  
   [AddTime] [datetime] NULL,
  
   CONSTRAINT [PK_ArticleType] PRIMARY KEY CLUSTERED
  
  (
  
   [TypeID] ASC
  
  )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
  
  ) ON [PRIMARY] 
   
   
  GO
  
  SET ANSI_PADDING OFF
热点文章/相关文章
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明 | 网站公告 | 友情链接 | 留言 | 旧版入口