后台代码:
////// 绑定下拉菜单 /// private void BindDropList() { DataTable dt = bacManage.GetAllArticleCategory(); CreateLevelDropDown(drpCategoryId, dt); } ////// 创建分级下拉框 /// /// /// private void CreateLevelDropDown(DropDownList ddlst, DataTable dt) { System.Collections.ArrayList allItems = new ArrayList(); DataRow[] rows = dt.Select("[parent]=" + 0); foreach (DataRow row in rows) CreateLevelDropDownAssistant(dt, ref allItems, row, string.Empty); ListItem[] items = new ListItem[allItems.Count]; allItems.CopyTo(items); ddlst.Items.AddRange(items); } ////// 设置下拉列表分级格式 /// /// /// /// /// private void CreateLevelDropDownAssistant(DataTable dt, ref ArrayList items, DataRow parentRow, string curHeader) { ListItem newItem = new ListItem(curHeader + parentRow["name"].ToString(), parentRow["category_id"].ToString()); items.Add(newItem); parentRow.Delete(); DataRow[] rows = dt.Select("[parent]='" + newItem.Value + "'"); for (int i = 0; i < rows.Length - 1; i++) CreateLevelDropDownAssistant(dt, ref items, rows[i], curHeader.Replace("┣", "|").Replace("┗", "┣") + "┣"); if (rows.Length > 0) CreateLevelDropDownAssistant(dt, ref items, rows[rows.Length - 1], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┗"); }
/// <summary>
/// 绑定下拉菜单 /// </summary> private void BindDropList() { DataTable dt = bacManage.GetAllArticleCategory(); CreateLevelDropDown(drpCategoryId, dt); } /// <summary> /// 创建分级下拉框 /// </summary> /// <param name="ddlst"></param> /// <param name="dt"></param> private void CreateLevelDropDown(DropDownList ddlst, DataTable dt) { System.Collections.ArrayList allItems = new ArrayList(); DataRow[] rows = dt.Select("[parent]=" + 0); foreach (DataRow row in rows) CreateLevelDropDownAssistant(dt, ref allItems, row, string.Empty); ListItem[] items = new ListItem[allItems.Count]; allItems.CopyTo(items); ddlst.Items.AddRange(items); } /// <summary> /// 设置下拉列表分级格式 /// </summary> /// <param name="dt"></param> /// <param name="items"></param> /// <param name="parentRow"></param> /// <param name="curHeader"></param> private void CreateLevelDropDownAssistant(DataTable dt, ref ArrayList items, DataRow parentRow, string curHeader) { ListItem newItem = new ListItem(curHeader + parentRow["name"].ToString(), parentRow["category_id"].ToString()); items.Add(newItem); parentRow.Delete(); DataRow[] rows = dt.Select("[parent]='" + newItem.Value + "'"); for (int i = 0; i < rows.Length - 1; i++) CreateLevelDropDownAssistant(dt, ref items, rows[i], curHeader.Replace("┣", "|").Replace("┗", "┣") + "┣"); if (rows.Length > 0) CreateLevelDropDownAssistant(dt, ref items, rows[rows.Length - 1], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┗"); }