C # does not produce a beautiful tree structure without the TreeView control

Source: Internet
Author: User
The project requires its own template-driven and UI structure to make it impossible to use any asp.net-self controls.
Today to create a tree structure of infinite classification, I refer to an algorithm on the web is said to be phpcms used to generate tree classes. Really handsome, not too much time directly copied into the C # version.
<summary>///Generic tree class that can generate any tree structure///</summary> public class Catetreeview {ilist& Lt  categoryinfo> lscate = new list<categoryinfo> ();   Initialize array string[] icons = new string[] {"│", "├", "└"};
        Modifier symbol String htmlret = "";
        String nbsp = " ";
            Public Catetreeview (ilist<categoryinfo> ls) {lscate = ls;
        Htmlret = "";
                CategoryInfo getone (int id) {foreach (categoryinfo m in lscate) {
                if (M.cateid = = ID) {return m;
        } return null; ///<summary>///Get parent series///</summary>///<param name= "myID" ></p
        aram>///<returns></returns> public ilist<categoryinfo> get_parent (int myid) {ilist<categoryinfo> lsNew = new list<categoryinfo> ();
            CategoryInfo m = GetOne (myID);
            if (M = = null) return lsnew;
            int pid = M.parentid;
            if (PID = = 0) return lsnew;
            PID = GetOne (PID). ParentID;
                    foreach (CategoryInfo mcate in lscate) {if (Mcate.parentid = = pid) { Lsnew.
                ADD (mcate);

        } return lsnew; ///<summary>///Get child classification///</summary>///<param name= "myID" ></pa 
            ram>///<returns></returns> public ilist<categoryinfo> get_child (int myid) {

            ilist<categoryinfo> lsnew = new list<categoryinfo> ();
                    foreach (CategoryInfo mcate in lscate) {if (Mcate.parentid = = myID) { Lsnew.
                ADD (mcate);
            }
            }return lsnew; }///<summary>///gets the tree structure///</summary>///<param name= "myID" > says get this  All children under ID </param>///<param name= "str" > Basic code for generating a tree structure, for example: Option value= $id $selected $spacer $name option </param>///<param name= "Sid" > selected IDs, such as the need to use </param>///<param name= when making a tree drop-down box adds
        "></param>///<param name=" Str_group "></param>///<returns></returns> public string Get_tree (int myid, string str, int sid, String adds, string str_group) {int numb
            ER = 1;
            ilist<categoryinfo> child = Get_child (myID); if (child. Count > 0) {int total = child.
                Count;
                    foreach (categoryinfo m in child) {string j = "", k = ""; if (number = total) {j + icoNS[2];
                        else {j + + icons[1]; K = adds!= ""?
                    Icons[0]: ""; String spacer = Adds!= ""?
                    Adds + J: ""; String selected = M.cateid = = Sid?
                    "Selected": "";
                    String tempstr = ""; if (M.parentid = = 0 && str_group!= "")//?
                    \ $nstr = \ "$str _group\"; ":" \ $nstr = \ "$str \"; ";
                    {tempstr = Str_group;
                    else {tempstr = str; } TempStr = TempStr.
                    Replace ("$id", m.cateid.tostring ()); TempStr = TempStr.

                    Replace ("$parentid", m.parentid.tostring ()); TempStr = TempStr.
                    Replace ("$select", selected); TempStr = TempStr.
                Replace ("$spacer", spacer);    TempStr = TempStr.
                    Replace ("$name", m.catename); TempStr = TempStr.

                    Replace ("$modelid", m.modelid.tostring ());
                    Htmlret + = TempStr;
                    String bspstr = nbsp;
                    Get_tree (M.cateid, str, sid, adds + K + bspstr, str_group);
                    number++;
                    $this->ret. = $nstr;
                    $nbsp = $this->nbsp;
                    $this->get_tree ($id, $str, $sid, $adds. $k, $nbsp, $str _group);
                $number + +;
            } return Htmlret;
        return $this->ret;
        ///<summary>///The above methods, jquery TreeView style, Scalable style (requires TreeView plugin support)///</summary> <param name= "myID" > means to get all the children under this ID </param>///<param name= "effected_id" > need to generate the number of TreeView directories id</param>///<param name= "str" > End-level style </param>///<param name= "str2" > Directory-level sampleType </param>///<param name= "Showlevel" > Direct display level number, the rest is asynchronous display, 0 is full limit </param>///<param name= The ' style ' > TOC style default Filetree can add additional styles such as ' Filetree treeview-famfamfam</param>///<param name= ' CurrentLevel
        t; Compute the current level, recursive use of the modified function does not need to use this parameter </param>///<param name= "recursion" > recursion to use external call when false</param> <returns>HTML</returns> public string Get_treeview (int myID, string effected_id, String str, stri ng str2, int showlevel, string style, int currentlevel, bool recursion) {ilist<categoryinfo> ch
           
            ILD = Get_child (myID);

            string effected = "id=\" "+ effected_id +" "";
            String placeholder = "<ul><li><span class= ' placeholder ' ></span></li></ul>";
            if (!recursion) Htmlret + = "<ul" + effected + "class=\" "+ style +" \ >;
               foreach (categoryinfo m in child) { ilist<categoryinfo> child2 = Get_child (M.cateid);
                String folder = "";
                @extract ($a); if (Showlevel > 0 && showlevel = = CurrentLevel && (child2. Count > 0) folder = "HasChildren";
                For example, set display level mode @2011.07.01 string floder_status = ((Folder!= "")? "class=\" "+ folder +" \ "" "" "); Htmlret + = recursion? "<ul><li" + floder_status + "id=\" "+ M.cateid +" > ":" <li "+ floder_status +" id=\ "" + M.cateid + "
                \ ">";
                recursion = false; if (child2.
                    Count > 0) {string nstr = str2;
                    Eval ("\ $nstr = \" $str 2\ ";");
                    string tempstr = str2; TempStr = TempStr.
                    Replace ("$id", m.cateid.tostring ()); TempStr = TempStr.
                    Replace ("$parentid", m.parentid.tostring ()); TempStr = TempStr.
  Replace ("$select", selected);                  TempStr = TempStr.
                    Replace ("$spacer", spacer); TempStr = TempStr.
                    Replace ("$name", m.catename); TempStr = TempStr.
                    Replace ("$modelid", m.modelid.tostring ());
                    Htmlret + = TempStr; if (showlevel = 0 | |
                    (Showlevel > 0 && showlevel > CurrentLevel))
                    {Get_treeview (M.cateid, effected_id, str, str2, showlevel, style, CurrentLevel + 1, true); 
                        else if (Showlevel > 0 && showlevel = = currentlevel) {
                    Htmlret + = placeholder;
                    } else {string nstr = str;
                    string tempstr = str; TempStr = TempStr.
                    Replace ("$id", m.cateid.tostring ()); TempStr = TempStr.
              Replace ("$parentid", m.parentid.tostring ());      TempStr = TempStr.
                    Replace ("$select", selected); TempStr = TempStr.
                    Replace ("$spacer", spacer); TempStr = TempStr.
                    Replace ("$name", m.catename); TempStr = TempStr.
                    Replace ("$modelid", m.modelid.tostring ());
           
                    Htmlret + = TempStr;
                str = NSTR; } Htmlret + = recursion?
            "</li></ul>": "</li>";
            if (!recursion) Htmlret + = "</ul>";
        return htmlret; }

    }


Not too high reusability, the use of people to improve their own hehe. Don't forget to change it and share it. Where the CategoryInfo class is a taxonomy entity class.

The effect shown with the jquery TreeView plugin is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.