Asp.Net Web表单中的母版页 [英] Master Pages in Asp.Net Web forms

查看:76
本文介绍了Asp.Net Web表单中的母版页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii ..



我陷入了一个有多个母版页的情况。这是我的问题: -



我的网站使用Asp.Net默认会员提供商。我有3个用户和3个角色。



我有一个名为RaiseTicket.aspx的页面,所有角色的用户都可以访问此页面。母版页的设计是2列布局。左侧是导航,右侧是主要内容区域。所有角色在左列上都有不同的链接。管理员拥有所有链接,其他2个角色拥有有限的链接。我想要的是,如果管理员导航到任何页面,管理页面上的链接应该是可见的。如果用户导航到RaiseTicket.aspx页面,他应该只能看到自己的链接。



它就像一页有多个母版页视图。我应该如何实现这一目标。或者如果你有不同的观点请告诉我。

解决方案

你可以按照以下链接...





[ ^ ]




导航面板上的
给出runat =server属性,在内容页面上找到控制权主页面然后你可以访问控件并可以隐藏它。


你可以通过实现你自己的站点地图提供类(扩展 StaticSiteMapProvider )并根据用户角色添加或不将节点链接到您的母版页。必须通过覆盖方法 BuildSiteMap 来构建基于用户角色的主页链接节点,如下例所示:



  ///   <  摘要 >  
/// 使用
/// 管理节点,URL,标题,描述的指定站点地图提供程序,
/// _userRoles,其他属性以及
的显式和隐式资源键
/// /// < / summary >
/// < param < span class =code-summarycomment> name =key > 提供者特定的查找键。< / param >
/// < param name =title > 节点的标签,通常由导航控件显示。< / param >
/// < param name =url > 节点代表的网页的网址在网站内。< / param >
/// < param name =description > 节点描述。< / param >
/// < param name =_ userRoles > 允许查看页面的_userRoles的System.Collections.IList。< / param >
/// < 返回 > 新创建的站点地图节点。< / returns >
private SiteMapNode CreateNode( string 键,字符串标题,字符串 url, string description,IList _userRoles)
{
return new SiteMapNode( this ,key,url,title,description,_userRoles, null null null );
}

/// < 摘要 >
/// 添加新的站点地图节点节点。
/// < / summary >
/// < param < span class =code-summarycomment> name =node > 网站map node。< / param >
/// < param name =parentNode > 父节点。< / param >
protected 覆盖 void AddNode(SiteMapNode node,SiteMapNode parentNode)
{
base .AddNode(node,parentNode);
_rebuildSiteMap = true ;
}

/// < 摘要 >
/// 从站点地图中删除节点。
/// < / summary >
/// < param < span class =code-summarycomment> name =node > 节点删除。< / param >
受保护 覆盖 void RemoveNode(SiteMapNode节点)
{
base .RemoveNode(node);
_rebuildSiteMap = true ;
}

/// < 摘要 >
/// 获取根节点。
/// < / summary >
/// < 返回 < span class =code-summarycomment>> 根节点。< / returns >
protected 覆盖 SiteMapNode GetRootNodeCore()
{
return BuildSiteMap();
}

/// < 摘要 >
/// 检索一个布尔值,指示指定的站点地图节点
///
/// < / summary >
/// < param < span class =code-summarycomment> name =context > 上下文。< / param >
/// < param name =node > 站点地图节点。< / param >
/// < span class =code-summarycomment>< 返回 > 如果节点为真可由用户查看,否则为false。< / returns >
public 覆盖 bool IsAccessibleToUser(HttpContext context,SiteMapNode节点)
{
return ;
}

/// < 摘要 >
/// 构建站点地图的工人方法。
/// < / summary >
/// < 返回 < span class =code-summarycomment>> 根节点。< / returns >
public 覆盖 SiteMapNode BuildSiteMap()
{
if (_rebuildSiteMap)
{
BuildMainSiteMap();
//
_rebuildSiteMap = ;
}
//
返回 _rootNode;
}

