微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CascadingDropDown +Webservice:实现联动下拉框


本程序包含Default2.aspx、Default2.aspx.cs、CaRSService.asmx、App_Code/CaRSService.cs、CountrysService.xml五个文件;标红色的代码表示要注意的:
1、Default2.aspx完整代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2"  EnableEventValidation="false"@H_404_12@ %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />

        <table>
            <tr>
                <td>Make</td>
                <td><asp:DropDownList ID="DropDownList1" runat="server" Width="170" /></td>
            </tr>
            <tr>
                <td>Model</td>
                <td><asp:DropDownList ID="DropDownList2" runat="server" Width="170" /></td>
            </tr>
            <tr>
                <td>Color</td>
                <td><asp:DropDownList ID="DropDownList3" runat="server" Width="170" AutopostBack="true"
                    OnSelectedindexChanged="DropDownList3_SelectedindexChanged" /></td>
            </tr>
        </table>
        <br />
        <asp:XmlDataSource
            ID="XmlDataSource1" runat="server" DataFile="~/App_Data/CountrysService.xml"></asp:XmlDataSource>
       
        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1"
            Category="country"  PromptText="Please select a make"  LoadingText="[Loading makes...]"
            ServicePath="CaRSService.asmx" ServiceMethod="GetDropDownContents" />
           
        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
            Category="state" PromptText="Please select a model" LoadingText="[Loading models...]"
            ServiceMethod="GetDropDownContentsPageMethod" ParentControlID="DropDownList1" />
           
        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3"
            Category="university" PromptText="Please select a color" LoadingText="[Loading colors...]"
            ServicePath="CaRSService.asmx" ServiceMethod="GetDropDownContents"
            ParentControlID="DropDownList2" />
     
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" RenderMode="inline">
            <ContentTemplate>
                <asp:Label ID="Label1" runat="server" Text="[No response provided yet]" />
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="DropDownList3" EventName="SelectedindexChanged" />
            </Triggers>
        </asp:UpdatePanel>

    </form>
</body>

</html>
2、Default2.aspx.cs完整代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Web.Services;@H_404_12@
using System.Collections.Specialized;@H_404_12@
using AjaxControlToolkit;@H_404_12@
public partial class Default2 : System.Web.UI.Page
{
    protected void DropDownList3_SelectedindexChanged(object sender,EventArgs e)
    {
        // Get selected values
        string make = DropDownList1.SelectedItem.Text;
        string model = DropDownList2.SelectedItem.Text;
        string color = DropDownList3.SelectedItem.Text;
        // Output result string based on which values are specified
        if (string.IsNullOrEmpty(make))
        {
            Label1.Text = "Please select a make.";
        }
        else if (string.IsNullOrEmpty(model))
        {
            Label1.Text = "Please select a model.";
        }
        else if (string.IsNullOrEmpty(color))
        {
            Label1.Text = "Please select a color.";
        }
        else
        {
            Label1.Text = string.Format("You have chosen a {0} {1} {2}. Nice University!",color,make,model);
        }
    }


    [WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string kNownCategoryValues,string category)
    {
        return new CaRSService().GetDropDownContents(kNownCategoryValues,category);
    }

}

3、添加CaRSService.asmx不用改CaRSService.asmx,App_Code/CaRSService.cs完整代码

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Xml;@H_404_12@
using System.Collections.Specialized;@H_404_12@
/// <summary>
/// CaRSService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]@H_404_12@
public class CaRSService : System.Web.Services.WebService {

    private static XmlDocument _document;
    private static object _lock = new object();

