设置 HeaderText 时 GridView FindControl 返回 null

发布于 2024-07-13 13:33:36 字数 3965 浏览 6 评论 0原文

我有一个 GridView...

<asp:GridView EnableViewState="true" 
                ID="grdResults" 
                runat="server" 
                CssClass="resultsGrid" 
                OnRowDataBound="grdResults_OnRowDataBound" 
                AutoGenerateColumns="false" 
                HeaderStyle-CssClass="header" 
                OnRowCommand="grdResults_OnRowCommand">
    <Columns>
       <asp:TemplateField>
           <ItemTemplate>
               <asp:Label ID="lblView" 
                           runat="server" 
                           Visible="false" 
                           Text="View">
               </asp:Label>
               <asp:HyperLink ID="hypEdit" 
                               runat="server" 
                               Visible="false" 
                               Text="(Edit)" 
                               CssClass="edit">
               </asp:HyperLink>
               <asp:LinkButton ID="btnDelete" 
                               runat="server" 
                               Visible="false" 
                               Text="(Delete)" 
                               CssClass="delete" 
                               CommandName="DeleteItem" 
                               OnClientClick="return confirm('Are you sure you want to delete?')">
               </asp:LinkButton>
               <asp:HyperLink ID="hypSelect" 
                               runat="server" 
                               Visible="false" 
                               Text="(Select)" 
                               CssClass="select">
               </asp:HyperLink>
           </ItemTemplate>
       </asp:TemplateField>
   </Columns>
</asp:GridView>

它有一个静态列,其中包含一个标签、两个超链接和一个链接按钮,并且还有许多动态生成的列...

private void SetupColumnStructure(IEnumerable<string> columnNames)
{
    var columnNumber = 0;
    foreach (var columnName in columnNames)
    {
        var templateColumn = new TemplateField
                                 {
                                     ItemTemplate = new CellTemplate(columnName)
                                 };
       grdResults.Columns.Insert(columnNumber, templateColumn);
       columnNumber++;
    }
}

作为 OnRowDataBound 处理程序的一部分,我检索静态列中的控件之一并设置一些属性...

protected void grdResults_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    .
    .
    .
    var row = e.Row;
    var rowData = row.DataItem as Dictionary<string, object>;
    if (rowData != null)
    {
       if ((bool)rowData[displayEditLink])
       {
           var hypEdit = (HyperLink)row.FindControl("hypEdit");
           hypEdit.NavigateUrl = "~/Pages/Edit.aspx?action=Edit&objectType=" + rowData[objectTypeLiteral] + "&id=" + rowData[objectIdLiteral];
           hypEdit.Visible = true;
       }
   }
   .
   .
   .
}

这一切都很好,但没有显示列名。 因此,我然后修改 SetupColumnStructure 方法,以便在模板字段上设置 HeaderText,如下所示...

private void SetupColumnStructure(IEnumerable<string> columnNames)
{
    var columnNumber = 0;
    foreach (var columnName in columnNames)
    {
        var templateColumn = new TemplateField
                                 {
                                     ItemTemplate = new CellTemplate(columnName),
                                     HeaderText = columnName
                                };
       grdResults.Columns.Insert(columnNumber, templateColumn);
       columnNumber++;
    }
}

出于某种原因,这一额外的行更改会导致 row.FindControl("hypEdit"); 调用 OnRowDataBound 处理程序返回 null。有人能看到我在这里缺少的东西吗?或者有人遇到过类似的问题吗?

更新

我已确保此处不是指页眉或页脚行。 另外,如果我跳过对象引用异常,数据源中的每个项目都会发生这种情况。

不确定这是否有帮助,但正如我预期的那样,当我单步执行代码时,表已生成所有预期的列,但在设置 HeaderText 时,所有单元格 (DataControlFieldCells) 均不包含控件,但在未设置时,则包含所有预期的控件。

一切都很奇怪。 如果您还能发现其他任何东西,请告诉我。

I have a GridView...

<asp:GridView EnableViewState="true" 
                ID="grdResults" 
                runat="server" 
                CssClass="resultsGrid" 
                OnRowDataBound="grdResults_OnRowDataBound" 
                AutoGenerateColumns="false" 
                HeaderStyle-CssClass="header" 
                OnRowCommand="grdResults_OnRowCommand">
    <Columns>
       <asp:TemplateField>
           <ItemTemplate>
               <asp:Label ID="lblView" 
                           runat="server" 
                           Visible="false" 
                           Text="View">
               </asp:Label>
               <asp:HyperLink ID="hypEdit" 
                               runat="server" 
                               Visible="false" 
                               Text="(Edit)" 
                               CssClass="edit">
               </asp:HyperLink>
               <asp:LinkButton ID="btnDelete" 
                               runat="server" 
                               Visible="false" 
                               Text="(Delete)" 
                               CssClass="delete" 
                               CommandName="DeleteItem" 
                               OnClientClick="return confirm('Are you sure you want to delete?')">
               </asp:LinkButton>
               <asp:HyperLink ID="hypSelect" 
                               runat="server" 
                               Visible="false" 
                               Text="(Select)" 
                               CssClass="select">
               </asp:HyperLink>
           </ItemTemplate>
       </asp:TemplateField>
   </Columns>
