如何动态地将Gridview中的成员资格角色显示为列

时间:2013-09-20 09:08:09

标签: asp.net gridview asp.net-membership

我正在使用Membership来定义Asp.net.Now中的角色。根据我的要求,我需要将角色显示为gridview的动态列,前两列为固定列,其余列为表中的角色数但是我不知道如何满足这一点,因为我以前从未使用过它。

这是我在aspx页面中的静态gridview代码...

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
                    AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan"
                    BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" 
                    Width="477px">
                    <AlternatingRowStyle BackColor="PaleGoldenrod" />
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:CheckBox ID="chkhdr" runat="server" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="chkChild" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Username">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("col0") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Role(Admin)">
                            <ItemTemplate>
                                <asp:CheckBox ID="chkAdmin" runat="server" Checked='<%# Eval("col1") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Role(DPAO User )">
                            <ItemTemplate>
                                <asp:CheckBox ID="chkUser" runat="server" Checked='<%# Eval("col2") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Role(GeneralUser)">
                            <ItemTemplate>
                                <asp:CheckBox ID="chkgen" runat="server" Checked='<%# Eval("col3") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>   
                </asp:GridView>

以下是使用数据加载网格的代码......

protected void BindGridviewData()
{

    var role = from MembershipUser u in Membership.GetAllUsers()
               select new
               {
                   User = u.UserName,
                   Role = string.Join(",", Roles.GetRolesForUser(u.UserName))
               };


    DataTable dTable = new DataTable();
    dTable.Columns.Add("col0", typeof(string));
    dTable.Columns.Add("col1", typeof(bool));
    dTable.Columns.Add("col2", typeof(bool));
    dTable.Columns.Add("col3", typeof(bool));
    foreach (MembershipUser u in Membership.GetAllUsers())
    {
        DataRow dRow = dTable.NewRow();
        dRow[0] = u.UserName;

        string[] roles = Roles.GetRolesForUser(u.UserName);
        dRow[1] = roles.Contains("Admin") ? true : false;
        dRow[2] = roles.Contains("DPAO User") ? true : false;
        dRow[3] = roles.Contains("GeneralUser") ? true : false;
        dTable.Rows.Add(dRow);
    }
    GridView1.DataSource = dTable;
    GridView1.DataBind();
}

在给定的网格中,我需要前两列作为固定列,其他列基于来自成员资格的Role表中的值...

任何帮助都将受到高度赞赏。谢谢提前..

1 个答案:

答案 0 :(得分:1)

对于动态显示更新角色删除记录,请尝试以下操作:

ASPX:

<asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound1"/></asp:GridView>
<asp:Button ID="cmdDelete" runat="server" onclick="cmdDelete_Click1" Text="Delete" />
<asp:Button ID="cmdUpdateRole" runat="server" onclick="cmdUpdateRole_Click" Text="Update Roles" />

代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridviewData();
    }       
}
protected void BindGridviewData()
{
    DataTable dTable = new DataTable();
    dTable.Columns.Add("Select", typeof(bool));
    dTable.Columns.Add("Username", typeof(string));
    dTable.Columns.Add("Role(Admin)", typeof(bool));
    dTable.Columns.Add("Role(DPAO User)", typeof(bool));
    dTable.Columns.Add("Role(GeneralUser)", typeof(bool));
    foreach (MembershipUser u in Membership.GetAllUsers())
    {
        DataRow dRow = dTable.NewRow();
        dRow[0] = false;
        dRow[1] = u.UserName;
        string[] roles = Roles.GetRolesForUser(u.UserName);
        dRow[2] = roles.Contains("Admin") ? true : false;
        dRow[3] = roles.Contains("DPAO User") ? true : false;
        dRow[4] = roles.Contains("GeneralUser") ? true : false;
        dTable.Rows.Add(dRow);
    }
    GridView1.DataSource = dTable;
    GridView1.DataBind();
}   
protected void cmdUpdateRole_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        string username = row.Cells[1].Text;
        CheckBox chkAdmin = (CheckBox)row.Cells[2].Controls[0];
        CheckBox chkUser = (CheckBox)row.Cells[3].Controls[0];
        CheckBox chkgen = (CheckBox)row.Cells[4].Controls[0];         
        List<string> roles=new List<string>();   
        if (chkAdmin.Checked)
            roles.Add("Admin");
        if (chkUser.Checked)
            roles.Add("DPAO User");
        if (chkgen.Checked)
            roles.Add("GeneralUser");
        if (Roles.GetRolesForUser(username).Length > 0)
        {
            Roles.RemoveUserFromRoles(username, Roles.GetRolesForUser(username));
        }
        if (roles.Count > 0)
        {
            Roles.AddUserToRoles(username, roles.ToArray());
        }
        BindGridviewData();
    }
}
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox c0 = (CheckBox)e.Row.Cells[0].Controls[0];
        CheckBox c2 = (CheckBox)e.Row.Cells[2].Controls[0];
        CheckBox c3 = (CheckBox)e.Row.Cells[3].Controls[0];
        CheckBox c4 = (CheckBox)e.Row.Cells[4].Controls[0];
       c0.Enabled=c2.Enabled =c3.Enabled=c4.Enabled= true;
    }
}
protected void cmdDelete_Click1(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox chk = (CheckBox)row.Cells[0].Controls[0];
        if (chk.Checked)
        {
            string username = row.Cells[1].Text;
            Membership.DeleteUser(username);
            BindGridviewData();
        }
    }
}