This article describes the C # recursive method to achieve the infinite level of classification display effect. Share to everyone for your reference, specific as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Text;
Using System.Data;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Public partial class RoleDemo20150305:System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {
DataTable dt = Createdt (); DataTable dtfinal = dt.
Clone ();
list<string> list = new list<string> ();
String userstr = Getuserdata ();
if (Userstr.contains (",")) {string[] STRs = Userstr.split (', '); for (int i = 0; i < STRs. Length; i++) {list.
ADD (Strs[i]); } else {list.
ADD (USERSTR); for (int i = 0; i < list. Count;
i++) {string module_id = List[i]; datarow[] Drsource = dt. Select ("module_id = '" + module_id + ""); Finally DT exists the data datarow[] drexist = Dtfinal.select ("module_id = '" + module_id + ""); Finally DT exists the data if (drexist.length = 0) {Dtfinal.rowS.add (drsource[0]["module_id"], Getpretag (drsource[0]["Module_level"]) + drsource[0]["Module_name"], drSource[0][" Module_fatherid "], drsource[0][" Module_url "], drsource[0][" Module_order "], drsource[0][" Module_level "]);
Add record} findfatherfromdt (DT, Drsource, dtfinal);
//display mode one DataView dv = Dtfinal.defaultview; Dv.
Sort = "module_id ASC"; DataTable dtneed = dv.
ToTable ();
Gridview1.datasource = Dtneed;
Gridview1.databind ();
Display mode two StringBuilder builder = new StringBuilder ();
for (int i = 0; i < DtNeed.Rows.Count i++) {DataRow dr = Dtneed.rows[i]; Builder. Appendline ("<span data-id=" "+ dr[" module_id "] +" ' data-father= ' "+ dr[" Module_fatherid "] +" ' style= ' Display:none "&G
t; "
+ dr["module_name"] + "</span>"); } LT1. Text = Builder.
ToString (); ///<summary>///continues to trace the addition of parent class records///</summary>///<param name= "DT" ></param>///<para M name= "Dtfinal";</param> protected void Findfatherfromdt (DataTable dt, datarow[] drfather, DataTable dtfinal) {datarow[] D Rfatherup = dt.
Select ("module_id = '" + drfather[0]["Module_fatherid"] + "" ");
datarow[] Drfatherupexist = Dtfinal.select ("module_id = '" + drfather[0]["Module_fatherid"] + "" "); if (drfatherup.length > 0 && drfatherupexist.length = = 0)//The record has a parent class and the parent class is not added to the final dt {DTFINAL.ROWS.ADD (DrF atherup[0]["module_id"], Getpretag (drfatherup[0]["Module_level"]) + drfatherup[0]["Module_name"], drFatherUp[0][" Module_fatherid "], drfatherup[0][" Module_url "], drfatherup[0][" Module_order "], drfatherup[0][" Module_level "]);
Continue to trace the addition of the parent class record FINDFATHERFROMDT (DT, Drfatherup, dtfinal); #region add prefix protected string getpretag (Object num) {string pre = string.
Empty;
int i = Convert.ToInt32 (num);
if (I < 2) {} else if (i = = 2) {pre + = "└";
else {pre + = "└"; for (int j = 0;J < I-2;
J + +) {pre + = "-";
} return to pre;
#endregion #region user test data protected static string Getuserdata () {DataTable dt = new DataTable (); Dt.
Columns.Add ("UserId"); Dt.
Columns.Add ("Usermoduleoption"); Dt.
Rows.Add ("John", "m01010305,m010205,m0101030301"); Return dt. rows[0]["Usermoduleoption"].
ToString ();
#endregion #region Create data protected static DataTable Createdt () {DataTable dt = new DataTable (); Dt.
Columns.Add ("module_id"); Dt.
Columns.Add ("Module_name"); Dt.
Columns.Add ("Module_fatherid"); Dt.
Columns.Add ("Module_url"); Dt.
Columns.Add ("Module_order"); Dt.
Columns.Add ("Module_level"); Dt.
Rows.Add ("C1", "national", "0", "", "1", "1"); Dt.
Rows.Add ("M01", "Guangdong", "C1", "" "," 1 "," 2 "); Shenzhen dt.
Rows.Add ("M0101", "Shenzhen", "M01", "3.aspx", "100", "3"); Dt.
Rows.Add ("M010101", "Nanshan District", "M0101", "4.aspx", "1000", "4"); Dt.
Rows.Add ("M010102", "Luohu District", "M0101", "" "," 1001 "," 4 "); Dt. RoWS.
ADD ("M010103", "Futian District", "M0101", "" "," 1002 "," 4 "); Dt.
Rows.Add ("M010104", "Baoan District", "M0101", "" "," 1003 "," 4 "); Dt.
Rows.Add ("M010105", "Longgang District", "M0101", "" "," 1004 "," 4 "); Shenzhen-Futian District dt.
Rows.Add ("M01010301", "on Merlin", "M010103", "" "," 1002001 "," 5 "); Dt.
Rows.Add ("M01010302", "under Merlin", "M010103", "" "," 1002002 "," 5 "); Dt.
Rows.Add ("M01010303", "Che Kung Temple", "M010103", "", "1002003", "5"); Dt.
Rows.Add ("M01010304", "Bamboo Grove", "M010103", "" "," 1002004 "," 5 "); Dt.
Rows.Add ("M01010305", "Gossip Ridge", "M010103", "" "," 1002005 "," 5 "); Dt.
Rows.Add ("M01010306", "Huaqiang North", "M010103", "" "," 1002006 "," 5 "); Shenzhen-Futian District-che Kung temple dt.
Rows.Add ("M0101030301", "Tian an digital City", "M01010303", "" "," 100200301 "," 6 "); Guangzhou dt.
Rows.Add ("M0102", "Guangzhou", "M01", "" "," 101 "," 3 "); Dt.
Rows.Add ("M010201", "Yuexiu District", "M0102", "" "," 1105 "," 4 "); Dt.
Rows.Add ("M010202", "Haizhu District", "M0102", "" "," 1106 "," 4 "); Dt.
Rows.Add ("M010203", "Tianhe District", "M0102", "" "," 1107 "," 4 "); Dt. Rows.Add ("M010204", "Baiyun District", "M0102", "" "," 1108 "," 4 "); Dt.
Rows.Add ("M010205", "Huangpu District", "M0102", "" "," 1109 "," 4 "); Dt.
Rows.Add ("M010206", "Liwan District", "M0102", "" "," 1110 "," 4 "); Dt.
Rows.Add ("M010207", "Luo Gang area", "M0102", "" "," 1111 "," 4 "); Dt.
Rows.Add ("M010208", "South Sandy Area", "M0102", "" "," 1112 "," 4 ");
return DT;
} #endregion}
Read more about C # Interested readers can view the site topics: "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # object-oriented Program design Introductory Course" and "C # programming Thread Usage Skill Summary"
I hope this article will help you with C # programming.