Dropdownlist多级实现
要求:实现dropdownlist的下拉框中包含所有分类。
数据库如下:
代码如下:(这里做成用户控件)
ascx文件:
代码
1 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UC_ddlCategroy.ascx.cs" Inherits="CartDemo.WebUI.UserControl.UC_ddlCategroy" %>
2
3 <asp:DropDownList ID="ddlCategory" runat="server" BackColor="White" Font-Bold="True" ForeColor="Green" AutoPostBack="true">
4 </asp:DropDownList>
2
3 <asp:DropDownList ID="ddlCategory" runat="server" BackColor="White" Font-Bold="True" ForeColor="Green" AutoPostBack="true">
4 </asp:DropDownList>
cs文件:
代码
1 public partial class UC_ddlCategroy : System.Web.UI.UserControl
2 {
3 /// <summary>
4 /// 初始化
5 /// </summary>
6 /// <param name="sender"></param>
7 /// <param name="e"></param>
8 protected void Page_Load(object sender, EventArgs e)
9 {
10 if (!IsPostBack)
11 {
12 DataTable datatable = this.GetDataTable();
13 if (datatable.Rows.Count > 0)
14 {
15 this.ddlCategory.Items.Add("全部分类");
16 foreach (DataRow row in datatable.Rows)
17 {
18 if (row["CategoryPid"].ToString().Trim() == "0")
19 {
20 this.ddlCategory.Items.Add(new ListItem(row["CategoryName"].ToString(), row["CategoryId"].ToString()));
21 addSonCategory(string.Empty, Convert.ToInt32(row["CategoryId"]), datatable, 1);
22 }
23 }
24 }
25 }
26 }
27
28 /// <summary>
29 /// 添加子节点
30 /// </summary>
31 /// <param name="strPading">空格</param>
32 /// <param name="DirId">父路径ID</param>
33 /// <param name="datatable">返回的datatable</param>
34 /// <param name="deep">树形的深度</param>
35 private void addSonCategory(string strPading, int id, DataTable datatable, int deep)
36 {
37 DataRow[] rowlist = datatable.Select("CategoryPid='" + id + "'");
38 foreach (DataRow row in rowlist)
39 {
40 strPading = string.Empty;
41 for (int j = 0; j < deep; j++)
42 {
43 strPading += " "; //用全角的空格(智能abc输入法状态下的v11字符)
44 }
45 // 添加节点
46 ListItem li = new ListItem(strPading + "|--" + row["CategoryName"].ToString());
47 this.ddlCategory.Items.Add(li);
48 // 递归调用addSonCategory函数,在函数中把deep加1
49 addSonCategory(strPading, Convert.ToInt32(row["CategoryId"]), datatable, deep + 1);
50 }
51 }
52
53 /// <summary>
54 /// 从数据库中读取数据返回datatable
55 /// </summary>
56 /// <returns></returns>
57 private DataTable GetDataTable()
58 {
59 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
60 SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM [T_Category]", conn);
61 DataSet datset = new DataSet();
62 conn.Open();
63 adp.Fill(datset, "T_Category");
64 return datset.Tables[0];
65 }
66
67 /// <summary>
68 /// SelectedValue属性(暴露给外部使用)
69 /// 只有Get方法,没有Set方法,保证只读
70 /// </summary>
71 public string SelectedValue
72 {
73 get
74 {
75 string strSelectedValue = this.ddlCategory.SelectedValue;
76
77 if (strSelectedValue.Contains(" ") && (strSelectedValue.Contains("|--")))
78 { // 如果是子类别
79 int iStartIndex = strSelectedValue.LastIndexOf("|--") + 3; // 字符串截取的起始位置
80 strSelectedValue = strSelectedValue.Substring(iStartIndex);
81 }
82
83 return strSelectedValue;
84 }
85 }
86 }
2 {
3 /// <summary>
4 /// 初始化
5 /// </summary>
6 /// <param name="sender"></param>
7 /// <param name="e"></param>
8 protected void Page_Load(object sender, EventArgs e)
9 {
10 if (!IsPostBack)
11 {
12 DataTable datatable = this.GetDataTable();
13 if (datatable.Rows.Count > 0)
14 {
15 this.ddlCategory.Items.Add("全部分类");
16 foreach (DataRow row in datatable.Rows)
17 {
18 if (row["CategoryPid"].ToString().Trim() == "0")
19 {
20 this.ddlCategory.Items.Add(new ListItem(row["CategoryName"].ToString(), row["CategoryId"].ToString()));
21 addSonCategory(string.Empty, Convert.ToInt32(row["CategoryId"]), datatable, 1);
22 }
23 }
24 }
25 }
26 }
27
28 /// <summary>
29 /// 添加子节点
30 /// </summary>
31 /// <param name="strPading">空格</param>
32 /// <param name="DirId">父路径ID</param>
33 /// <param name="datatable">返回的datatable</param>
34 /// <param name="deep">树形的深度</param>
35 private void addSonCategory(string strPading, int id, DataTable datatable, int deep)
36 {
37 DataRow[] rowlist = datatable.Select("CategoryPid='" + id + "'");
38 foreach (DataRow row in rowlist)
39 {
40 strPading = string.Empty;
41 for (int j = 0; j < deep; j++)
42 {
43 strPading += " "; //用全角的空格(智能abc输入法状态下的v11字符)
44 }
45 // 添加节点
46 ListItem li = new ListItem(strPading + "|--" + row["CategoryName"].ToString());
47 this.ddlCategory.Items.Add(li);
48 // 递归调用addSonCategory函数,在函数中把deep加1
49 addSonCategory(strPading, Convert.ToInt32(row["CategoryId"]), datatable, deep + 1);
50 }
51 }
52
53 /// <summary>
54 /// 从数据库中读取数据返回datatable
55 /// </summary>
56 /// <returns></returns>
57 private DataTable GetDataTable()
58 {
59 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
60 SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM [T_Category]", conn);
61 DataSet datset = new DataSet();
62 conn.Open();
63 adp.Fill(datset, "T_Category");
64 return datset.Tables[0];
65 }
66
67 /// <summary>
68 /// SelectedValue属性(暴露给外部使用)
69 /// 只有Get方法,没有Set方法,保证只读
70 /// </summary>
71 public string SelectedValue
72 {
73 get
74 {
75 string strSelectedValue = this.ddlCategory.SelectedValue;
76
77 if (strSelectedValue.Contains(" ") && (strSelectedValue.Contains("|--")))
78 { // 如果是子类别
79 int iStartIndex = strSelectedValue.LastIndexOf("|--") + 3; // 字符串截取的起始位置
80 strSelectedValue = strSelectedValue.Substring(iStartIndex);
81 }
82
83 return strSelectedValue;
84 }
85 }
86 }
别的aspx页面调用之:
1 <%@ Register Src="~/UserControl/UC_ddlCategroy.ascx" TagName="ddlCategory" TagPrefix="uc1" %>
1 <uc1:ddlCategory ID="ddlCategory" runat="server" />
显示效果: