如何在 ASP.NET 中基于角色获取 XML 格式的菜单并将其绑定到菜单控件





0/5 (0投票)
本文档描述了如何在 ASP.NET 中基于角色获取 XML 格式的菜单并将其绑定到菜单控件。
引言
以下是获取基于角色的 XML 格式菜单并在 ASP.NET 中将其绑定到菜单控件的步骤。
- 创建
GroupMaster
表,用于定义角色或组。此表包含以下列:pkGroupId
作为主键,以及GroupName
和Description
。 - 创建另一个
Table
,即Menu
表,其中包含诸如pkMenuId
作为主键(定义菜单)、ParentID
(定义子菜单应显示在哪个菜单下)、Title
、URL
、description
和Roles
等列。 - 创建第三个表,
GroupMenuDetails
,用于放置上述两个表的主键关系,即pkGroupId
和pkMenuId
。此表包含一个IsDisplay
标志。 - 现在创建存储过程,该过程接受角色作为输入参数,并输出 XML 字符串。
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO Create PROCEDURE [dbo].[GetXMLMenus] ( @Role as Varchar(100) ) AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. -- SET NOCOUNT ON; -- SET @Role = 1; SELECT -- Map columns to XML attributes/elements with XPath selectors. MainMenus.Title AS '@Text', Url AS '@Url', ( -- Use a sub query for child elements. SELECT SubMenus.Title AS '@Text', SubMenus.Url AS '@Url' FROM ( SELECT MD.pkMenuId AS pkMenuId, MD.ParentId AS ParentID, MD.Title, MD.Description, MD.Url, GMD.pkGroupMenuId, GMD.IsDisplay FROM GroupMenuDetails AS GMD INNER JOIN Menu AS MD ON GMD.pkMenuId = MD.pkMenuId AND GMD.pkGroupId = @Role ) AS SubMenus WHERE SubMenus.ParentID = MainMenus.pkMenuID AND SubMenus.ParentID IS NOT Null ORDER BY SubMenus.pkMenuID FOR XML PATH('SubMenu'),--The element name for each row. TYPE -- Column is typed so it nests as XML, not text. ) --AS 'products' -- The root element name for this child collection. FROM ( SELECT MD.pkMenuId AS pkMenuId, MD.ParentId AS ParentId, MD.Title, MD.Url, MD.Description, GMD.pkGroupMenuId, GMD.IsDisplay FROM GroupMenuDetails AS GMD INNER JOIN Menu AS MD ON GMD.pkMenuId = MD.pkMenuId AND GMD.pkGroupId = @Role AND MD.ParentId IS Null ) AS MainMenus FOR XML PATH('Menu'), --The element name for each row. ROOT('Menus') --The root element name for this result set. END
- 现在创建 aspx 页面,并在其上放置菜单控件和
xmlDataSource
。 - 现在编写页面加载代码,通过将参数作为角色传递来调用存储过程。
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { populatemenu(); } } protected void populatemenu() { --Create the sql connection . SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["SqlConn1"]); --Role Parameter int Role =1; SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; --Call to Stored Procedure cmd.CommandText = "GetXMLMenus"; if ((Role !=0)) { cmd.Parameters.AddWithValue("@Role", Role); cmd.Connection = conn; SqlDataAdapter da_1 = new SqlDataAdapter(cmd); DataSet ds_1 = new DataSet(); string result = ""; Xml XmlData = new Xml(); XmlDocument XmlDocument = new XmlDocument(); try { conn.Open(); --get the xml string result =Convert.ToString(cmd.ExecuteScalar()); da_1.Fill(ds_1); ds_1.DataSetName = "Menus1"; ds_1.Tables[0].TableName = "Menu1"; --Bind the get the xml string to xmldatasourse XmlDataSource1.Data = result; XmlDataSource1.XPath = "Menus/Menu"; --Bind the datasource to the menu control Menu1.DataSourceID= "XmlDataSource1"; Menu1.DataBind(); } catch (Exception ex) { Response.Write(ex.Message); } finally { conn.Dispose(); cmd.Dispose(); da_1.Dispose(); ds_1.Dispose(); } } }
优势/好处
我们可以使用相同的代码来处理 TreeView
控件或菜单控件,而无需更改存储过程或代码。我们只需要将 TreeView
控件放置在菜单控件的位置即可。
历史
- 2012 年 1 月 29 日:初始版本