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

(转载)
Ajax.NET Ectention 实现的一个无刷新的多级动态下拉列表框,使用的3UpdatePanel,每一个中放一个DropDownList,分别为

DropDownList122,其中UpdatePanel2UpdatePanel1触发,UpdatePanel3UpdatePanel2UpdatePanel1共同触发,

也可以增加到很多级,只要类似的改代码就可以了。

以下为源代码

 

<%@ 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

下载文件

转载于:https://www.cnblogs.com/snow365/archive/2008/07/04/1235644.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值