DropDownList联动

前台代码:
<asp:DropDownList ID="DropDownList1" runat="server" Style="width: 200px; height: 30px" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" Style="width: 200px; height: 30px" AutoPostBack="true">
</asp:DropDownList>

后台代码:
public void Bindate()
{
DropDownList1.DataSource = comBLL.GetModelList("state=1");
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
public void BindTo()
{
var selected = DropDownList1.SelectedValue;
List<Model.Person.pmDepartment> ds = opmDepartmentBLL.GetModelList(" state=1 and CompanyId=" + selected);
DropDownList2.DataSource = ds;
if (ds == null || ds.Count < 0)
{
DropDownList2.Items.Clear();
}
else
{
DropDownList2.DataTextField = "Name";
DropDownList2.DataValueField = "id";
DropDownList2.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var selected = DropDownList1.SelectedValue;
if (string.IsNullOrEmpty(selected))
{
DropDownList2.Items.Clear();
}
else
{
BindTo();
}
}

页面加载:
protected void Page_Load(object sender, EventArgs e)
{
if (!LoadQueryString())
return;

if (!IsPostBack)
{
//调用方法
Bindate();
BindTo();
DropDownList1.SelectedValue = opmUserInfo.CompanyId.ToString();
DropDownList2.SelectedValue = opmUserInfo.DepartmentId.ToString();
}
}

posted @ 2017-09-11 16:36  没有狐狸的南墙  阅读(68)  评论(0编辑  收藏  举报