    // we make these public statics just so we can call them from externally for the
    // page method call
    //
    public static XmlDocument Document
    {
        get
        {
            lock (_lock)
            {
                if (_document == null)
                {
                    // Read XML data from disk
                    _document = new XmlDocument();
                    //_document.Load(HttpContext.Current.Server.MapPath("~/App_Data/CaRSService.xml"));
                    _document.Load(HttpContext.Current.Server.MapPath("~/App_Data/CountrysService.xml"));
                }
            }
            return _document;
        }
    }
    //Hierarchy层级的意思
    public static string[] Hierarchy
    {
        get
        {

            //return new string[] { "make","model" };
            return new string[] { "country","state" };
        }
    }


    public CaRSService () {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

    [WebMethod]
    public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string kNownCategoryValues,string category)
    {
        // Get a dictionary of kNown category/value pairs
        StringDictionary kNownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKNownCategoryValuesstring(kNownCategoryValues);

        // Perform a simple query against the data document
        return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document,Hierarchy,kNownCategoryValuesDictionary,category);
    }
}

4、CountrysService.xml完整:

<?@H_404_12@ xml version="1.0" encoding="utf-8" @H_404_12@ ?>@H_404_12@

@H_404_12@ <@H_404_12@ CountrysService@H_404_12@ >@H_404_12@

  

  @H_404_12@ country @H_404_12@ name@H_404_12@ ="China"@H_404_12@

    @H_404_12@ state @H_404_12@ ="BeiJing"@H_404_12@

      @H_404_12@ university @H_404_12@ ="BeiJing University"@H_404_12@  @H_404_12@ />@H_404_12@ ="Tsinghua University "@H_404_12@ ="Renmin University "@H_404_12@ </@H_404_12@ state@H_404_12@ ="ShangHai"@H_404_12@ ="ShangHai Jiao Tong University"@H_404_12@ ="FuDan University"@H_404_12@ ="FuJian"@H_404_12@ ="FuJian normal University"@H_404_12@

        @H_404_12@ People @H_404_12@ ="Chuang Huang Leen"@H_404_12@  value@H_404_12@ ="Chuang Huang Leen (value)"@H_404_12@ university @H_404_12@ ="Xiamen University"@H_404_12@

  @H_404_12@ country@H_404_12@ ="USA"@H_404_12@ ="USA (value)"@H_404_12@ ="New York"@H_404_12@ ="New York (value)"@H_404_12@ ="New York University"@H_404_12@ ="New York University (value)"@H_404_12@ ="SATATE UNIVERSITY OF NEW YORK AT STONY broOK"@H_404_12@ ="SATATE UNIVERSITY OF NEW YORK AT STONY broOK (value)"@H_404_12@ ="Columbia University"@H_404_12@ ="Columbia University (value)"@H_404_12@ ="New Jersey"@H_404_12@ ="New Jersey (value)"@H_404_12@ ="Princeton University"@H_404_12@ ="Princeton University (value)"@H_404_12@ ="Seton Hall University"@H_404_12@ ="Seton Hall University (value)"@H_404_12@ ="Boston"@H_404_12@ ="Boston (value)"@H_404_12@ ="Harvard University "@H_404_12@ ="Harvard University (value)"@H_404_12@


    @H_404_12@ ="UK"@H_404_12@ ="UK (value)"@H_404_12@ ="London"@H_404_12@ ="London (value)"@H_404_12@ ="University College London"@H_404_12@ ="University College London (value)"@H_404_12@ ="Imperial Coll London"@H_404_12@ ="Imperial Coll London (value)"@H_404_12@ ="Univ Coll London"@H_404_12@ ="Univ Coll London (value)"@H_404_12@ ="Cambridge"@H_404_12@ ="Cambridge (value)"@H_404_12@ ="University of Cambridge"@H_404_12@ ="University of Cambridge (value)"@H_404_12@ ="University of Oxford"@H_404_12@ ="University of Oxford (value)"@H_404_12@ ="Manchester"@H_404_12@ ="Manchester (value)"@H_404_12@ ="University Manchester"@H_404_12@ ="University Manchester (value)"@H_404_12@

@H_404_12@
5、ok!参考How Do I--www.asp.net---video 3

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