asp.net a custom tree with XML generation and extension-practical tips

Source: Internet
Author: User
The data source is XML, so it expands.
No more nonsense, directly on the code (for some reason, the partial data modified or streamlined).
XML code:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<MenuData>
<module id= "Business" permissions= "49,53,58,59,65,99,100,70,69" name= "Business Management" isexpand= "true" >
<menu id= "Orders" permissions= "49,53,58" name= "My Orders" isexpand= "true" >
<item permissions= "isonlyvip=" "true" name= "booking order" url= "business/prolist.aspx" title= "Reservation Form"/>
<item id= "Renting" permissions= "isonlyvip=" "true" name= "in order" url= "business/list.aspx" title= "in order"/>
<item permissions= isonlyvip= "true" name= "Order History" url= "Business/rlist.aspx" title= "Order History"/>
</Menu>
<menu id= "Clients" permissions= "Name=" My Customers isexpand= "true" >
<item id= "clientlist" permissions= "name=" "Customer List" url= "client/list.aspx" title= "Customer List"/>
</Menu>
<menu id= "" permissions= 65,99,100 name= My Reports isexpand= "true" >
<item permissions= "isonlyvip=" "true" name= "income Statistics" url= "report/finance.aspx" title= "Income statistics"/>
<item permissions= "isonlyvip=" "true" name= "product Statistics" url= "report/product.aspx" title= "Product statistics"/>
<item permissions= isonlyvip= "true" name= "Customer Statistics" url= "report/client.aspx" title= "Customer statistics"/>
</Menu>
<menu id= "" permissions= "70,69" name= "message Management" isexpand= "true" >
<item permissions= "name=" Send Station message "url=" "title=" Send Station Message "eventscript=" onclick= ' Openmsgwindow () ' "/>
<item id= "receivemsglist" permissions= "name=" message receive list Url= "message/rlist.aspx" title= "message receive list"/>
<item permissions= "name=" "Message send List" url= "message/slist.aspx" title= "message send List"/>
</Menu>
</Module>
<module id= "Advertise" permissions= "89,90" name= "I want to promote" eventscript= "Opentuiguangindex ();" >
<menu id= "" "permissions=" name= "Product Promotion" Url= "tuiguang/product.aspx" title= "Product promotion"/>
<menu id= "" "permissions=" name= "advertisement Promotion" url= "tuiguang/ad.aspx" title= "advertisement promotion"/>
</Module>
<module id= "Systemmanage" permissions= "67,68" name= "System Settings" >
<menu id= "permissions=" name= "Change system Password" url= "sysmanage/password.aspx" title= "Change system password"/>
<menu id= "" permissions= "name=" System configuration Url= "sysmanage/config.aspx" title= "System Configuration"/>
</Module>
<module id= "VIP" permissions= "name=" Member Introduction "isunvip=" true ">
<menu id= "name=" Member Introduction "Url=" vip/index.html "title=" member service/>
</Module>
</MenuData>

Program code:
Copy Code code as follows:

Using System.Text;
Using System.Collections;
Using System.Xml;
Using System.Web;
Using System;
<summary>
Summary description of Createtree
</summary>
public class Menutree
{
int index = 0;//Menu Column ID index
Private ArrayList havepermission = new ArrayList ();
private bool Isvip = false;
<summary>
Permissions that the logged-on user has
</summary>
Private ArrayList Havepermissions
{
get {return havepermission;}
set {havepermission = value;}
}
<summary>
Whether the logged-on user is a VIP
</summary>
private bool Isvip
{
get {return ISVIP;}
set {ISVIP = value;}
}
<summary>
Whether the logged-on user has the privilege of being a VIP user
</summary>
<param name= "Havepermission" ></param>
<param name= "ISVIP" ></param>
Public Menutree (ArrayList havepermission, bool ISVIP)
{
This. Havepermissions = havepermission;
This. ISVIP = ISVIP;
}
<summary>
Binding tree
</summary>
public string Binddatatotree ()
{
System.Xml.XmlDocument document = new System.Xml.XmlDataDocument ();
Document. Load (HttpContext.Current.Server.MapPath ("Menudata.xml"));
return createtreehtml (document. DocumentElement, 0);
}
<summary>
Create a column tree
</summary>
<param name= "Document" >xml node </param>
<param name= "Deep" > Tree depth </param>
private string createtreehtml (System.Xml.XmlNode document, int deep)
{
String nodeType = "menu";//the type of node to generate the CSS type of the child node
StringBuilder treehtml = new StringBuilder ();
foreach (System.Xml.XmlNode node in document. ChildNodes)
{
String MenuID = String. Empty;
String treenodehtml = String. Empty;
String nodename = node. Name;
String showname = Getattributesvalue (node. attributes["Name"])//Display column name
String nodeId = Getattributesvalue (node. attributes["id"]);//Column ID
BOOL Isexpand = getattributesvalue (node. attributes["Isexpand"]). ToLower (). Trim () = = "true"? Whether the true:false;//is expanded
String permissions = Getattributesvalue (node. attributes["Permissions"]);//Permission string
BOOL ISONLYVIP = getattributesvalue (node. attributes["ISONLYVIP"]). ToLower (). Trim () = = "true"? true:false;//whether only VIP access is allowed
BOOL ISUNVIP = getattributesvalue (node. attributes["ISUNVIP"]). ToLower (). Trim () = = "true"? true:false;//whether only non-VIP access
String eventscript = Getattributesvalue (node. attributes["Eventscript"]);//Event Script
int chlidnodescount = node. childnodes.count;//Number of child nodes
BOOL Ispermissions = getispermissions (permissions);//have permission
if (!ispermissions)
{
Continue If there is no permission, this node is not generated
}
if (nodename = = "Module")
{
if (ISUNVIP && isvip)
{
continue;//if the VIP member is set to not allow access to the sub column
}
MenuID = Getmenuid (nodeId);
Treehtml.appendformat ("<div class= ' Module ' id= ' menu{0} ' onclick= ' Donodes" (this); 1} ' onselectstart= ' return false; ' > ", MenuID, Eventscript);
Treehtml.append ("Treehtml.appendformat ("<span>{0}</span>", showname);
Treehtml.append ("</div>");
Deep = 0;
NodeType = "Module";
}
Else
{
Treehtml.append ("<table cellpadding= ' 0 ' cellspacing= ' 0 ' style= '" border-width:0;width:90% ' > ");
Treehtml.append ("<tr class= ' Nodeline ' >");
for (int i = 0; i < deep; i++)
{
if (i = = deep-1)
{
Treehtml.append ("<td class= ' Nodeicobox ' >");
if (Chlidnodescount > 0)
{
MenuID = Getmenuid (nodeId);
Treehtml.appendformat ("<a id= ' menu{0} ' href= ' javascript:; ' onclick= ' Donodes (this,\" menu\ ") ' ></a>, MenuID, (Isexpand?) "Open-menu": "Close-menu"));
}
Else
{
Treehtml.append ("}
Treehtml.append ("</td>");
}
Else
{
Treehtml.append ("<td style= ' width:20px;") > </td> ");
}
}
String url = getattributesvalue (node. attributes["URL"]); Link Address
string title = Getattributesvalue (node. attributes["title"]);//link Title information
String Menunodeid = Nodeid.trim (). Length > 0? "Id= ' Menunode" + nodeId + "'": String. empty;//tree Node ID
Treehtml.append ("<td style= ' white-space:nowrap;") > ");
if (URL. Length > 0 | | Chlidnodescount = 0)
{
if (!isonlyvip | | (Isonlyvip && ISVIP)) Whether the column is only open for VIP
{
if (URL. Length > 0)
{
Treehtml.appendformat ("<a href= ' {0} ' target= ' MainFrame ' title= ' {1} ' {3} {4}>{2}</a>", URL, title, ShowName , Eventscript, Menunodeid);
}
Else
{
Treehtml.appendformat ("<a href= ' javascript:; ' target= ' MainFrame ' title= ' {0} ' {2} {3}>{1}</a>", title, ShowName, Eventscript, Menunodeid);
}
}
Else
{
Treehtml.appendformat (' <a href= ' javascript:; ' target= ' MainFrame ' title= ' {1} ' onclick= ' return Alertvip (); ' class= ' Disablecolor ' {3}>{2}</a> ', url, title, ShowName, Menunodeid);
}
}
Else
{
Treehtml.appendformat ("<a href= ' javascript:; ' onclick= ' Doaclick (\" menu{0}\ "); ' title= ' {1} ' {3} {4}>{2}</a > ", MenuID, title, ShowName, Eventscript, Menunodeid);
}
Treehtml.append ("</td>");
Treehtml.append ("</tr>");
Treehtml.append ("</table>");
}
if (Chlidnodescount > 0)
{
treenodehtml = createtreehtml (node, deep + 1);
}
if (Treenodehtml.length > 0)
{
Treehtml.appendformat ("<div id= ' Menu{0}nodes ' {1} style= ' {2} ' >", MenuID, (NodeType = "Module"?) "Class= ' Menus '": "Class= ' Menunodes '"), (Isexpand? "Display:block;": "Display:none;");
Treehtml.append (treenodehtml);
Treehtml.append ("</div>");
}
}
return treehtml.tostring ();
}
<summary>
Get the ID of the column
</summary>
private string Getmenuid (String nodeId)
{
Return nodeid.length > 0? NodeId:Convert.ToString (++index);
}
<summary>
Get node value
</summary>
private String Getattributesvalue (XmlAttribute attributevalue)
{
return attributevalue = null? "": AttributeValue.Value.Trim ();
}
<summary>
Whether you have permission
</summary>
private bool Getispermissions (string permissions)
{
if (havepermissions.count = 0)
{
return false;
}
if (permissions. Length = = 0)
{
return true;
}
Else
{
string[] arrpermissions = permissions. Split (', ');
for (int i = 0; i < arrpermissions.length; i++)
{
if (Havepermissions.contains (arrpermissions[i). Trim ())
{
return true;
}
}
return false;
}
}
}

Related JS code:
Copy Code code as follows:

function Donodes (obj,type)
{
var divid=obj.id+ ' Nodes ';
var qdivobj=$ ("#" +divid);
if (qdivobj.is (": Hidden"))
{
Qdivobj.show ();
if (type== ' menu ')
{
$ (obj). Children ("img"). attr ("src", "images/open-menu.gif");
}
Else
{
Qdivobj.siblings (". Menus:visible "). Hide ();
}
}
Else
{
Qdivobj.hide ();
if (type== ' menu ')
{
$ (obj). Children ("img"). attr ("src", "images/close-menu.gif");
}
}
}
function Doaclick (ID)
{
$ ("#" +id). Click ();
}
function Alertvip ()
{
Alert ("Very sorry, this module is only open to VIP Members!"); return false;
}

Final effect

This code I feel easy to expand, as long as a little change in the code can be used in their own projects.
Related Article

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.