根据角色

时间:2016-11-04 14:12:50

标签: c# asp.net

希望能够将“编辑”链接按钮设置为visible = false,除非用户具有“编辑”角色。

一直在寻找stackoverflow和其他地方,到目前为止还没有能够让它工作。

的GridView:

<asp:GridView ID="GridView1" runat="server" Caption="Questions Awaiting Review" AllowSorting="True" PagerSettings-Mode="NumericFirstLast" 
    OnPageIndexChanging="GridView1_PageIndexChanging" CaptionAlign="Top" EmptyDataText="No Questions Pending Review." 
    PageSize="10" AllowPaging="true" PagerStyle-HorizontalAlign="Center" PagerStyle-Font-Size="Large" DataKeyNames="QuestionID"
    OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="#cccccc"
    OnPreRender="GridView1_OnPreRender">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:Label ID="QuestionID" runat="server" Text='<%# Eval("QuestionID") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="KeyObjective" HeaderText="Key Objective" ItemStyle-Width="250" />
    <asp:BoundField DataField="SubmitDate" HeaderText="Submitted Date" ItemStyle-Width="60" />
    <asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="Details" CommandArgument='<%# Eval("QuestionID") %>' runat="server" CommandName="viewQuestion">View Question</asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
     <ItemTemplate>
        <asp:LinkButton ID="Edit" CommandArgument='<%# Eval("QuestionID") %>' runat="server" CommandName="editQuestion">Edit Question</asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>
</Columns>

</asp:GridView>

更改后面的代码以使用OnPreRender作为gridview,如果值是硬编码的,则隐藏列。但是,当我尝试检索编辑器的角色时,该值似乎没有正确评估。即使用户具有编辑角色,也始终返回false。

protected void GridView1_OnPreRender(object sender, EventArgs e)
    {
        if (Roles.IsUserInRole("Editor"))
        {
            // Enter correct column index.
            GridView1.Columns[4].Visible = true;
        }
        else
        {
            GridView1.Columns[4].Visible = false;
        }
    }

希望我遗漏一些简单的东西,对asp.net来说不是新鲜事。

4 个答案:

答案 0 :(得分:1)

隐藏上一栏。

this.GridView1.Columns[this.GridView1.Columns.Count - 1].Visible = Roles.IsUserInRole("Editor");

答案 1 :(得分:0)

像这样使用LinkBut​​ton,并在代码后面的函数中设置Visibility属性。

<asp:LinkButton ID="Edit" Visible='<%# ShowEditBasedOnRole() %>' CommandArgument='<%# Eval("QuestionID") %>' runat="server" CommandName="editQuestion">Edit Question</asp:LinkButton>

然后在返回bool

的函数后面的代码中
public bool ShowEditBasedOnRole()
{
    if (Roles.IsUserInRole("Editor"))
    {
        return true;
    }
    else
    {
        return false;
    }
}

答案 2 :(得分:0)

您希望显示/隐藏整个列而不是 LinkBut​​ton 控件。否则, 未经授权的用户将始终看到一个空格为 的列,这是奇怪的。

以下示例将隐藏整个列。

屏幕截图(授权与未经授权)

enter image description here

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DemoWebForm.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1"
            runat="server"
            DataKeyNames="QuestionID"
            OnPreRender="GridView1_OnPreRender"
            AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="QuestionID" runat="server" Text='<%# Eval("QuestionID") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="KeyObjective" HeaderText="Key Objective" ItemStyle-Width="250" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="Edit" CommandArgument='<%# Eval("QuestionID") %>'
                            runat="server" CommandName="editQuestion">Edit Question</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </form>
</body>
</html>

代码背后

public class Question
{
    public int QuestionID { get; set; }
    public string KeyObjective { get; set; }
}

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = new List<Question>
            {
                new Question {QuestionID = 1, KeyObjective = "One"},
                new Question {QuestionID = 2, KeyObjective = "Two"},
                new Question {QuestionID = 3, KeyObjective = "Three"},
            };
            GridView1.DataBind();
        }
    }

    protected void GridView1_OnPreRender(object sender, EventArgs e)
    {
        bool isEditor = true; // Business logic here
        if (isEditor)
        {
            // Enter correct column index.
            GridView1.Columns[2].Visible = false;
        }
    }

}

答案 3 :(得分:0)

一次快速修改,而不是按索引访问列。可以使用标题文本来访问它,即使将来在代码片段中将新列插入到被访问的列之前,也不会影响代码。

protected void grdResults_OnPreRender(object sender, EventArgs e)
{
    TemplateField FieldToAccess= grdResults.Columns.OfType<TemplateField> 
                                 ().Where(f => f.HeaderText == 
                                 "ValidityDate").FirstOrDefault();
    if (role)
    FieldToAccess.Visible = false;
}