◎网速测试 ◎IP查询 ◎Alexa排名
首 页 关于我们 网站建设 网站推广 网站优化 业务流程 案例展示 推荐文章
 
文章分类
网站优化SEO
网站安全管理
网络营销策略
建站指南
设计前沿
代码分享
用Ajax.net实现的一个无刷新的多级联动下拉列表框

日期:2008-5-26

 用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


版权所有 桂林网站建设网 Copyright © GX360 All Rights Reserved.
桂ICP备06006609号