/// < 摘要 >
/// 获取当前用户的根节点。
/// < / summary >
/// < 返回 < span class =code-summarycomment>> 根节点或null。< / returns >
SiteMapNode GetRootNode()
{
if (_ currentUser == null
return ;
else if (_ currentUser.UserRole == UserRoles.Administrator)
return CreateNode( _ root,Resources.Resource .MenuHome, UserListPage.aspx?Role = 2 null ,_ userRoles);
else
return CreateNode( _ root,Resources.Resource.MenuHome, ComplainListPage.aspx?Type = 1 null ,_ userRoles);
}

/// < 摘要 >
/// 构建主站点地图的工人方法。
/// < / summary >
private void BuildMainSiteMap()
{
_rootNode = GetRootNode();
if (_rootNode == null
返回;
//
// 添加根节点
//
base .AddNode(_rootNode, null );
//
SiteMapNode节点,subNode;
//
// 抱怨,Chilreen,案例
//
node = CreateNode( _ complainNode
Resources.Resource.MenuComplains, ComplainListPage.aspx null ,_ userRoles);
base .AddNode(node,_rootNode);
//
node = CreateNode( _ childrenNode
Resources.Resource.MenuChildren, < span class =code-string> ChildListPage.aspx
null ,_ userRoles);
base .AddNode(node,_rootNode);
//
node = CreateNode( _ casesNode
Resources.Resource.MenuCases, < span class =code-string> CaseListPage.aspx
null ,_ userRoles);
base .AddNode(node,_rootNode);
//
// 保护程序
//
if (_ currentUser.UserRole == UserRoles.Administrator)
{
node = CreateNode( _ protectorsNode
Resources.Resource.MenuProtectors, null null ,_ userRoles); // ServiceListPage.aspx?Flag = 0
base .AddNode(node,_rootNode);
//
subNode = CreateNode( _ servicesNode
Resources.Resource.MenuServices, < span class =code-string> ServiceListPage.aspx, null ,_ userRoles);
base .AddNode(subNode,node);
//
subNode = CreateNode( _ centersNode
Resources.Resource.MenuCenters, < span class =code-string> CenterListPage.aspx, null ,_ userRoles);
base .AddNode(subNode,node);
//
subNode = CreateNode( _ ongNode
Resources.Resource.MenuONGs, < span class =code-string> ONGListPage.aspx, null ,_ userRoles);
base .AddNode(subNode,node);
//
subNode = CreateNode( _ familiesNode
Resources.Resource.MenuFamilies, < span class =code-string> FamilyListPage.aspx, null ,_ userRoles);
base .AddNode(subNode,node);
}
else
{
node = CreateNode( _ protectorsNode
Resources.Resource.MenuFamilies, FamilyListPage.aspx null ,_ userRoles);
base .AddNode(node,_rootNode);
}
// ...
}


Hii..

I am stuck in one scenario where there are multiple master pages. Here's my question :-

My Website uses Asp.Net default membership provider. I have 3 users and 3 roles.

I have a page called as "RaiseTicket.aspx" this page is accessed by users of all roles. The design of master pages is 2 column layout. Left its the navigation and right is the main content area. All roles have different links on left column. Admin have all links, the other 2 roles have limited links. What I want is the links on the admin page should be visible if admin navigates to any of the page. And if the user navigates to RaiseTicket.aspx page he should be only visible his own links.

Its like 1 page having multiple views of master pages. How should I achieve that. or if you have different view please tell me.

解决方案

u can follow the below link ...


[^]


on the navigation panel give runat="server" attribute and on content page find control by master page and then you can access the control and can hide it.


You could do this by implementing you own a site map provide class (that extend StaticSiteMapProvider) and based on the user roles to add or not links nodes to you master page. The building of your master page links node based on the user roles have to be done by overriding the method BuildSiteMap like in the next example:

/// <summary>
        /// Initializes a new instance of the System.Web.SiteMapNode class using the
        /// specified site map provider that manages the node, URL, title, description,
        /// _userRoles, additional attributes, and explicit and implicit resource keys for
        /// localization.
        /// </summary>
        /// <param name="key">A provider-specific lookup key.</param>
        /// <param name="title">A label for the node, often displayed by navigation controls.</param>
        /// <param name="url">The URL of the page that the node represents within the site.</param>
        /// <param name="description">The node description.</param>
        /// <param name="_userRoles">An System.Collections.IList of _userRoles that are allowed to view the page.</param>
        /// <returns>The new created site map node.</returns>
        private SiteMapNode CreateNode(string key, string title, string url, string description, IList _userRoles)
        {
            return new SiteMapNode(this, key, url, title, description, _userRoles, null, null, null);
        }

        /// <summary>
        /// Add a new site map node node.
        /// </summary>
        /// <param name="node">The site map node.</param>
        /// <param name="parentNode">The parent node.</param>
        protected override void AddNode(SiteMapNode node, SiteMapNode parentNode)
        {
            base.AddNode(node, parentNode);
            _rebuildSiteMap = true;
        }

        /// <summary>
        /// Remove a node from the site map.
        /// </summary>
        /// <param name="node">The node to remove.</param>
        protected override void RemoveNode(SiteMapNode node)
        {
            base.RemoveNode(node);
            _rebuildSiteMap = true;
        }

        /// <summary>
        /// Gets the root node.
        /// </summary>
        /// <returns>The root node.</returns>
        protected override SiteMapNode GetRootNodeCore()
        {
            return BuildSiteMap();
        }

        /// <summary>
        /// Retrieves a Boolean value indicating whether the specified site map node
        /// can be viewed by the current user in the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="node">The site map node.</param>
        /// <returns>True if the node can be viewed by the user, otherwise false.</returns>
        public override bool IsAccessibleToUser(HttpContext context, SiteMapNode node)
        {
            return true;
        }

        /// <summary>
        /// The worker method that build the site map.
        /// </summary>
        /// <returns>The root node.</returns>
        public override SiteMapNode BuildSiteMap()
        {
            if (_rebuildSiteMap)
            {
                BuildMainSiteMap();
                //
                _rebuildSiteMap = false;
            }
            //
            return _rootNode;
        }

        /// <summary>
        /// Get the the root node for the current user.
        /// </summary>
        /// <returns>The root node or null.</returns>
        SiteMapNode GetRootNode()
        {
            if (_currentUser == null)
                return null;
            else if(_currentUser.UserRole == UserRoles.Administrator)
                return CreateNode("_root", Resources.Resource.MenuHome, "UserListPage.aspx?Role=2", null, _userRoles);
            else
                return CreateNode("_root", Resources.Resource.MenuHome, "ComplainListPage.aspx?Type=1", null, _userRoles);
        }

        /// <summary>
        /// The worker method that build the main site map.
        /// </summary>
        private void BuildMainSiteMap()
        {
            _rootNode = GetRootNode();
            if (_rootNode == null)
                return;
            //
            // Add the root node
            //
            base.AddNode(_rootNode, null);
            //
            SiteMapNode node, subNode;
            //
            // Complains, Chilreen, Cases
            //
            node = CreateNode("_complainNode",
                     Resources.Resource.MenuComplains, "ComplainListPage.aspx", null, _userRoles);
            base.AddNode(node, _rootNode);
            //
            node = CreateNode("_childrenNode",
                     Resources.Resource.MenuChildren, "ChildListPage.aspx", null, _userRoles);
            base.AddNode(node, _rootNode);
            //
            node = CreateNode("_casesNode",
                     Resources.Resource.MenuCases, "CaseListPage.aspx", null, _userRoles);
            base.AddNode(node, _rootNode);
            //
            // Protectors
            //
            if (_currentUser.UserRole == UserRoles.Administrator)
            {
                node = CreateNode("_protectorsNode",
                         Resources.Resource.MenuProtectors, null, null, _userRoles); //"ServiceListPage.aspx?Flag=0"
                base.AddNode(node, _rootNode);
                //
                subNode = CreateNode("_servicesNode",
                         Resources.Resource.MenuServices, "ServiceListPage.aspx", null, _userRoles);
                base.AddNode(subNode, node);
                //
                subNode = CreateNode("_centersNode",
                         Resources.Resource.MenuCenters, "CenterListPage.aspx", null, _userRoles);
                base.AddNode(subNode, node);
                //
                subNode = CreateNode("_ongNode",
                         Resources.Resource.MenuONGs, "ONGListPage.aspx", null, _userRoles);
                base.AddNode(subNode, node);
                //
                subNode = CreateNode("_familiesNode",
                         Resources.Resource.MenuFamilies, "FamilyListPage.aspx", null, _userRoles);
                base.AddNode(subNode, node);
            }
            else
            {
                node = CreateNode("_protectorsNode",
                         Resources.Resource.MenuFamilies, "FamilyListPage.aspx", null, _userRoles);
                base.AddNode(node, _rootNode);
            }
            //...
        }


这篇关于Asp.Net Web表单中的母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