Create a dynamic Treeview

Source: Internet
Author: User

First, send a request to the server:

Datatable dt = deserializedatatable (getmenudata (defineconst. guser )); // send a request to the server to obtain the datatable object /// <summary> /// deserialize the datatable /// </Summary> /// <Param name = "pxml"> serialization </param> /// <returns> datatable </returns> Public static datatable deserializedatatable (string pxml) {datatable dt = new datatable (); try {stringreader strreader = new stringreader (pxml); xmlreader = xmlreader. create (strreader); xmlserializer serializer = new xmlserializer (typeof (datatable); dt = serializer. deserialize (xmlreader) as datatable; return DT;} catch (exception e) {return DT ;}}

Server processing content:

Public String getmenudata (string userid) {string groupid = defineconst. gusergroupid; string SQL = @ "select rightid, rightname, form_id, parentid from mix_right_info"; SQL = string. format (SQL, groupid, userid); dataset DS = GetSet (SQL); datatable dt = new datatable (); If (Ds. tables [0]! = NULL & Ds. tables [0]. rows. count> 0) {dt = Ds. tables [0];} DT. tablename = "Treeview"; return serializedatatablexml (DT );} /// <summary> /// serialize the able /// </Summary> /// <Param name = "PDT"> datatable containing data </param> /// <returns> serialized able </returns> Public static string serializedatatablexml (datatable DT) {// serialize ableablestringbuilder sb = new stringbuilder (); xmlwriter writer = xmlwriter. create (SB); xmlserializer serializer = new xmlserializer (typeof (datatable); serializer. serialize (writer, DT); writer. close (); return sb. tostring ();}

The client generates a tree structure for the datatable operation:

Createmainmenu (DT, tvmenu) /// <summary> /// generate a tree menu /// </Summary> /// <Param name = "DT"> datetable </param> /// <Param name = "TV"> Treeview </param> Public static bool createmainmenu (datatable DT, treeview TV) {string parentid = ""; foreach (datarow DR in DT. rows) {If (Dr [3]. tostring () = "1") {treenode Tn = new treenode (); tn. name = Dr [0]. tostring (); tn. TEXT = Dr [1]. tostring (); tn. tag = Dr [2]. tostring (); parentid = Dr [3]. tostring (); TV. nodes. add (TN); tn. imageindex = 0; addsubmenu (TN, DT, Dr) ;}} return true ;} /// <summary> /// generate sub-menu /// </Summary> /// <Param name = "tn"> </param> /// <Param name = "DT"> </param> // <Param name = "Dr"> </param> Private Static void addsubmenu (treenode TN, datatable DT, datarow Dr) {string parentid = ""; foreach (datarow DDR in DT. rows) {If (DDR [3]. tostring () = Dr [0]. tostring () {// The child node whose DDR is Dr, treenode ttn = new treenode (); ttn. name = DDR [0]. tostring (); // ttn. name = DDR ["form name"]. tostring (); ttn. TEXT = DDR [1]. tostring (); // ttn. TEXT = DDR ["permission name"]. tostring (); parentid = DDR [3]. tostring (); // parentid = DDR ["parent permission"]. tostring (); ttn. tag = DDR [2]. tostring (); ttn. imageindex = 0; ttn. selectedimageindex = 1; tn. nodes. add (ttn); addsubmenu (ttn, DT, DDR );}}}

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.