开发者

How can I display a gridview column only to users in a certain role?

I have an asp.net page with a gridview. I would like to make it so that certain columns i开发者_如何转开发n the gridview are displayed only if the user is in a certain role, that is, if HttpContext.Current.User.IsInRole(XXX) returns true.

What is the easiest way to do this?

I would prefer a solution where I can easily put this in my ASP code and not in the page code-behind.


I used this technique in one of my sites.

If Roles.IsUserInRole(Profile.UserName, "This Role") Then
    productGridview.Columns(2).Visible = False
End If

Of course, in this case 2 represents the index of the column to hide.


I can think of a few options here, although some are of a hack-y variety. You could:

  1. Set a JavaScript variable to the results of your HttpContext.Current.User.IsInRole(xxx) function, and then remove the offending controls by jQuery. I've done something similar using the `$("control_type[id$=]") selector.
  2. Use the FindControl("ID") function to get the controls you want to hide out of each of your grids, and set Visible = false on each of them.
  3. Create two separate GridView aspx pages, one with all the columns, and one with a restricted set of columns. You could then use a <div> tag as a place holder and call the jQuery load() function (with references to the appropriate aspx page) based on the results of IsInRole().

If you're auto-generating your columns, maybe you could just pass the results of IsInRole() to the class constructor/function you're using, and hide the columns that way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