First, the code for creating a database table is as follows:
Database Table code of the infinite level tree
Copy codeThe Code is 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] (100) COLLATE Chinese_PRC_CI_AS NULL,
[Menu _value] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL,
[Menu_url] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL,
[Menu_parent] [int] NULL,
[Menu_role] [varchar] (200) 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
Among them, menu_parent is the menu level code, the top level is 0, and other levels of code is the flowid of the upper menu.
ASP. NET uses the Menu Control in the navigation bar as the Menu.
First, add the top Menu with a level of 0 to the Menu. The Code is as follows:
Copy codeThe Code is as follows:
/// <Summary>
/// Obtain the system menu based on User Permissions
/// </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 ");
// Generate the parent menu cyclically
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 sub-menus of all levels
CreateMenu (ds. Tables [0], dr ["flowid"]. ToString (), mi );
}
}
In the above Code, sysSqlRunner. getDataset is a method in the data persistence layer framework for my own use. It is used to execute an SQL statement and return a Dataset. Different methods can be used as needed. In addition, an SQL statement contains a self-defined function dbo. GetCharCount, which is used to obtain the number of characters in a string.
Copy codeThe Code is as follows:
Create function GetCharCount (@ destination varchar (100), @ sear varchar (1 ))
Returns int
As
Begin
Declare @ charcount int
Select @ charcount = (len (@ target)-len (replace (@ target, @ sear ,'')))
Return @ charcount
End
The following code generates a lower-level stepless tree:
Copy codeThe Code is as follows:
/// <Summary>
/// Create a stepless tree menu
/// </Summary>
/// <Param name = "dt"> obtain the data source from the menu </param>
/// <Param name = "parentID"> parent ID of the menu </param>
/// <Param name = "parItem"> Create a 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;
}
}
Now, you only need to add menu records to the data table to generate the required menus. Note that the menu_parent value of the top-level menu must be 0. Of course, you can also make modifications based on your needs.