asp.net of C generates the unlimited level menu

  • 2020-05-09 18:29:45
  • OfStack

First, the code to create the database table is as follows:
Infinite level tree database table code
 
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 

Where, menu_parent is the level code of the menu, the top level is 0, and the other level code is flowid of the superior menu.
ASP.NET USES the Menu control in the navigation as a menu.
First, we need to add the top-level menu with level 0 to Menu. The code is as follows:
 
/// <summary> 
///  Get the system menu according to user privileges  
/// </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 to generate the 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); 
// The recursive algorithm generates submenus for all levels of children  
CreateMenu(ds.Tables[0], dr["flowid"].ToString(), mi); 
} 
} 

sysSqlRunner.getDataset in the above code is a method within the data persistence layer framework that I use myself to execute a segment of SQL and return Dataset, which can be used in different methods according to my own needs. Another SQL statement contains a custom function that I wrote myself, dbo.GetCharCount, 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 code for the method to generate a subordinate stepless tree:
 
/// <summary> 
///  Create a non-hierarchical tree menu  
/// </summary> 
/// <param name="dt"> Gets the data source for the menu </param> 
/// <param name="parentID"> The father of the menu ID</param> 
/// <param name="parItem"> menu-creating 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 you just need to add a menu record to the data table to generate the required level of menus. Note that the menu_parent value for the top-level menu must be 0. Of course, it can also be modified on this basis as required.

Related articles: