Flash Games

 FAQ   Search   Members   Groups   Register  User Control Panel      Login 

Your time now:
Mon Nov 23, 2009 2:50 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 1 post ]  Bookmark and Share
Author Message
 Post subject: GridView FindControl returns null when HeaderText is set
PostPosted: Wed Feb 04, 2009 3:45 pm 
Offline

Joined: Wed Feb 04, 2009 3:12 pm
Posts: 1
I have a GridView...

Code:
<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:
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:
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:
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group - Flash Games - TNX Invitation Code - TNX Review

Webmaster - Excruciating - Johnathan - Kotik - Ash - Tomi - rangana - Phate - dflynn - Medley