前台代码:
<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();
}
}