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

ajax控件 CascadingDropDown用法

2010-12-02 20:26 309 查看
最近在csdn上经常看到很多初学者问如何实现无刷新三级联动,下面我们拿ajax中的CascadingDropDown控件来实现这个功能。

Cascading DropDown实现无刷新联动的原理是他调用Web Service实现异步加载。使用该控件有个需要注意的地方就是在websrvice方法中,方法的参数名称必须是固定的。

下面给大家列出部分代码:前台

<!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 id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<Form id="form1" runat="server">
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="provinceDr"
ServicePath="GetDLDetailInfo.asmx" ServiceMethod="GetProvince" Category="province"
PromptValue="0" ParentControlID="" LoadingText="加载省份.." PromptText="请选择省份">
</ajaxToolkit:CascadingDropDown>
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="cityDr"
ServicePath="GetDLDetailInfo.asmx" ServiceMethod="GetCityByProvince" Category="city"
ParentControlID="provinceDr" LoadingText="加载城市.." PromptText="请选择城市">
</ajaxToolkit:CascadingDropDown>
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="areaDr"
ServicePath="GetDLDetailInfo.asmx" ServiceMethod="GetAreaByCity" Category="area"
ParentControlID="cityDr" LoadingText="加载地区.." PromptText="请选择地区">
</ajaxToolkit:CascadingDropDown>
<div id="map_diqu">
地区选择:<asp:DropDownList ID="provinceDr" runat="server" Width="128px">
</asp:DropDownList>
<asp:DropDownList ID="cityDr" runat="server" Width="160px">
</asp:DropDownList>
<asp:DropDownList ID="areaDr" runat="server" Width="151px">
</asp:DropDownList>
详细地址:<asp:TextBox ID="TextBox2" runat="server" Width="120px"></asp:TextBox>
店铺名称:<asp:TextBox ID="TextBox1" runat="server" Width="136px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="搜索" Width="41px" OnClick="Button1_Click" />
</div>
</Form>
</body>
</html>


webservice代码:

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Collections.Generic;
using System.Collections.Specialized;
using DataAccess;
using AjaxControlToolkit;
using BLL;

/// <summary>
/// GetDLDetailInfo 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class GetDLDetailInfo : System.Web.Services.WebService
{
private DataTable _myTable;
private province _myProvinceOper = new province();
private List<CascadingDropDownNameValue> _values;

public GetDLDetailInfo()
{

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

[WebMethod]
public CascadingDropDownNameValue[] GetProvince(string knownCategoryValues, string category)
{
_myTable = _myProvinceOper.SelectAllProvince();
_values = new List<CascadingDropDownNameValue>();

foreach (DataRow _r in _myTable.Rows)
{
_values.Add(new CascadingDropDownNameValue(_r["province"].ToString(), _r["provinceID"].ToString()));
}
return _values.ToArray();
}

[WebMethod]
public CascadingDropDownNameValue[] GetCityByProvince(string knownCategoryValues, string category)
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

int _provinceID;
if (!kv.ContainsKey("province") || !Int32.TryParse(kv["province"], out _provinceID))
{
return null;
}

_myTable = _myProvinceOper.SelectAllCity(_provinceID);
_values = new List<CascadingDropDownNameValue>();

foreach (DataRow _r in _myTable.Rows)
{
_values.Add(new CascadingDropDownNameValue(_r["city"].ToString(), _r["cityID"].ToString()));
}
return _values.ToArray();
}

[WebMethod]
public CascadingDropDownNameValue[] GetAreaByCity(string knownCategoryValues, string category)
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
int _cityID;
if (!kv.ContainsKey("city") || !Int32.TryParse(kv["city"], out _cityID))
{
return null;
}
_myTable = _myProvinceOper.SelectAllArea(_cityID);
_values = new List<CascadingDropDownNameValue>();

foreach (DataRow _r in _myTable.Rows)
{
_values.Add(new CascadingDropDownNameValue(_r["area"].ToString(), _r["areaID"].ToString()));
}
return _values.ToArray();
}
}


强调下,上面的webservice方法中的参数名称不能变哦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: