您的位置:首页 > 运维架构

DropDownList 省市区无刷新三级联动

2009-09-09 14:20 363 查看
Default.aspx页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" 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>51aspx省市区县(含完整数据库)无刷新联动</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlProvince" runat="server">
</asp:DropDownList><asp:DropDownList ID="ddlCity" runat="server">
</asp:DropDownList><asp:DropDownList ID="ddlVilliage" runat="server">
</asp:DropDownList><ajax:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="ddlProvince" ServicePath="SNWebService.asmx" ServiceMethod="GetProvinceContents" Category="Province" PromptText="请选择省份" LoadingText="省份加载中...">
</ajax:CascadingDropDown>
<ajax:CascadingDropDown ID="CascadingDropDown1" runat="server" ParentControlID="ddlProvince" ServicePath="SNWebService.asmx" ServiceMethod="GetCityContents" Category="City" TargetControlID="ddlCity" PromptText="请选择城市" LoadingText="城市加载中...">
</ajax:CascadingDropDown>
<ajax:CascadingDropDown ID="CascadingDropDown3" runat="server" Category="Villiage" LoadingText="区县加载中..."
ParentControlID="ddlCity" PromptText="请选择区县" ServiceMethod="GetViliageContents"
ServicePath="SNWebService.asmx" TargetControlID="ddlVilliage">
</ajax:CascadingDropDown>
</ContentTemplate>
</asp:UpdatePanel>
<br />
</div>
</form>
</body>
</html>


SNWebService.cs(WebService类文件代码)

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Collections.Specialized;
using AjaxControlToolkit;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// SNWebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class SNWebService : System.Web.Services.WebService
{

public SNWebService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
/// <summary>
/// 获取省份数据
/// </summary>
/// <param name="knownCategoryValues"></param>
/// <param name="category"></param>
/// <returns></returns>
[WebMethod]
public CascadingDropDownNameValue[] GetProvinceContents(string knownCategoryValues, string category)
{
List<CascadingDropDownNameValue> provinceList = new List<CascadingDropDownNameValue>();

string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];

SqlConnection sqlConn = new SqlConnection(connectionString);

sqlConn.Open();

string strSql = "Select * From Province";

SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);

SqlDataReader dtrProvince = sqlCmd.ExecuteReader();

while (dtrProvince.Read())
{
provinceList.Add(new CascadingDropDownNameValue(dtrProvince["Name"].ToString(),dtrProvince["Code"].ToString()));
}

dtrProvince.Close();

sqlConn.Close();

return provinceList.ToArray();
}
/// <summary>
/// 获取市数据
/// </summary>
/// <param name="knownCategoryValues"></param>
/// <param name="category"></param>
/// <returns></returns>
[WebMethod]
public CascadingDropDownNameValue[] GetCityContents(string knownCategoryValues, string category)
{
StringDictionary provinceList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];

SqlConnection sqlConn = new SqlConnection(connectionString);

sqlConn.Open();

string strSql = "Select * From city Where ProvinceId='" + provinceList["Province"] + "'";

SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);

SqlDataReader dtrCity = sqlCmd.ExecuteReader();

List<CascadingDropDownNameValue> cityList = new List<CascadingDropDownNameValue>();

while (dtrCity.Read())
{
cityList.Add(new CascadingDropDownNameValue(dtrCity["Name"].ToString(), dtrCity["code"].ToString()));
}

dtrCity.Close();

return cityList.ToArray();
}
/// <summary>
/// 获取乡镇数据
/// </summary>
/// <param name="knownCategoryValues"></param>
/// <param name="category"></param>
/// <returns></returns>
[WebMethod]
public CascadingDropDownNameValue[] GetViliageContents(string knownCategoryValues, string category)
{
StringDictionary cityList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];

SqlConnection sqlConn = new SqlConnection(connectionString);

sqlConn.Open();

string strSql = "Select * From area Where CityId='" + cityList["City"] + "'";

SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);

SqlDataReader dtrViliage = sqlCmd.ExecuteReader();

List<CascadingDropDownNameValue> viliageList = new List<CascadingDropDownNameValue>();

while (dtrViliage.Read())
{
viliageList.Add(new CascadingDropDownNameValue(dtrViliage["Name"].ToString(), dtrViliage["id"].ToString()));
}

dtrViliage.Close();

return viliageList.ToArray();
}

}


Web.Cofig

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<appSettings>
<add key="ConnectionString" value="Data Source=(local);Initial Catalog=my51aspx;User ID=sa;Password=sa;"/>
</appSettings>
<system.web>
<authentication mode="Forms">
<forms name=".AspNetSafetyForums" protection="Encryption" timeout="60" loginUrl="~/UserLogin.aspx"/>
</authentication>
<machineKey validationKey="C50B3C89CB21F4F1422FF158A5B42D0E8DB8CB5CDA1742572A487D9401E3400267682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE" decryptionKey="8A9BE8FD67AF6979E7D20198CFEA50DD3D3799C77AF2B72F" validation="SHA1">
</machineKey>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add tagPrefix="webdiyer" namespace="Wuqi.Webdiyer" assembly="AspNetPager"/>
<add tagPrefix="web" namespace="WebChart" assembly="WebChart"/>
<add tagPrefix="web" namespace="EeekSoft.Web" assembly="EeekSoft.Web.PopupWin"/>
<add tagPrefix="ajax" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/>
</controls>
</pages>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true">
<assemblies>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="Microsoft.ReportViewer.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
</assemblies>
<buildProviders>
<add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</buildProviders>
</compilation>
<httpHandlers>
<remove path="*.asmx" verb="*" />
<add path="*.asmx" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
validate="false" />
<add path="*_AppService.axd" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
validate="false" />
<add path="ScriptResource.axd" verb="GET,HEAD" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
validate="false" />
<add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
validate="false" />
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</handlers>
</system.webServer>
</configuration>


bin目录下引用

AjaxControlToolkit.dll

System.Web.Extensions.Design.dll

System.Web.Extensions.dll

需要数据库的跟下

转自:51aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: