First, the code for creating the database table is as follows:
Database table code for infinite-level tree
if exists (select * from 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 level code of the menu, the top level is 0, and the other level codes are the flowid of the previous menu.
Use the Menu control in the navigation as a menu.
First, you need to add a top-level menu with level 0 to the menu, the code is as follows:
/// <summary>
/// Obtain the system menu based on user permissions
/// </summary>
private void GetSysMenu()
{
string str = "select * from work_sysmenu where (menu_role,'" + +"')=1 and isvalid=1 order by menu_order";
DataSet ds= (str);
DataRow[] drRoot = [0].Select("menu_parent=0");
//Change to generate parent menu
foreach (DataRow dr in drRoot)
{
MenuItem mi = new MenuItem();
= dr["menu_title"].ToString();
= dr["menu_value"].ToString();
// = dr["menu_url"].ToString();
= false;
(mi);
//The recursive algorithm generates all levels of submenu submenu
CreateMenu([0], dr["flowid"].ToString(), mi);
}
}
The above code is a method in the data persistence layer framework I use to execute a piece of SQL and return a Dataset. This can be used to use different methods according to your needs. There is also an SQL statement containing a custom function I wrote myself, which is used to get the number of characters in a string.
Create function GetCharCount(@target 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 is the method code for generating the lower levelless tree:
/// <summary>
/// Create a stepless tree menu
/// </summary>
/// <param name="dt">Get the data source of the menu</param>
/// <param name="parentID">Parent ID of the menu</param>
/// <param name="parItem">Create menu Item</param>
private void CreateMenu(DataTable dt,string parentID,MenuItem parItem)
{
DataRow[] drs= ("menu_parent=" + parentID);
if ( > 0)
{
foreach (DataRow dr in drs)
{
MenuItem mi = new MenuItem();
= dr["menu_title"].ToString();
= dr["menu_value"].ToString();
= dr["menu_url"].ToString();
(mi);
CreateMenu(dt, dr["flowid"].ToString(), mi);
}
}
else
{
return ;
}
}
OK, now you just need to add menu records in the data table to generate menus of the required level. It should be noted that the menu_parent value of the top menu must be 0. Of course, you can also make modifications as needed on this basis.
Database table code for infinite-level tree
Copy the codeThe code is as follows:
if exists (select * from 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 level code of the menu, the top level is 0, and the other level codes are the flowid of the previous menu.
Use the Menu control in the navigation as a menu.
First, you need to add a top-level menu with level 0 to the menu, the code is as follows:
Copy the 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 (menu_role,'" + +"')=1 and isvalid=1 order by menu_order";
DataSet ds= (str);
DataRow[] drRoot = [0].Select("menu_parent=0");
//Change to generate parent menu
foreach (DataRow dr in drRoot)
{
MenuItem mi = new MenuItem();
= dr["menu_title"].ToString();
= dr["menu_value"].ToString();
// = dr["menu_url"].ToString();
= false;
(mi);
//The recursive algorithm generates all levels of submenu submenu
CreateMenu([0], dr["flowid"].ToString(), mi);
}
}
The above code is a method in the data persistence layer framework I use to execute a piece of SQL and return a Dataset. This can be used to use different methods according to your needs. There is also an SQL statement containing a custom function I wrote myself, which is used to get the number of characters in a string.
Copy the codeThe code is as follows:
Create function GetCharCount(@target 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 is the method code for generating the lower levelless tree:
Copy the codeThe code is as follows:
/// <summary>
/// Create a stepless tree menu
/// </summary>
/// <param name="dt">Get the data source of the menu</param>
/// <param name="parentID">Parent ID of the menu</param>
/// <param name="parItem">Create menu Item</param>
private void CreateMenu(DataTable dt,string parentID,MenuItem parItem)
{
DataRow[] drs= ("menu_parent=" + parentID);
if ( > 0)
{
foreach (DataRow dr in drs)
{
MenuItem mi = new MenuItem();
= dr["menu_title"].ToString();
= dr["menu_value"].ToString();
= dr["menu_url"].ToString();
(mi);
CreateMenu(dt, dr["flowid"].ToString(), mi);
}
}
else
{
return ;
}
}
OK, now you just need to add menu records in the data table to generate menus of the required level. It should be noted that the menu_parent value of the top menu must be 0. Of course, you can also make modifications as needed on this basis.