Using Container.DataItem with Dynamic Column Names

I have a page where the user can select a number of reports at the top of the page and select the appropriate variables to go with it. It uses an update panel for a clean call back and everything is dumped into a repeater control. Every report returns the exact same fields. Everything worked great until.....

The user has decided that one report needs 4 new fields. Only that report will have these fields. I have set a boolean value on the .VB file that is set to true when that report is run and false when any other report is run. I use this to add the column headers as needed to the header template. The problem is in the ItemTemplate where now we have some fields that don't match up. I have tried:
<% If MyBool = True Then %>
Special Report Code
<% Else %>
Original Report Code
<% End If %>

I can see in debug that the book is set correctly but it still tries to render the "Special Report Code" anyway. I was pretty sure that this type of IF no longer worked and it appears to be the case. I have done some tricky formatting stuff with this report where I use this:
<%#MyVBFunction(Container.DataItem("Col1"), Container.DataItem("Col2"))%>
That works like a charm but can't be used here because the columns aren't always there.

Any ideas?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

NazoUKConnect With a Mentor Commented:
I think the best way to do it would be to handle the OnItemDataBound event of your repeater control.
Put literals inside your ItemTemplate where the extra fields would go, then in the event:

Protected Sub myRepeater_ItemDataBound(sender as object, e as RepeaterItemEventArgs)

   If RenderExtraColumns Then

       Dim lit1 as Literal = Ctype(e.Item.FindControl("lit1"),Literal)
       ... do for all columns

      Dim row as myRowDataType = Ctype(e.Item.DataItem, myRowDataType)
      lit1.Text = Data from row
   End If
End Sub

Without knowing what kind of data you are binding to the control I can't specify exactly how to access it, but e.Item.DataItem contains the data for the row being bound which you can access. If you aren't sure what type it is then examine it in the debugger first.
HainKurtSr. System AnalystCommented:
what about to use that boolean to make some elements visible/hidden

<div id=rptSpecial visible=<%=isSpecial%> runat=server>
..... elements for special reports are here, you can repeat same syntax for other parts as well
GoCubsAuthor Commented:
Well, it still errors out because the repeater tries to link with columns that are not there even if it is in an IF block.
HainKurtSr. System AnalystCommented:
hmms... ok, then do this

create two repeater, one regular, the other one special

<asp:repeater id=rptRegular visible=<%=not isSpecial%> runat=server>

<asp:repeater id=rptSpecial visible=<%=isSpecial%> runat=server>
HainKurtSr. System AnalystCommented:
and set datatsources to nothing first, then bind data to the correct repeater not to get any error...
All Courses

From novice to tech pro — start learning today.