十二、CascadingDropDown——级联下拉列表

 

CascadingDropDown级联下拉列表,就是一个无刷新的多级联动。

 

举例如下:

这里用一个三级联动的例子说明,其它的二级或更多级的联动都很容易了。

1.       添加一个ScriptManage

2.       添加三个DropDownList和三个CascadingDropDown,每个DropDownList都需要对应一个CascadingDropDown

        <cc1:CascadingDropDown ID="CascadingDropDown_1" runat="server"

                               TargetControlID="DropDownList_1"        //对应的下拉框表控件的ID

                               ServicePath="CascadingDropDown.asmx"        //要调用的web服务

                               ServiceMethod="getListItems"        //要调用的web服务的方法

                               Category="一级"        //当前下拉列表的类型

                               LoadingText="正在加载一级列表。。。"        //正在加载时显示的文本

                               PromptText="请选择一级列表">        //当下拉列表中无数据或没有选择数据时给用户的提示

        </cc1:CascadingDropDown>

        <asp:DropDownList ID="DropDownList_1" runat="server">

        </asp:DropDownList>

        <cc1:CascadingDropDown ID="CascadingDropDown_2" runat="server"

                               ParentControlID="DropDownList_1"        //对于联动的最上层下拉列表不需要这个参数,而其它的都需要

                               TargetControlID="DropDownList_2"

                               ServicePath="CascadingDropDown.asmx"

                               ServiceMethod="getListItems"

                               Category="二级"

                               LoadingText="正在加载二级列表"

                               PromptText="请选择二级列表">

        </cc1:CascadingDropDown>

        <asp:DropDownList ID="DropDownList_2" runat="server">

        </asp:DropDownList>

        <cc1:CascadingDropDown ID="CascadingDropDown_3" runat="server"

                               ParentControlID="DropDownList_2"

                               TargetControlID="DropDownList_3"

                               ServicePath="CascadingDropDown.asmx"

                               ServiceMethod="getListItems"

                               Category="三级"

                               LoadingText="正在加载三级列表"

                               PromptText="请选择三级列表">

        </cc1:CascadingDropDown>

        <asp:DropDownList ID="DropDownList_3" runat="server">

        </asp:DropDownList>

3.       编写web服务

<%@ WebService Language="C#" Class="AjaxToolkit.CascadingDropDown" %>

using System;

using System.Collections;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Linq;

 

namespace AjaxToolkit

{

    /// <summary>

    ///CascadingDropDown 的摘要说明

    /// </summary>

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。

    [System.Web.Script.Services.ScriptService]

    public class CascadingDropDown : System.Web.Services.WebService

    {

        public CascadingDropDown()

        {

            //如果使用设计的组件,请取消注释以下行

            //InitializeComponent();

        }

 

        [WebMethod]

        public AjaxControlToolkit.CascadingDropDownNameValue[] getListItems(string knownCategoryValues, string category)        //必须使用CascadingDropDownNameValue数组返回

        {

            System.Collections.Generic.List<AjaxControlToolkit.CascadingDropDownNameValue> cdnvs =

                new System.Collections.Generic.List<AjaxControlToolkit.CascadingDropDownNameValue>();

 

            System.Threading.Thread.Sleep(1 * 1000);   //延时一秒

            System.Collections.Specialized.StringDictionary kv =

AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);   //获取下拉列表值

            if (category == "一级")

            {

                for (int i = 0; i < 6; i++)

                {

                    string name = category + "_" + i.ToString();

                    string value = "1_" + i.ToString();

                    cdnvs.Add(new AjaxControlToolkit.CascadingDropDownNameValue(name, value));

                }

            }

            else if (category == "二级")

            {

                for (int i = 0; i < 6; i++)

                {

                    string name = "二级_" + i.ToString();

                    string value = kv["一级"].ToString() + "_" + i.ToString();    //因为kvICollection接口,所以必须使用[“”]方法读取数据,其中“”是CascadingDropDown属性中的Category

                    cdnvs.Add(new AjaxControlToolkit.CascadingDropDownNameValue(name, value));

                }

            }

            else if (category == "三级")

            {

                for (int i = 0; i < 6; i++)

                {

                    string name = "三级_" + i.ToString();

                    string value = kv["二级"].ToString() + "_" + i.ToString();

                    cdnvs.Add(new AjaxControlToolkit.CascadingDropDownNameValue(name, value));

                }

            }

           

            return cdnvs.ToArray<AjaxControlToolkit.CascadingDropDownNameValue>();       //返回CascadingDropDownNameValue类型数组

        }

    }

}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值