CascadingDropDown +Webservice:实现联动下拉框

本文介绍了一个ASP.NET应用案例,该应用利用级联下拉框实现了基于国家、州和大学的选择功能。通过CarsService.asmx Web服务获取数据,并使用C#和AJAX Control Toolkit实现动态更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


本程序包含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 name="BeiJing">
      
<university name="BeiJing University" />
      
<university name="Tsinghua University " />
      
<university name="Renmin University " />
    
</state>
    
<state name="ShangHai">
      
<university name="ShangHai Jiao Tong University" />
      
<university name="FuDan University" />
    
</state>
    
<state name="FuJian">
      
<university name="FuJian Normal University" >
        
<People name="Chuang Huang Leen" value="Chuang Huang Leen (value)" />
      
</university >
      
<university name="Xiamen University" />
    
</state>
  
</country>
  
  
<country name="USA" value="USA (value)">
    
<state name="New York" value="New York (value)">
      
<university name="New York University" value="New York University (value)" />
      
<university name="SATATE UNIVERSITY OF NEW YORK AT STONY BROOK" value="SATATE UNIVERSITY OF NEW YORK AT STONY BROOK (value)" />
      
<university name="Columbia University" value="Columbia University (value)" />
    
</state>
    
<state name="New Jersey" value="New Jersey (value)">
      
<university name="Princeton University" value="Princeton University (value)" />
      
<university name="Seton Hall University" value="Seton Hall University (value)" />
    
</state>
    
<state name="Boston" value="Boston (value)">
      
<university name="Harvard University " value="Harvard University (value)" />
    
</state>
  
</country>

    
<country name="UK" value="UK (value)">
      
<state name="London" value="London (value)">
        
<university name="University College London" value="University College London (value)" />
        
<university name="Imperial Coll London" value="Imperial Coll London (value)" />
        
<university name="Univ Coll London" value="Univ Coll London (value)" />
      
</state>
      
<state name="Cambridge" value="Cambridge (value)">
        
<university name="University of Cambridge" value="University of Cambridge (value)" />
        
<university name="University of Oxford" value="University of Oxford (value)" />
      
</state>
      
<state name="Manchester" value="Manchester (value)">
        
<university name="University Manchester" value="University Manchester (value)" />
      
</state>
  
</country>
</CountrysService>

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值