【问题标题】:How to refer to a Cell in a ASP.NET GridView without using its index?如何在不使用索引的情况下引用 ASP.NET GridView 中的单元格?
【发布时间】:2012-03-18 06:58:01
【问题描述】:

我想知道是否有一种方法可以在 Asp.Net GridView 中按名称或 Id 来识别和引用单元格。

就我而言,我有这种情况,我想访问 TemplateField 中的 ImageButton 控件。

我的代码是这样的:

 ImageButton ibtStatus = (ImageButton)e.Row.Cells[8].FindControl(ibtIDStatus);

但我想像这样引用单元格:

 ImageButton ibtStatus = (ImageButton)e.Row.Cells["MyCell"].FindControl(ibtIDStatus);

如何通过名称、HeaderText 或任何其他不通过其索引的方式引用 Cell?

已编辑:我使用的是 Visual Studio 2005 和 .NET 2.0。

【问题讨论】:

  • 为什么要在特定单元格中查找控件?您可以通过搜索 Row 元素 (e.Row.FindControl("yourControlID")) 获得相同的结果。
  • 哼...我想我对我的例子不满意。我也想隐藏一些特定的Cell,比如e.Row.Cells[4].Visible = false怎么做?

标签: c# asp.net datagrid .net-2.0


【解决方案1】:

您应该能够完全绕过单元格索引并且毫无问题地执行此操作。

ImageButton ibtStatus = (ImageButton)e.Row.FindControl(ibtIDStatus);

【讨论】:

    【解决方案2】:

    我相信您是在 GridView_RowDataBound 事件中编写此代码的......对吗?? ?

    编写代码的更好方法是。

    ImageButton ibtStatus  = e.Row.FindControl("ibtIDStatus") as ImageButton;
    if(ibtStatus !=null)
    {
    // Write your code here.
    }
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-10-30
      • 2013-07-17
      • 2015-09-21
      • 2021-09-15
      • 2011-04-21
      • 1970-01-01
      • 2011-03-11
      • 2022-01-21
      • 1970-01-01
      相关资源
      最近更新 更多