droplist下拉层级选择

View Code
 1  public partial class WebForm1 : System.Web.UI.Page

 2     {

 3         protected void Page_Load(object sender, EventArgs e)

 4         {

 5             if (!IsPostBack)

 6             {

 7                 BindDrpClass();

 8             }

 9         }

10         private void BindDrpClass()

11         {

12             //获得所有数据列表

13             Maticsoft.BLL.L_Men sm = new Maticsoft.BLL.L_Men();

14             DataSet ds;

15             ds = sm.GetList("");

16             DataTable dt = ds.Tables[0];

17             ddl.Items.Clear();

18             ddl.Items.Add(new ListItem("选择类别", "0"));

19             DataRow[] drs = dt.Select("MenParentID= " + 0);//得到父ID

20 

21             foreach (DataRow dr in drs)

22             {

23                 string classid = dr["MenID"].ToString();

24                 string classname = dr["MenName"].ToString();

25                 //顶级分类显示形式

26                 classname = "" + classname;

27 

28                 ddl.Items.Add(new ListItem(classname, classid));

29                 int sonparentid = int.Parse(classid);

30                 string blank = " |---";

31                 //递归子分类方法

32                 BindNode(sonparentid, dt, blank);

33             }

34             ddl.DataBind();

35         }

36         private void BindNode(int parentid, DataTable dt, string blank)

37         {

38             DataRow[] drs = dt.Select("MenParentID= " + parentid);

39 

40             foreach (DataRow dr in drs)

41             {

42                 string classid = dr["MenID"].ToString();

43                 string classname = dr["MenName"].ToString();

44 

45                 classname = blank + classname;

46                 ddl.Items.Add(new ListItem(classname, classid));

47 

48                 int sonparentid = int.Parse(classid);

49                 string blank2 = blank + "-";

50                 BindNode(sonparentid, dt, blank2);

51             }

52         }

53 

54         protected void ddl_SelectedIndexChanged(object sender, EventArgs e)

55         {

56             Response.Write("选择值是:"+ddl.SelectedValue);

57         }

58 

59         protected void Button1_Click(object sender, EventArgs e)

60         {

61 

62         }

63       

64     }

droplist下拉层级选择

你可能感兴趣的:(plist)