简体   繁体   English

ASP.NET成员资格和GridViews

[英]ASP.NET Membership & GridViews

I have a site using ASP.NET Membership and have a number of roles that users are assigned to. 我有一个使用ASP.NET成员资格的网站,并且有许多角色分配给用户。 One feature I really want to do is to be able to programatically add the "edit" or "delete" buttons to rows in a GridView, based on the role the user is a member of. 我真正想做的一项功能是能够根据用户所属的角色,以编程方式向GridView中的行添加“编辑”或“删除”按钮。 For example: 例如:

If the user is an admin I want to show the edit and delete buttons on the gridview, but if they are an editor, to just show the edit button. 如果用户是管理员,我想在gridview上显示“编辑”和“删除”按钮,但是如果他们是编辑器,则只显示“编辑”按钮。 I know this is possible as I have seen an example done before, but can't for the life of me find it in my bookmarks! 我知道这是有可能的,因为我已经看过一个示例,但是我一生无法在书签中找到它!

Thanks in advance. 提前致谢。

Handle the " RowDataBound " event for your gridview, and inside your event handler, check the user's credentials and set the appropriate visibility on your buttons. 处理gridview的“ RowDataBound ”事件,并在事件处理程序内部,检查用户的凭据并在按钮上设置适当的可见性。

void MyGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{  
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Check user credentials and set button visibility
      e.Row.Cells[x].Controls[y].Visible = true; //or false
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM