I inherited some crazy code from someone who's long gone. He implemented a single custom control to implement a large passel of loosely related queries.
The control has a bunch of different panels, all set to Visible='false' to reflect the different set of filter inputs. Then there's a single GridView for the results. The GridView has <asp:TemplateFields Visible='false'>
for the superset of all possible columns returned from all the different queries he wanted to run.
The code-behind for the control runs around at init flipping Visible='true' for the filter panel and the columns of the particular query wanted at the moment.
To compound the craziness, I just noticed that having <asp:TemplateField Visible="false">
doesn't stop the <ItemTemplate> inside from getting built, so his control is doing all kinds of extra work that will never get seen.
To give one example:
<asp:TemplateField HeaderText="Expiration Date" SortExpression="ExpirationDate" Visible="false"><br />
<itemtemplate><br />
<asp:HyperLink runat="server" ID="FormLink" Target="_blank" NavigateURL="<%# CreateFormLink() %>" Text='<%# DateUtil.getFormattedDate(Eval("ExpireDate")) %>' ><br />
</asp:HyperLink><br />
</itemtemplate><br />
</asp:TemplateField><br />
The code-behind method CreateFormLink()
gets called even though Visible is false...
The only reason it hadn't blown up before was that the method uses StringBuilder to cobble together the href, and StringBuilder.Append(Eval("foo"))
doesn't blow up.
I tried to replace the StringBuilder use with concatenations to be more memory efficient and got a boom when the column wasn't in the current result set.
Since Visible='false'
isn't stopping the work from being done (even though it stops the display), is there a more canonical way to do this? Or should I just start over?
You might want to implement show/hide logic in ItemDataBound event.
If you do so, you have full control over calling of CreateFormLink()