c# - GridView 行中的动态复选框在 ASP.NET 中未正确显示

标签 c# asp.net gridview

我有一个 gridview,其中我在 gridview 行复选框中显示要选中或取消选中的值。现在我想在 gridview 行中动态地显示这些值,但它不会发生。所有复选框都被选中,而结果应该不同..

这是我的硬编码代码条件,用于显示即将到来的结果......

string[] rolesarr = Roles.GetAllRoles();

    DataTable dTable = new DataTable();
    dTable.Columns.Add("Select", typeof(bool));
    dTable.Columns.Add("Username", typeof(string));
    Array.ForEach(rolesarr, r => dTable.Columns.Add(r, 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();

现在我想让这个条件动态化,我已经为其编写了代码..

string[] rolesarr = Roles.GetAllRoles();

    DataTable dTable = new DataTable();
    dTable.Columns.Add("Select", typeof(bool));
    dTable.Columns.Add("Username", typeof(string));
    Array.ForEach(rolesarr, r => dTable.Columns.Add(r, 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);

        for (int i = 0; i < roles.Length; i++)
        {

            string rol = roles[i];

            for (int j = 2; j < dTable.Columns.Count; j++)
            {
                dRow[j] = roles.Contains(rol) ? true : false;

            }

        }
     dTable.Rows.Add(dRow);
    }
    GridView1.DataSource = dTable;
    GridView1.DataBind();

这是我的复选框 RowDatabound 事件..

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;
    }
}

请大家帮帮我..提前致谢...

最佳答案

问题应该是你提供的双循环。

根据您的硬编码代码,您希望在数组 rolesarr 和用户的 roles 中的角色之间进行映射,以显示为每个用户检查了哪些角色。

要为索引 2 - 4 处的数据行设置值,您将有两个循环,第一个循环重复 rolesarr 数组,第二个循环重复 roles 数组并进行比较他们在第二个循环中

这是我的意思:

string[] rolesarr = Roles.GetAllRoles();

DataTable dTable = new DataTable();

dTable.Columns.Add("Select", typeof(bool));
dTable.Columns.Add("Username", typeof(string));
Array.ForEach(rolesarr, r => dTable.Columns.Add(r, 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);

    for (int i = 0; i < rolesarr.Length; i++)
    {
        for (int j = 0; j < roles.Length; j++)
        {
            if (rolesarr[i] == roles[j])
            {
                dRow[i + 2] = true;
                break;
            }
        }
    }
    dTable.Rows.Add(dRow);
}
GridView1.DataSource = dTable;
GridView1.DataBind(); 

请注意,我在第二个循环中使用数据行索引作为 i + 2 (dRow[i + 2]),因为您的角色列从 index= 开始2,而不是 0,并且长度必须等于 rolesarr.Length,您将它们用作角色列。

关于c# - GridView 行中的动态复选框在 ASP.NET 中未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18952202/

相关文章:

c# - 在mvc中清除浏览器缓存中的对象

c# - 如何实现单机单实例应用?

ASP.NET 路由 : RouteCollection class missing

css - ImageResizer、azure cdn、缓存清除和 url 重写

android - GridView 在 notifyDataSetChanged() 之后没有更新

c# - Gridview动态添加新行

c# - 在新的Thread()中等待是否毫无意义?

c# - 在 ASP.NET MVC 中呈现 View 与呈现 HTML 文件之间的性能差异是什么?

android - 按下 GridView 中的按钮时如何更改黄色方形背景?

c# - 如何向用户报告标准异常?