AjaxControlToolkit 加 webservice 实现DropDownList来联动

下载地址:http://download.csdn.net/source/330798
Tool.asmx

<%@ WebService Language="C#" CodeBehind="~/App_Code/Tool.cs" Class="Tool" %>
Tool.cs
using  System;
using  System.Web;
using  System.Collections;
using  System.Web.Services;
using  System.Web.Services.Protocols;
using  System.Collections.Generic;
using  AjaxControlToolkit;
using  System.Data;
/// <summary>
/// Tool 的摘要说明

/// </summary>

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

    
public Tool()
    
{

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


    [WebMethod]
    
public string HelloWorld()
    
{
        
return "Hello World";
    }

    [WebMethod]
    
public CascadingDropDownNameValue[] GetProvices(string knownCategoryValues, string category)
    
{
        List
<CascadingDropDownNameValue> Provinces = new List<CascadingDropDownNameValue>();
        
        DBClass db 
= new DBClass();
        DataTable dt 
= db.DBClass_getDataTable("select * from Province");
        
if (dt != null && dt.Rows.Count > 0)
        
{
            
for (int i = 0; i < dt.Rows.Count; i++)
            
{
                CascadingDropDownNameValue Province 
= new CascadingDropDownNameValue();
                Province.name 
= dt.Rows[i]["name"].ToString();
                Province.value 
= dt.Rows[i]["code"].ToString();
                Provinces.Add(Province);
            }

        }

        
/*从数据库中加载所有省份*/
        
/*  下面是事例 */
        
//Provinces.Add(new CascadingDropDownNameValue("北京", "beijing"));
        
//Provinces.Add(new CascadingDropDownNameValue("江苏", "jiangsu"));
        
//Provinces.Add(new CascadingDropDownNameValue("上海", "shanghai"));
        return Provinces.ToArray();
    }

    [WebMethod]
    
public CascadingDropDownNameValue[] GetCities(string knownCategoryValues, string category)
    
{//"Province:110000;"
        List<CascadingDropDownNameValue> Citys = new List<CascadingDropDownNameValue>();
        
        DBClass db 
= new DBClass();
        
string ProvinceId= (knownCategoryValues.Substring(knownCategoryValues.IndexOf(':')+1)).Replace(";","");
        DataTable dt 
= db.DBClass_getDataTable("select * from City where provinceId=" + ProvinceId);
        
if (dt != null && dt.Rows.Count > 0)
        
{
            
for (int i = 0; i < dt.Rows.Count; i++)
            
{
                CascadingDropDownNameValue city 
= new CascadingDropDownNameValue();
                city.name 
= dt.Rows[i]["name"].ToString();
                city.value 
= dt.Rows[i]["code"].ToString();
                Citys.Add(city);
            }

        }

        
/*从数据库中加载所有省份*/
        
/*  下面是事例 */

        
//Provinces.Add(new CascadingDropDownNameValue("南京", "beijing"));
        
//Provinces.Add(new CascadingDropDownNameValue("徐州", "jiangsu"));
        
//Provinces.Add(new CascadingDropDownNameValue("扬州", "shanghai"));
        
//Provinces.Add(new CascadingDropDownNameValue("常州", "shanghai"));
        return Citys.ToArray();
    }


 
    
}



 Visitor_Register.aspx
<% @ Page Language="C#" AutoEventWireup="true" CodeFile="Visitor_Register.aspx.cs" Inherits="page_P_apply_Apply_WebMarket_DSN_DNS_User_Register"   %>

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

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  id ="Head1"  runat ="server" >      
< title > 用户注册 </ title >   

 



</ head >                      
< body  text ="#00000" >
    
< form  id ="form1"  runat ="server" >  
 
< asp:DropDownList  ID ="DDL_Province"  runat ="server"  CssClass ="textarea_nowide"  Width ="200px" >
                        
< asp:ListItem  Value ="1" > 2 </ asp:ListItem >
                        
< asp:ListItem  Value ="2" > 3 </ asp:ListItem >
                    
</ asp:DropDownList > &nbsp; < span  style ="color: #ff0000" >   </ span >
                    
< asp:RequiredFieldValidator  ID ="RequiredFieldValidator2"  runat ="server"  ErrorMessage ="必选"  ControlToValidate ="DDL_Province"  Display ="Dynamic" ></ asp:RequiredFieldValidator >
                    
< cc1:CascadingDropDown  ID ="CascadingDropDown1"  runat ="server"  TargetControlID ="DDL_Province"
                    Category
="Province"                     
                    LoadingText
="正在加载省份..."
                    PromptText 
="请选择省份..."
                    ServicePath
="~/CusService/Tool.asmx"
                    ServiceMethod
="GetProvices"
 
                    
>
                    
</ cc1:CascadingDropDown >



< asp:DropDownList  ID ="DDL_Cities"  runat ="server"  CssClass ="textarea_nowide"  Width ="200px" >
                    
</ asp:DropDownList >< span  class ="Cred" >
                        
< asp:RequiredFieldValidator  ID ="RequiredFieldValidator3"  runat ="server"  ErrorMessage ="必选"  ControlToValidate ="DDL_Cities"  Display ="Dynamic" ></ asp:RequiredFieldValidator >
                    
< cc1:CascadingDropDown  ID ="CascadingDropDown2"
                        runat
="server"  TargetControlID ="DDL_Cities"
                        Category
="City"
                        LoadingText
="正在加载城市..."
                        PromptText 
="请选择城市..."
                        ServicePath
="~/CusService/Tool.asmx"
                        ServiceMethod
="GetCities"
                        ParentControlID
="DDL_Province"                           
                        
>
                    
</ cc1:CascadingDropDown >



        

    
</ form >     
</ body >
</ html >

你可能感兴趣的:(webservice,server,String,list,Class,asp)