码迷,mamicode.com
首页 > 其他好文 > 详细

三级联动

时间:2015-03-20 09:10:27      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

public void getProdata() //第一级
{
List<ChinaStates> list = new List<ChinaStates>();

var query = from r in DataC.ChinaStates where r.ParentAreaCode == "0001" select r;
list = query.ToList();
foreach (ChinaStates cdata in list)
{
ListItem lt = new ListItem(cdata.AreaName, cdata.AreaCode);
DropDownList1.Items.Add(lt);
}

}


public void getcitydata(string code) //第二级
{
List<ChinaStates> list = new List<ChinaStates>();

var query = from r in DataC.ChinaStates where r.ParentAreaCode == code select r;
list = query.ToList();
DropDownList2.Items.Clear();
foreach (ChinaStates cdata in list)
{
ListItem lt = new ListItem(cdata.AreaName,cdata.AreaCode);
DropDownList2.Items.Add(lt);
}

}

public void getquxiandata(string code) //第三级
{
List<ChinaStates> list = new List<ChinaStates>();
var query = from r in DataC.ChinaStates where r.ParentAreaCode == code select r;
list = query.ToList();
DropDownList3.Items.Clear();
foreach (ChinaStates cdata in list)
{
ListItem lt = new ListItem(cdata.AreaName, cdata.AreaCode);
DropDownList3.Items.Add(lt);
}

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
getcitydata(DropDownList1.SelectedValue);
UpdatePanel1.Update();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
getquxiandata(DropDownList2.SelectedValue);
UpdatePanel1.Update();
}

 

 


方法二:

DataClassesDataContext context = new DataClassesDataContext();
protected void Page_Load(object sender, EventArgs e)
{
//IsPostBack当前请求是否第一次打开,只有在第一次打开的时候是false,其它时候都是true
//在每个Load页面写代码之前,都加上这句话
getProdata();
getcitydata();
getquxiandata();
}
///// <summary>
///// 三级联动
///// </summary>
public void getProdata()
{
var query = context.Members.Where(p => p.ParentId == 0);
DropDownList1.DataSource = query;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
public void getcitydata()
{
string name1 = DropDownList1.SelectedValue;

var query = context.Members.Where(p => p.ParentId == int.Parse(name1));

DropDownList2.DataSource = query;
DropDownList2.DataTextField = "Name";
DropDownList2.DataValueField = "id";
DropDownList2.DataBind();
}

public void getquxiandata()
{
string name2 = DropDownList2.SelectedValue;

var query = context.Members.Where(p => p.ParentId == int.Parse(name2));

DropDownList3.DataSource = query;
DropDownList3.DataTextField = "Name";
DropDownList3.DataValueField = "id";
DropDownList3.DataBind();
}


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
getcitydata();
getquxiandata();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
getquxiandata();
}

三级联动

标签:

原文地址:http://www.cnblogs.com/mxx0426/p/4352750.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!