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

<!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;
using System.Collections.Specialized;
using AjaxControlToolkit;
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;
using System.Collections.Specialized;
/// <summary>
/// CaRSService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
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完整:

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

< CountrysService >

  

  
country  name ="China"

    
state  ="BeiJing"

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

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

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


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

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

相关文章

1.使用ajax调用varxhr;functioninvoke(){if(window.ActiveXO...
               好不容易把WebService服务器...
1新建一个工程项目用来做服务端增加一个MyService1类文件pac...
packagecom.transsion.util;importjava.io.BufferedReader;i...
再生产wsdl文件时重写描述文件1usingSystem;2usingSystem.Co...
一般情况下,使用eclipse自带的jax-ws生成webservice会自动生...