AJAX控件之CascadingDropDow

前台代码:

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

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!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>CascadingDropDown控件使用</title>
    
<style type="text/css">
        .style1
        
{
            width: 400px;
        }

        .style2
        
{
            width: 228px;
        }

    
</style>
</head>
<body>
    
<form id="form1" runat="server">
        
<asp:ScriptManager ID="ScriptManager1" runat="server" />
        
<div style="text-align: center">
            
<table class="style1" border="1">
                
<tr>
                    
<td>
                        省份:
</td>
                    
<td align="left" class="style2">
                        
<asp:DropDownList ID="DropDownList1" runat="server" Width="190px" Height="24px">
                        
</asp:DropDownList>
                    
</td>
                
</tr>
                
<tr>
                    
<td>
                        县:
</td>
                    
<td align="left" class="style2">
                        
<asp:DropDownList ID="DropDownList2" runat="server" Width="188px" Height="20px">
                        
</asp:DropDownList>
                    
</td>
                
</tr>
                
<tr>
                    
<td>
                        市:
</td>
                    
<td align="left" class="style2">
                        
<asp:DropDownList ID="DropDownList3" runat="server" Height="22px" Width="185px" 
                            AutoPostBack
="True" onselectedindexchanged="DropDownList3_SelectedIndexChanged">
                        
</asp:DropDownList>
                    
</td>
                
</tr>
            
</table>
            
<cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" 
                Category
="province" PromptText="请选择省" ServiceMethod="GetDropDownContents" 
                ServicePath
="WebService.asmx" TargetControlID="DropDownList1">
            
</cc1:CascadingDropDown>
            
<cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" Category="city" 
                ParentControlID
="DropDownList1" PromptText="请选择市" 
                ServiceMethod
="GetDropDownContents" TargetControlID="DropDownList2" ServicePath="WebService.asmx">
            
</cc1:CascadingDropDown>
            
<cc1:CascadingDropDown ID="CascadingDropDown3" runat="server" Category="county" 
                ParentControlID
="DropDownList2" PromptText="请选择县" ServiceMethod="GetDropDownContents" 
                TargetControlID
="DropDownList3" ServicePath="WebService.asmx">
            
</cc1:CascadingDropDown>
            
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                
<ContentTemplate>
                    
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                
</ContentTemplate>
                
<Triggers>
                    
<asp:AsyncPostBackTrigger ControlID="DropDownList3" 
                        EventName
="SelectedIndexChanged" />
                
</Triggers>
            
</asp:UpdatePanel>
        
</div>
    
</form>
</body>
</html>
后台代码:
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;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    
{
        
if (DropDownList3.SelectedItem.Text == "")
        
{
            Label1.Text 
= "你还没有选择!";
        }

        
else
        
{
            Label1.Text
="你的籍贯是"+DropDownList1.SelectedItem.Text+DropDownList2.SelectedItem.Text+DropDownList3.SelectedItem.Text;
        }

    }

}
XML页代码:
<?xml version="1.0" encoding="utf-8" ?>
<WebServer>
  
<province name="河南省">
    
<city name="南阳市">
      
<county name="宛城区">
      
</county>
      
<county name="卧龙区">
      
</county>
      
<county name="高新区">
      
</county>
    
</city>
    
<city name="郑州市">
      
<county name="武侯区">
      
</county>
      
<county name="青阳区">
      
</county>
      
<county name="海安区">
      
</county>
    
</city>
  
</province>
  
<province name="广东省">
    
<city name="潮州市">
      
<county name="潮安县">
      
</county>
    
</city>
    
<city name="河源市">
      
<county name="东源县">
      
</county>
      
<county name="和平县">
      
</county>
    
</city>
  
</province>
    
<province name="江苏省">
    
<city name="盐城市">
      
<county name="建湖县">
      
</county>
      
<county name="盐城县">
      
</county>
    
</city>
    
<city name="南通市">
      
<county name="海安县">
      
</county>
      
<county name="如东县">
      
</county>
    
</city>
    
<city name="扬州市">
      
<county name="宝应县">
      
</county>
    
</city>
  
</province>
</WebServer>

websevrer页代码:
using System;
using System.Web;
using System.Collections.Specialized;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;

/// <summary>
/// WebService 的摘要说明
/// </summary>

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{

    
public WebService()
    
{
    }


    
private static XmlDocument xmlDoc;
    
private static object objectLock = new object();

    
public static XmlDocument Document
    
{
        
get
        
{
            
lock (objectLock)
            
{
                
if (xmlDoc == null)
                
{
                    xmlDoc 
= new XmlDocument();
                    xmlDoc.Load(HttpContext.Current.Server.MapPath(
"~/App_Data/XML.xml"));
                }

            }

            
return xmlDoc;
        }

    }


    
public static string[] Hierarchy
    
{
        
get return new string[] "province""city" }; }
    }


    [WebMethod]
    
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category)
    
{
        StringDictionary knownCategoryValuesDictionary 
= AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        
return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category);
    }


}


posted @ 2008-03-23 13:02  MicroCoder  阅读(287)  评论(1编辑  收藏  举报