百度空间 | 百度首页 
               
 
查看文章
 
CascadingDropDown:实现联动下拉框
2007-07-05 09:56
我们简单的模拟上面功能,
         <cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
         Category="Province"       //唯一性
PromptText="请选择省份..."
         LoadingText="省份加载中..."
         ServicePath="Country.asmx"
          ServiceMethod="GetProvince"  
      ParentControlID="DropDownList1"
         >
         </cc1:CascadingDropDown>
         <cc1:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3"
         Category="City" PromptText="请选
择城市..." LoadingText="城市加载中..."
         ServicePath="Country.asmx" ServiceMethod="GetCity"   ParentControlID="DropDownList2">
         </cc1:CascadingDropDown>
         <cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1"
         Category="Country" PromptText="请选择国家..." LoadingText="国家加载中..."
         ServicePath="Country.asmx" ServiceMethod="GetCountry"
         >
         </cc1:CascadingDropDown>
下面是我们的服务方法
using AjaxControlToolkit;//需要我们添加
using System.Collections.Generic;
using System.Collections.Specialized;

/// <summary>
/// Country 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class Country : System.Web.Services.WebService
{
     [WebMethod]
     public CascadingDropDownNameValue[] GetCountry(string knownCategoryValues, string category)
//不能修改返回值,形参,唯一可以修改的是函数名,否则,就不能引用
     {
         //如果使用设计的组件,请取消注释以下行
         //InitializeComponent();
         List<CascadingDropDownNameValue> CountryList = new List<CascadingDropDownNameValue>();
         CountryList.Add(new CascadingDropDownNameValue("中国","china"));
         CountryList.Add(new CascadingDropDownNameValue("其他","Other"));
         return CountryList.ToArray();
     }
     [WebMethod]
     public CascadingDropDownNameValue[] GetProvince(string knownCategoryValues, string category)
     {
         StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
         if(kv["Country"]!="china")
       //就是前面的category值
             return null;
         List<CascadingDropDownNameValue> ProvList = new List<CascadingDropDownNameValue>();
         ProvList.Add(new CascadingDropDownNameValue("安徽", "ah"));
         ProvList.Add(new CascadingDropDownNameValue("北京", "bh"));
         return ProvList.ToArray();
     }
     [WebMethod]
     public CascadingDropDownNameValue[] GetCity(string knownCategoryValues, string category)
     {
         StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
         if (kv["Province"] != "ah")
             return null;
         List<CascadingDropDownNameValue> CityList = new List<CascadingDropDownNameValue>();
         CityList.Add(new CascadingDropDownNameValue("安庆", "aq"));
         CityList.Add(new CascadingDropDownNameValue("合肥", "bh"));
         return CityList.ToArray();
     }
     [WebMethod]
     public string HelloWorld()
     {
         return "Hello World";
     }
}
可能遇到问题:返回异常:
修改方法EnableEventValidation="false"
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ThreeDropDownList.aspx.cs" Inherits="ThreeDropDownList" EnableEventValidation="false"   %>

类别:Ajax | 添加到搜藏 | 浏览() | 评论 (4)
 
最近读者:
 
网友评论:
1
2008-08-18 22:41 | 回复
按照示例实现了,在IE6里能成功实现无刷新联动,但在IE7里却不能,不知是为什么?
 
2
2008-08-19 20:20 | 回复
那我就不知道了,你用VS2008试试,微软可能存在这种情况
 
5
2009-02-18 17:01 | 回复
我也试了~~不行!!!
 
6
2009-02-19 10:28 | 回复
但在IE7里却不能,的确是这样
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu