您的位置:首页 > 其它

用回调实现的省市县区四级下拉选择联动的例子

2006-08-17 15:01 537 查看
本文示例代码(52K),请在VS2005中用"打开网站"的方式打开并运行
function OnProvinceChanged()

// 接收回调后服务器传回的结果,这里是DropDownList_Provinces控件重新绑定后的HTML编码
function UpdataDropDownList_Cities(result, context)
using System.Web.UI;
using System.IO;
using System.Globalization;
服务器端对下拉控件的更新如下,相对正常的绑定只做了很小的更改
// 更新地级市
public string UpdateCities(string str_ProvinceID)

// 根据上级编号更新指定的DropDownList
private string UpdateDropDownList(string str_parent_id, DropDownList theDropDownList)
string str_SQL = "Select area_id, area_name From Cities Where area_parent_id = '" + str_parent_id + "' Order By area_id Asc";

DataTable theDataTable = GetDataTable(str_SQL);

if (theDataTable == null)
return string.Empty;

// 数据绑定
theDropDownList.DataSource = theDataTable;
theDropDownList.DataTextField = "area_name";
theDropDownList.DataValueField = "area_id";
theDropDownList.DataBind();

// 返回的是Javascript便于处理的string
if (str_parent_id == "000000")
// 省级列表不需要联动
return string.Empty;
else
return RenderControl(theDropDownList);
}
4个下拉列表的联动需要涉及到3次回调过程,限于微软在回调JS端的代码BUG,这3次回调只能用串联进行,中间用setTimeout()连接,否则会有Javascript报错,这也算是回调的一大缺陷吧。
Access中表Ctities的组织如下,area_parent_id顾名思义是该地区上级地区的编码,省级的上级是000000,这里我收集了3523条记录,基本涵盖了大江南北



更细节的地方还是在示例代码中找答案吧。

回调说到底还是微软对xmlhttp对象操作的封装,和其它Ajax实现大同小异,微软制定了固定的流程套路来降低Ajax的开发门槛,不用额外第三方DLL的支持是其最大的亮点,因此可以稳定在各支持ASP2.0的Server上运行,当然,回调并不是ASP2.0独有的,在ASP1.1时就已存在。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: