It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming ASP & ASP.NET Forum

GridView FindControl returns null when HeaderText is set

GridView FindControl returns null when HeaderText is set

Postby fitruss on Wed Feb 04, 2009 2:45 pm

I have a GridView...

Code: Select all
<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...

Code: Select all
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...

Code: Select all
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...

Code: Select all
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.

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.

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

Can anyone see something im missing here or has anyone experienced a similar issue?
fitruss
 
Posts: 1
Joined: Wed Feb 04, 2009 2:12 pm

Who is online

Users browsing this forum: No registered users and 0 guests