locked
Changing CSS of GridView based on user role RRS feed

  • Question

  • User-1168517927 posted

    I have a requirement to display the List.aspx GridView in grey (for example) when a user isn't in the role needed for data editing.  I have been Googling all day, and haven't found a solution yet.  I understand the IAutoFieldGenerator interface with the GenerateFields() method can be used to cycle through each column.  When I tried that approach, the GenerateFields() method never fired.  My application is .NET 4.5, and I think that approach is for an older version of dynamic data.

    I want to decorate each column with something like:

    [RequiresAuthorization(Role="Admin")]

    Then, as the mark-up is being generated, compare the user's role to the one applied to the column.

    I can think of a way to do this, but it isn't very "dynamic data", and it requires modifying List.aspx.cs, which I want to keep to a minimum.  Please help.

    Friday, July 18, 2014 5:35 PM

Answers

  • User-183147428 posted

    The entire gridview needs a style change based on the user's role? If so, couldn't you just check their role before loading the gridview and assign the proper css class? For example, in your css, you could have a gridview-admin class and a gridview-nonadmin class, or something of the sort. In your cs code (if willing to modify it), you'd have something like:

    if (role == "Admin")
    {
        GridView1.CssClass = "gridview-admin";
    }
    else
    {
        GridView1.CssClass = "gridview-nonadmin";
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 18, 2014 5:43 PM
  • User-330204900 posted

    Hi bspeake, you have to use the auto field generator in the Page_Load event. also you may want to look at my articles here:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 20, 2014 7:57 AM

All replies

  • User-183147428 posted

    The entire gridview needs a style change based on the user's role? If so, couldn't you just check their role before loading the gridview and assign the proper css class? For example, in your css, you could have a gridview-admin class and a gridview-nonadmin class, or something of the sort. In your cs code (if willing to modify it), you'd have something like:

    if (role == "Admin")
    {
        GridView1.CssClass = "gridview-admin";
    }
    else
    {
        GridView1.CssClass = "gridview-nonadmin";
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 18, 2014 5:43 PM
  • User-330204900 posted

    Hi bspeake, you have to use the auto field generator in the Page_Load event. also you may want to look at my articles here:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 20, 2014 7:57 AM