</asp:GridView>

This has one static column containing a label two hyperlinks and a link button and also has a number of dynamically generated columns...

private void SetupColumnStructure(IEnumerable<string> columnNames)
{
    var columnNumber = 0;
    foreach (var columnName in columnNames)
    {
        var templateColumn = new TemplateField
                                 {
                                     ItemTemplate = new CellTemplate(columnName)
                                 };
       grdResults.Columns.Insert(columnNumber, templateColumn);
       columnNumber++;
    }
}

As part of the OnRowDataBound handler I retrieve one of the controls in the statically column and set some attributes on it...

protected void grdResults_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    .
    .
    .
    var row = e.Row;
    var rowData = row.DataItem as Dictionary<string, object>;
    if (rowData != null)
    {
       if ((bool)rowData[displayEditLink])
       {
           var hypEdit = (HyperLink)row.FindControl("hypEdit");
           hypEdit.NavigateUrl = "~/Pages/Edit.aspx?action=Edit&objectType=" + rowData[objectTypeLiteral] + "&id=" + rowData[objectIdLiteral];
           hypEdit.Visible = true;
       }
   }
   .
   .
   .
}

This all works fine but no column names are displayed. So I then modify the SetupColumnStructure method so that the HeaderText is set on the template field like this...

private void SetupColumnStructure(IEnumerable<string> columnNames)
{
    var columnNumber = 0;
    foreach (var columnName in columnNames)
    {
        var templateColumn = new TemplateField
                                 {
                                     ItemTemplate = new CellTemplate(columnName),
                                     HeaderText = columnName
                                };
       grdResults.Columns.Insert(columnNumber, templateColumn);
       columnNumber++;
    }
}

For some reason this one extra line change causes the row.FindControl("hypEdit"); call in the OnRowDataBound handler to return null.Can anyone see something im missing here or has anyone experienced a similar issue?

UPDATE

I've made sure that I'm not referring to a header or footer row here. Also, if I step over the object reference exception this occurs for every item that is in the DataSource.

Not sure if this helps, but as I expected, when I stepped through the code the table has generated all the columns expected but all cells (DataControlFieldCells) contain no controls when the HeaderText is set, yet all expected controls when it isnt set.

All very strange. Let me know if you can spot anything else.

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

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

发布评论

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

评论(2

无悔心 2024-07-20 13:33:37

当您添加 HeaderText 时,一个新的 RowType 被添加到 gridview 中。 您需要检查引发 OnRowDataBound 事件的行类型并采取适当的操作。 在您的情况下,只需检查 e.Row.RowType 是否是 DataRow 就可以解决您的问题:

protected void grdResults_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
     if(e.Row.RowType == DataControlRowType.DataRow)
     {
          if ((bool)rowData[displayEditLink])
          {
           var hypEdit = (HyperLink)row.FindControl("hypEdit");
           hypEdit.NavigateUrl = "~/Pages/Edit.aspx?action=Edit&objectType=" + rowData[objectTypeLiteral] + "&id=" + rowData[objectIdLiteral];
           hypEdit.Visible = true;
          }
     }
}

When you added the HeaderText, a new RowType was added to the gridview. You'll need to check what type of row raised the OnRowDataBound event and take the appropriate action. In your case, just checking if the e.Row.RowType is a DataRow should solve your problem:

protected void grdResults_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
     if(e.Row.RowType == DataControlRowType.DataRow)
     {
          if ((bool)rowData[displayEditLink])
          {
           var hypEdit = (HyperLink)row.FindControl("hypEdit");
           hypEdit.NavigateUrl = "~/Pages/Edit.aspx?action=Edit&objectType=" + rowData[objectTypeLiteral] + "&id=" + rowData[objectIdLiteral];
           hypEdit.Visible = true;
          }
     }
}
薄荷→糖丶微凉 2024-07-20 13:33:37

这是因为您正在搜索的控件包含在另一个控件中。 FindControl() 不会查找控件的控件集合。 您将需要编写一个 recursiveFindControl() 方法。

希望这有所帮助!

Its because the control you are searching for is contained within another control. FindControl() does not look inside control collections of controls. You will need to write a recursiveFindControl() method.

Hope this helps a little!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文