简单三级联动的建立
嘿嘿,周六周日网络没啦,甚是纠结啊,终于在不懈的努力下重新拥有了网络,激动哦,于是今天在这里又开始了我的新的一个月的博客旅历,其实上周是要总结三级联动的,但是没有太多的时间总结于是就到了今天,简单的总结一下吧,刚开始在winform中其实已经总结过了三级联动,但是在这里我们使用了ajax技术异步传输,就不需要更新页面,下面就来简单的总结一下吧。
上面是前台类的实现代码,在这里我们看到使用的是jquery和ajax技术,实现我们通过post获取value值来给下一级传一个id,通过id来获取选取的name值和id值。下面就来看下后台类和bll层的代码和dal代码。
public class Handler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string province = context.Request.Form["province"]; //在前台类获取 string cityid = context.Request.Form["city"]; if (context.Request.HttpMethod == "POST") { //这些东西应拿到if判断里面创建 ProvinceBll pbll = new ProvinceBll(); CityBll cbll = new CityBll(); AreaBll abll = new AreaBll(); StringBuilder builder = new StringBuilder(); //province如果为空,则为页面加载,将所有的省份取出返回页面 //,否则取出省份下的所有市 if (string.IsNullOrEmpty(province)) { foreach (var item in pbll.GetAllProvince()) { //拼接option标签 builder.Append("<option value=" + item.provinceID + ">" + item.provincename + "</option>"); } context.Response.Write(builder.ToString()); } else { //如果城市id为空,则为触发省的change事件的请求 if (string.IsNullOrEmpty(cityid)) { //此时省份id不为空,触发了change事件,然后根据省份的id,找出所有的父id为province的city foreach (var item in cbll.GetAllCityByProid(Convert.ToInt32(province))) { //拼接option标签 builder.Append("<option value=" + item.cityID + ">" + item.cityname + "</option>"); } context.Response.Write(builder.ToString()); } else { //如果城市id不为空,则为触发城市的change事件的请求,然后根据cityid,查找area表中所有父id为cityid的区信息 foreach (var item in abll.GetAllAreaByCityid(Convert.ToInt32(cityid))) { //拼接option标签 builder.Append("<option value=" + item.id + ">" + item.areaname + "</option>"); context.Response.Write(builder.ToString()); } } } } }
public class ProvinceDal { /// <summary> /// 查询所有的省份 /// </summary> /// <returns>reader类型</returns> public List<ProvinceModel> GetAllProvince() { string sql = "select * from province"; SqlDataReader reader = SqlHelper.ExecuteReader(sql); List<ProvinceModel> list = new List<ProvinceModel>(); while (reader.Read()) { ProvinceModel model = new ProvinceModel(); model.id =Convert.ToInt32(reader[0]); model.provinceID = reader[1].ToString(); model.provincename = reader[2].ToString(); list.Add(model); } return list; } }
public class CityDal { CityModel model = new CityModel(); /// <summary> /// 通过上一级的省份获取其proid查询所有的市区 /// </summary> /// <param name="proid">省份id</param> /// <returns>reader类型</returns> public List<CityModel> GetAllCityByProid(int proid) { string sql = "select * from city where father = @proid"; SqlParameter parms = new SqlParameter("@proid",proid); SqlDataReader reader = SqlHelper.ExecuteReader(sql,parms); List<CityModel> list = new List<CityModel>(); while (reader.Read()) { CityModel model = new CityModel(); model.id = Convert.ToInt32(reader[0]); model.cityID = reader[1].ToString(); model.cityname = reader[2].ToString(); model.father = reader[3].ToString(); list.Add(model); } return list; } }
public class AreaDal { /// <summary> /// 通过城市的id来查询县区 /// </summary> /// <param name="cityid">城市id</param> /// <returns>reader类型</returns>
public List<AreaModel> GetAllAreaByCityid(int cityid)
{
string sql = "select * from area where fathor=@cityid";
SqlParameter parms = new SqlParameter("cityid",cityid);
SqlDataReader reader = SqlHelper.ExecuteReader(sql, parms);
List<AreaModel> list = new List<AreaModel>();
while (reader.Read()) {
AreaModel model = new AreaModel();
model.id =Convert.ToInt32(reader[0]);
model.areaID = reader[1].ToString();
model.areaname = reader[2].ToString();
model.father = reader[3].ToString();
list.Add(model); } return list;
}
}
就仅仅写了ui层的类和dal层的代码,但是我感觉通过这样的写法下次在彭代三级联动时间我会很简单的就写出来啦,写这个时间还是遇到啦好多的问题,或许有的是粗心忘记写上去啦,也或许是真的不清楚思路,还有谢谢友友的辛苦劳动,嘿嘿,总之,写完这个其实并不顺利的,但是我相信我不会仅仅就写着一边的,我会继续练习的,嘿嘿,继续努力的,加油加油!!!
我是小白,新建立了一个的群:461431726,希望在这里和大家一起交流,共同学习。前端的话建议加群:646564351,谢谢