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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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...
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.