如何根据用户角色设置控件的可见性? [英] how to set the visibility of control according to user role ?

查看:56
本文介绍了如何根据用户角色设置控件的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!!!
我有2个角色admin和user.他们俩都可以访问同一页面(default.aspx).此页面具有带有update和delete按钮的gridview.我想做的是禁用删除和编辑按钮,或者如果用户处于"USER"角色则将其隐藏.

请告诉我该怎么做.

Hello!!!
I have 2 roles admin and user. They both have access to same page (default.aspx) This page has a gridview with update and delete button. what i want to do is disable the delete and edit button or hide them if user is in role "USER".

Please tell me how to do this.

推荐答案

在查看页面时,如果当前用户是管理员或用户,则必须具有该信息.根据此信息,您将设置控件的可见性.

使用grid的RowDataBound 方法设置可见性.
While viewing the page, you must be having the information if the current user is an admin or user. Based on this information, you will set the visibility of the control.

Use grid''s RowDataBound method to set the visiblity.
Protected Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        Dim rtype As DataControlRowType = e.Row.RowType
        If ((rtype = DataControlRowType.DataRow)  _
                    AndAlso ((rtype <> DataControlRowType.Footer)  _
                    AndAlso ((rtype <> DataControlRowType.Separator)  _
                    AndAlso ((rtype <> DataControlRowType.Header)  _
                    AndAlso (rtype <> DataControlRowType.Pager))))) Then
            ' Get the delete button 
            ' Use Role
            ' Disable if not Admin
            If (currentRole <> "Admin") Then
                Dim btn As Button = CType(e.Row.FindControl("DeleteButton1"),Button)
                btn.Enabled = false
            End If
        End If
    End Sub


试试吧!


Try!


这篇关于如何根据用户角色设置控件的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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