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 );}}}