【问题标题】:Add roles column to DataGrid in ASP.NET Web Form在 ASP.NET Web 窗体中将角色列添加到 DataGrid
【发布时间】:2015-11-19 08:23:18
【问题描述】:

我在 ASP.NET 中有一个 Web 表单,我想在其中显示一个包含我的应用程序中所有用户的表格。应该有用户名、用户电子邮件、上次活动日期和用户角色。

目前我有这个:

<asp:DataGrid id="UserGrid" runat="server"
            CellPadding="2" CellSpacing="1"
            Gridlines="Both" AutoGenerateColumns="false">
  <Columns>
    <asp:BoundColumn DataField="UserName" ReadOnly="False" HeaderText="Name" />
    <asp:BoundColumn DataField="Email" ReadOnly="True" HeaderText="Email" />
    <asp:BoundColumn DataField="LastActivityDate" ReadOnly="True" HeaderText="Last activity"/>
</Columns>
<HeaderStyle BackColor="darkblue" ForeColor="white" />

</asp:DataGrid>

_

UserGrid.DataSource = Membership.GetAllUsers();
UserGrid.DataBind();

我想向此 DataGrid 添加列角色。我该怎么做?

在下一步中,我想添加带有按钮的列来编辑用户信息、管理他的角色等。

【问题讨论】:

  • 你的数据库表中有用户角色吗?
  • 是的。我可以为所有用户做:string[] roles = Roles.GetRolesForUser(username);但我不知道如何将此数据添加到 DataGrid 中的新列,或者将这些数据添加到 DataSource?
  • 您的 Membership.GetAllUsers(); 正在获取 UserRoles 对吗?还是在不同的查询中?

标签: c# asp.net datagrid user-roles


【解决方案1】:

如下图所示在你的 GridView 中添加列

 <asp:GridView id="UserGrid" runat="server"
                CellPadding="2" CellSpacing="1"
                Gridlines="Both" AutoGenerateColumns="false">
      <Columns>
        <asp:BoundColumn DataField="UserName" ReadOnly="False" HeaderText="Name" />
        <asp:BoundColumn DataField="Email" ReadOnly="True" HeaderText="Email" />
        <asp:BoundColumn DataField="LastActivityDate" ReadOnly="True" HeaderText="Last activity"/>
        <asp:TemplateField HeaderText="User Roles" ItemStyle-Width="30%">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblrole" runat="server" %>'></asp:Label>
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
    </Columns>
    <HeaderStyle BackColor="darkblue" ForeColor="white" />

    </asp:GridView>

声明一个全局变量来保存您的用户角色。

string[] roles;

在页面加载中获取您的数据

protected void page_load(object sender,EventArgs e)
{
if(!IsPostBack)
{
string[] roles=GetUserRoles();
}
}

在您的 RowDataBound 事件中将数据添加到您的 gridview

    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if(e.Row.RowType= DataControlRowType.DataRow)
      {
        e.Row.FindControl("lblrole").Text=roles[e.Row.RowIndex];
       }
    }

如果数据顺序不匹配,则对两个集合进行排序。这里 DataField 是您的数据库列名或您在查询中使用的别名

【讨论】:

  • 我应该如何为这个网格制作数据源?现在我有: UserGrid.DataSource = Membership.GetAllUsers(); UserGrid.DataBind();而且我不知道如何将有关用户角色的数据信息添加到此数据中,如何将角色添加到 MembershipUserCollection 字段。我的会员资格.GetAllUsers();不包含 UserRoles。
  • Membership.GetAllUsers() 方法必须在数据库上执行一些查询,对吗?检查该查询是否返回用户角色列
  • Membership.GetAllUsers() 是 System.Web.Security (msdn.microsoft.com/en-us/library/dy8swhya%28v=vs.110%29.aspx) 的一种方法,我无权访问其数据库查询。它返回 MembershipUserCollection (msdn.microsoft.com/pl-pl/library/…) 并且没有关于角色的字段。我可能应该调用每个用户:Roles.GetRolesForUser(username) 并以某种方式将此数据添加到我的 DataGrid,但我不知道如何。
  • 检查编辑。我用gridview用datagrid事件改变它
  • 我有 DataGrid 而不是 GridView。 DataGrid 没有 RowDataBound 事件。我想使用 Roles.GetRolesForUser(username) 为不同的用户添加不同的角色 - 在您的解决方案中有一个字符串 - 所有行的角色都相同。
猜你喜欢
  • 2015-06-07
  • 2014-03-11
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
相关资源
最近更新 更多