asp.net (C #) Generate Unlimited Level menu _ Practical Tips

Source: Internet
Author: User
First, the code to create the database table is as follows:
database table code for infinite-level trees
Copy Code code as follows:

if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Work_sysmenu] and OBJECTPROPERTY (ID, N ' isusertable ') = 1)
drop table [dbo]. [Work_sysmenu]
Go
CREATE TABLE [dbo]. [Work_sysmenu] (
[Flowid] [INT] IDENTITY (1, 1) not NULL,
[Menu_title] [varchar] (MB) COLLATE chinese_prc_ci_as NULL,
[Menu_value] [varchar] (MB) COLLATE chinese_prc_ci_as NULL,
[Menu_url] [varchar] (m) COLLATE chinese_prc_ci_as NULL,
[Menu_parent] [INT] Null
[Menu_role] [varchar] (m) COLLATE chinese_prc_ci_as NULL,
[Menu_meno] [Text] COLLATE Chinese_prc_ci_as NULL,
[IsValid] [INT] Null
[Menu_order] [INT] Null
) on [PRIMARY] textimage_on [PRIMARY]
Go

Where Menu_parent is the level code for the menu, the top 0, and the Other level code is the Flowid of the superior menu.
Asp. NET, use the menu control in the Navigator as the menus.
First you need to add a top-level menu at level 0 to the menu with the following code:
Copy Code code as follows:

<summary>
Get system menu based on user rights
</summary>
private void Getsysmenu ()
{
String str = "SELECT * from Work_sysmenu where dbo." GetCharCount (Menu_role, ' "+ This.roleid +") =1 and isvalid=1 order by Menu_order ";
DataSet ds= syssqlrunner.getdataset (str);
datarow[] Drroot = ds. Tables[0]. Select ("menu_parent=0");
Loop Generation Parent Menu
foreach (DataRow Dr in Drroot)
{
MenuItem mi = new MenuItem ();
Mi. Text = dr["Menu_title"]. ToString ();
Mi. Value = dr["Menu_value"]. ToString ();
Mi. NavigateUrl = dr["Menu_url"]. ToString ();
Mi. selectable = false;
MAINMENU.ITEMS.ADD (MI);
Recursive algorithms generate all levels of subordinate submenus
CreateMenu (ds. Tables[0], dr["Flowid"]. ToString (), MI);
}
}

The Syssqlrunner.getdataset in the above code is a method within the framework of a data persistence layer that I use to execute a section of SQL and return a dataset, which can be used in a different way depending on your needs. Another SQL statement contains a custom function Dbo.getcharcount I wrote, which is to get the number of characters in a string.
Copy Code code as follows:

Create function GetCharCount (@target varchar), @sear varchar (1))
returns int
As
Begin
DECLARE @charcount int
Select @charcount = (len (@target)-len (replace (@target, @sear, '))
Return @charcount
End

The following is a method code for generating a subordinate, level-free tree:
Copy Code code as follows:

<summary>
Create a level-free tree menu
</summary>
<param name= "DT" > Get menu Data source </param>
<param name= "ParentID" > Menu's parent id</param>
<param name= "Paritem" > Create menu item</param>
private void CreateMenu (DataTable dt,string parentid,menuitem Paritem)
{
Datarow[] drs= dt. Select ("menu_parent=" + parentid);
if (DRS). Length > 0)
{
foreach (DataRow Dr in Drs)
{
MenuItem mi = new MenuItem ();
Mi. Text = dr["Menu_title"]. ToString ();
Mi. Value = dr["Menu_value"]. ToString ();
Mi. NavigateUrl = dr["Menu_url"]. ToString ();
PARITEM.CHILDITEMS.ADD (MI);
CreateMenu (DT, dr["Flowid"). ToString (), MI);
}
}
Else
{
return;
}
}

OK, now just add a menu record to the datasheet to generate the menu of the level you want. It is important to note that the menu_parent value of the top-level menu must be 0. Of course, can also be based on the need to make changes.

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.