Conditional Footnotes On GridView

hand-079 A couple of days ago I ran into a requirement to conditionally display footnote information at the bottom of a GridView display.  We were already conditionally displaying icons in the row.  What we needed to do was to conditionally show text at the bottom of the screen that explained what each icon was for.  Here’s how we went about it.

The first thing we did was to make all of the footnotes invisible.  We will only make them visible if the associated icon is visible.  So we need some way of determining if the icon is visible, and if it is we need to make the associated footnote visible.

First, we find out if the icon is visible.

There is an event that fires every time a row is created in the GridView control.  The event name is RowCreated.  This event sends the sender object and a GridViewRowEventArgs object down to the handler.  And this is where the magic starts.

There are two ways of finding a control in the row.  You can go after it using:

e.Row.Cells[cellControlIsIn].Controls[indexOfTheControl];

Which you can then cast to the type of control you are expecting and check the Visible property to see if it is visible.

The other thing you can do is recursively look for the control by its ID by iterating through the Controls collection of each control in the Row.

We’ve written a utility function that does all the work:

public static Control
    FindControlByID(string id,
    ControlCollection controls)
{
    Control returnControl = null;
    foreach (Control c in controls)
    {
        if (c.ID == id)
            return c;

        returnControl =
            FindControlByID(id, c.Controls);
        if (returnControl != null)
            return returnControl;
    }
    return returnControl;
}

If you want the absolute fastest code, you’ll use the first method.  If you just want to get the code working and out the door, you can use the second method.

Once we know that the control is visible, we can set the footnote visible in the event handler and we are done.

Related Post

  • GridViews – Multiple Rows Per RecordGridViews – Multiple Rows Per Record I don't know about you, but I've had several occasions where I've needed to use the simplicity of the GridView control in ASP.NET.  DataBinding, Paging, Sorting, etc.  But I've also wanted to stac...
  • Editing in a GridView without switching to Edit mode.Editing in a GridView without switching to Edit mode. I just saw a question about this yesterday and realized that while I know how to do this, not everyone does.  So, here we go... Here's the problem.  You want to be able to edit all of the rows i...
  • Sharing ObjectDataSource From CodebehindSharing ObjectDataSource From Codebehind I ran into an interesting “problem” last week that wasn’t all that hard to figure out once I broke out the debugger.  But I thought it would be useful to share the results to save others th...
  • GridView and Updating A Row ManuallyGridView and Updating A Row Manually A couple of days ago I mentioned a project that I’ve been working on that is a bit out of the ordinary as far as GridViews go.  One of the issues I’ve had is that the edit template doesn’t ...
  • DotNetNuke Modules – Creating Base ModulesDotNetNuke Modules – Creating Base Modules Now that we have DotNetNuke installed into Visual Studio we can go ahead and create our first modules. Actually, creating the modules is pretty simple. But it is even easier to do it wrong, which...