
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
public partial class ProvinceCity : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RegisterAjax();
//一开始绑定省
ddlID_Province.DataSource = GetChild(0);
ddlID_Province.DataValueField = "key";
ddlID_Province.DataTextField = "value";
ddlID_Province.DataBind();
ddlID_Province.Items.Insert(0, "请选择");
}
}
public override void RaiseCallbackEvent(string eventArgument)
{
if (eventArgument.Contains(","))
{
string parentID = eventArgument.Split(',')[0];
//根据ID查数据库
Dictionary<int, string> items = GetChild(int.Parse(parentID));
if (items.Count > 0)
{
foreach (KeyValuePair<int, string> itemPair in items)
{
ajaxCallBackResult += itemPair.Key + "," + itemPair.Value + "|";
}
ajaxCallBackResult = ajaxCallBackResult.TrimEnd('|');
}
ajaxCallBackResult += "&" + eventArgument.Split(',')[1];//附加回去下拉列表控件ID
}
}
#region 假设这里为数据库查询
protected Dictionary<int, string> GetChild(int parentID)
{
Dictionary<int, string> items = new Dictionary<int, string>();
switch (parentID)
{
case 0:
items.Add(1, "广东省");
items.Add(2, "广西省");
break;
case 1:
items.Add(11, "汕尾市");
items.Add(12, "广州市");
break;
case 2:
items.Add(21, "桂林市");
items.Add(22, "南宁市");
break;
case 11:
items.Add(111, "海丰县");
break;
case 12:
items.Add(121, "天河区");
break;
case 111:
items.Add(1111, "城东");
items.Add(1112, "城西");
break;
}
return items;
}
#endregion
}
