全国省市县三级联动(数据完整版)


网页前台代码:
<%@ 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.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">
    
<div align="center">
        
<asp:ScriptManager ID="ScriptManager1" runat="server">
        
</asp:ScriptManager>
    
<fieldset >
    
<legend>全国省市县</legend>
    
<fieldset style="width: 265px">
    
<legend></legend>
                    
<asp:DropDownList ID="DropDownList1" runat="server" 
            AutoPostBack
="True">
                    
</asp:DropDownList>
                    
<cc1:CascadingDropDown ID="DropDownList1_CascadingDropDown" runat="server" 
                        Category
="provice" Enabled="True" LoadingText="读取数据中.." PromptText="请选择省" 
                        TargetControlID
="DropDownList1" ServicePath="WebService.asmx" ServiceMethod="GetproviceNames">
                    
</cc1:CascadingDropDown>
    
</fieldset>
        
<fieldset style="width: 266px">
    
<legend></legend>
                    
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
                    
</asp:DropDownList>
                    
<cc1:CascadingDropDown ID="DropDownList2_CascadingDropDown" runat="server" 
                        Category
="city" Enabled="True" TargetControlID="DropDownList2" LoadingText="读取数据中.." PromptText="请选择市" ParentControlID="DropDownList1"
                          ServicePath
="WebService.asmx" ServiceMethod="GetCityNames">
                    
</cc1:CascadingDropDown>
    
</fieldset>
            
<fieldset style="width: 266px">
    
<legend></legend>
                
<asp:DropDownList ID="DropDownList3" runat="server">
                
</asp:DropDownList>
                
<cc1:CascadingDropDown ID="DropDownList3_CascadingDropDown" runat="server"  Category="District" TargetControlID="DropDownList3"
                    Enabled
="True" LoadingText="读取数据中.." ParentControlID="DropDownList2"
                    ServicePath
="WebService.asmx" ServiceMethod="GetDistrictNames">
                
</cc1:CascadingDropDown>
    
</fieldset>
 
</fieldset>

    
</div>
    
</form>
</body>
</html>
WebService代码
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

//访问数据库命名空间
using System.Data;
using System.Data.SqlClient;

//访问web.config命名空间
using System.Web.Configuration;

//导入ScriptServiceAttribute类的命名空间
using System.Web.Script.Services;

//返回CascadingDropDownNameValue数组所需的命名空间
using AjaxControlToolkit;
using System.Collections.Generic;
using System.Collections.Specialized;

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

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{

    
public WebService()
    
{

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


    [WebMethod]
    
public CascadingDropDownNameValue[] GetproviceNames(string knownCategoryValues, string category)
    
{
        List
<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        SqlConnection conn 
= new SqlConnection(WebConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
        SqlCommand comm 
= conn.CreateCommand();
        comm.CommandText 
= "select [ProvinceID],[ProvinceName] from [S_Province] order by 1";
        conn.Open();
        
try
        
{
            SqlDataReader dr 
= comm.ExecuteReader();
            
while (dr.Read())
            
{
                values.Add(
new CascadingDropDownNameValue(dr[1].ToString(), dr[0].ToString()));
            }

            
return values.ToArray();
        }

        
finally
        
{
            conn.Close();
        }

    }

    [WebMethod]
    
public CascadingDropDownNameValue[] GetCityNames(string knownCategoryValues, string category)
    
{
        StringDictionary kcv 
= CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        
if (!kcv.ContainsKey("provice"))
        
{
            
return null;
        }

        List
<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        SqlConnection conn 
= new SqlConnection(WebConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
        SqlCommand comm 
= conn.CreateCommand();
        comm.CommandText 
= "select [CityID],[CityName] from [S_City] where [ProvinceID]=@ProvinceID order by 1";
        comm.Parameters.Add(
"@ProvinceID", SqlDbType.Int).Value = kcv["provice"];
        conn.Open();
        
try
        
{
            SqlDataReader dr 
= comm.ExecuteReader();
            
while (dr.Read())
            
{
                values.Add(
new CascadingDropDownNameValue(dr[1].ToString(), dr[0].ToString()));
            }

            
return values.ToArray();
        }

        
finally
        
{
            conn.Close();
        }

    }

    [WebMethod]
    
public CascadingDropDownNameValue[] GetDistrictNames(string knownCategoryValues, string category)
    
{
        StringDictionary kcv 
= CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        
if (!kcv.ContainsKey("city"))
        
{
            
return null;
        }

        List
<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        SqlConnection conn 
= new SqlConnection(WebConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
        SqlCommand comm 
= conn.CreateCommand();
        comm.CommandText 
= "select [DistrictID],[DistrictName] from [S_District] where [CityID]=@CityID order by 1";
        comm.Parameters.Add(
"@CityID", SqlDbType.Int).Value = kcv["city"];
        conn.Open();
        
try
        
{
            SqlDataReader dr 
= comm.ExecuteReader();
            
while (dr.Read())
            
{
                values.Add(
new CascadingDropDownNameValue(dr[1].ToString(), dr[0].ToString()));
            }

            
return values.ToArray();
        }

        
finally
        
{
            conn.Close();
        }

    }

}


 数据库脚本下载链接:

https://files.cnblogs.com/dushouke/PCD_AllData_V1.rar

posted @ 2008-05-22 21:20  MicroCoder  阅读(8103)  评论(6编辑  收藏  举报