65.9K
CodeProject 正在变化。 阅读更多。
Home

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

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2012年1月30日

CPOL

1分钟阅读

viewsIcon

20450

本文档描述了如何在 ASP.NET 中基于角色获取 XML 格式的菜单并将其绑定到菜单控件。

引言

以下是获取基于角色的 XML 格式菜单并在 ASP.NET 中将其绑定到菜单控件的步骤。

  1. 创建 GroupMaster 表,用于定义角色或组。此表包含以下列:pkGroupId 作为主键,以及 GroupNameDescription

    image001.jpg

  2. 创建另一个 Table,即 Menu 表,其中包含诸如 pkMenuId 作为主键(定义菜单)、ParentID(定义子菜单应显示在哪个菜单下)、TitleURLdescriptionRoles 等列。

    image002.jpg

  3. 创建第三个表,GroupMenuDetails,用于放置上述两个表的主键关系,即 pkGroupIdpkMenuId。此表包含一个 IsDisplay 标志。

    image003.jpg

  4. 现在创建存储过程,该过程接受角色作为输入参数,并输出 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
  5. 现在创建 aspx 页面,并在其上放置菜单控件和 xmlDataSource

    image004.jpg

    image005_small.jpg - Click to enlarge image

  6. 现在编写页面加载代码,通过将参数作为角色传递来调用存储过程。
    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 日:初始版本
© . All rights reserved.