asp.net 省市区级联
把代码封装到用户控件里面
把 <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>放到模板页中,这样可以在一个页面中重复使用
//根据省的id查询对应的城市
private void BindCity()
{
int provinceId = Convert.ToInt32(this.ddlProvince.SelectedValue);
this.ddlCity.DataSource = new T_CityManager().GetAllT_CitiesByProviceId(provinceId);
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "T_CityId";
this.ddlCity.DataBind();
}
private void BindProvince()
{
this.ddlProvince.DataSource = new T_ProvinceManager().GetAllT_Provinces();
this.ddlProvince.DataTextField = "ProName";
this.ddlProvince.DataValueField = "T_ProvinceId";
this.ddlProvince.DataBind();
}
//根据cityId查询区或县
private void BindDistrict()
{
int CityId = Convert.ToInt32(this.ddlCity.SelectedValue);
this.ddlDistrict.DataSource = new T_DistrictManager().GetT_DistrictsByCityId(CityId);
this.ddlDistrict.DataTextField = "DisName";
this.ddlDistrict.DataValueField = "T_DistrictId";
this.ddlDistrict.DataBind();
}
protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
{
BindCity();
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
BindDistrict();
}
前台调用的时候可以通过找控件的方式获取用户控件的值
DropDownList ddlProvince = (DropDownList)AdressControl2.FindControl("ddlProvince");
string provinceId = ddlProvince.SelectedValue;