62,046
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>
后台
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = GetList().Where(d => d.pid == 0);
DropDownList1.DataValueField = "id";
DropDownList1.DataTextField = "name";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var result = GetList().Where(d => d.pid == int.Parse(DropDownList1.SelectedValue)).ToList();
DropDownList2.DataSource = result;
DropDownList2.DataValueField = "id";
DropDownList2.DataTextField = "name";
DropDownList2.DataBind();
}
private List<Department> GetList()
{
List<Department> list = new List<Department>();
list.AddRange(new[] {
new Department() { id = 1, pid = 0, name = "人事局" },
new Department() { id = 2, pid = 1, name = "局长" },
new Department() { id = 3, pid = 1, name = "副局长" },
new Department() { id = 4, pid = 0, name = "人事处" },
new Department() { id = 5, pid = 4, name = "处长" },
new Department() { id = 6, pid = 4, name = "副处长" }
});
return list;
}
}
public class Department
{
public int id { get; set; }
public int pid { get; set; }
public string name { get; set; }
}
}