使用 ASP.NET GridView 控件,如何在 PageIndexChanged 后禁用 GridViewRow 中的控件或单个单元格?

发布于 2024-09-28 04:34:52 字数 712 浏览 2 评论 0原文

我有一个数据绑定 GridView 控件,在其中我可以根据用户角色禁用各个单元格。这只适用于第一页。

private void LimitAccessToGridFields()
    {
        if (User.IsInRole("Processing")) return;

        foreach (GridViewRow gridViewRow in gvScrubbed.Rows)
        {
            var checkBox = ((CheckBox) gridViewRow.FindControl("cbScrubbed"));
            checkBox.Enabled = false;

            // ButtonField does not have an ID to FindControl with
            // Must use hard-coded Cell index
            gridViewRow.Cells[1].Enabled = false; 
        }
    }

我在 Page_Load 上调用这个方法,它在那里工作。我已经在 PageIndexChaging 和 PageIndexChanged 事件处理程序中尝试过它,但它不起作用。调试时,似乎已成功将行中两个控件的 Enabled 设置为 false。我的目标是在更改页面后根据用户角色禁用这些字段。这应该如何实现?

I have a data bound GridView control, in which I am able to disable individual cells based on the User role. This only works on the first page.

private void LimitAccessToGridFields()
    {
        if (User.IsInRole("Processing")) return;

        foreach (GridViewRow gridViewRow in gvScrubbed.Rows)
        {
            var checkBox = ((CheckBox) gridViewRow.FindControl("cbScrubbed"));
            checkBox.Enabled = false;

            // ButtonField does not have an ID to FindControl with
            // Must use hard-coded Cell index
            gridViewRow.Cells[1].Enabled = false; 
        }
    }

I call this method on Page_Load, where it works. I've tried it in the PageIndexChaging and PageIndexChanged event handlers, where it doesn't work. While debugging, it appears to successfully set Enabled to false in both of the controls in the row. My goal is to disable these fields, depending on user role, after changing the page. How should this be accomplished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尾戒 2024-10-05 04:34:52

您不需要遍历任何控件来禁用或隐藏/可见它们。

GridView 控件中的每个单元格在呈现时实际上都是一个 HTML 表引用(使用 FireFly 或 Inspector 查看页面中的代码)。

那么为什么不迭代所有单元格以及每个单元格内找到的任何控件,只是禁用它们呢?或者您可以简单地循环遍历 GridView 的每一行并直接禁用或隐藏它,这将影响行内的所有内容。

使用表格单元格隐藏参考示例:

foreach (GridViewRow gRow in myGridView.Rows)
            {
                if (gRow.RowType == DataControlRowType.DataRow)
                {
                        TableCellCollection tbcCol = (TableCellCollection)gRow.Cells;
                        foreach (TableCell tblCell in tbcCol)
                                tblCell.Enabled = false;
                }
            }

这样将逐个表格单元格禁用所有表格单元格。

或者..为什么不禁用整行呢?

foreach (GridViewRow gRow in myGridView.Rows)
            {
                if (gRow.RowType == DataControlRowType.DataRow)
                   gRow.Enable = false;
            }

如果您需要精确定位或过滤特定的控件类型(CheckBox、TextBox、Label 等)并且仅影响这些控件,那么只需对它们进行测试即可!

foreach (GridViewRow gRow in myGridView.Rows)
{
  if (gRow.RowType == DataControlRowType.DataRow)
  {
     TableCellCollection tbcCol = (TableCellCollection)gRow.Cells;
     foreach (TableCell tblCell in tbcCol)
         if (((TextBox)tblCell) != null)
             ((TextBox)tblCell).Enable = false;
  }
}

You do not need to iterate through any controls to disable or hide/visible them.

Every cell in a GridView control is actually a HTML table reference, when rendered (look at the code in your page using FireFly or Inspector).

So why not iterate through all the cells, and any controls found within each cell, just disable them? Or you can simply loop through each row of your GridView and disable or hide it directly, which will affect everything inside the row.

Hiding using a Table Cell reference example:

foreach (GridViewRow gRow in myGridView.Rows)
            {
                if (gRow.RowType == DataControlRowType.DataRow)
                {
                        TableCellCollection tbcCol = (TableCellCollection)gRow.Cells;
                        foreach (TableCell tblCell in tbcCol)
                                tblCell.Enabled = false;
                }
            }

So that will disable everything table cell by table cell.

OR.. why not just disable the entire row?

foreach (GridViewRow gRow in myGridView.Rows)
            {
                if (gRow.RowType == DataControlRowType.DataRow)
                   gRow.Enable = false;
            }

If you need to pin-point or filter particular control types (CheckBox, TextBox, Label, etc) and only affect those controls then simply test for them!

foreach (GridViewRow gRow in myGridView.Rows)
{
  if (gRow.RowType == DataControlRowType.DataRow)
  {
     TableCellCollection tbcCol = (TableCellCollection)gRow.Cells;
     foreach (TableCell tblCell in tbcCol)
         if (((TextBox)tblCell) != null)
             ((TextBox)tblCell).Enable = false;
  }
}
浅听莫相离 2024-10-05 04:34:52

我发现这必须在 RowDataBound 事件处理程序中完成。

if (e.Row.RowType == DataControlRowType.DataRow)
{
   // details elided ...

   // Limits the access to grid fields.
   if (!User.IsInRole("PROCESSING"))
   {
       cbstuff.Enabled = false; // a checkbox
       e.Row.Cells[1].Enabled = false; //a link button
   }
}

I found that this must be done in the RowDataBound event handler.

if (e.Row.RowType == DataControlRowType.DataRow)
{
   // details elided ...

   // Limits the access to grid fields.
   if (!User.IsInRole("PROCESSING"))
   {
       cbstuff.Enabled = false; // a checkbox
       e.Row.Cells[1].Enabled = false; //a link button
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文