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();
}