"if" Conditions in Formview Markup
Asked Answered
A

1

2

I want to show a student report card using a form view. The report card will have additional columns, if it is a report card from 2nd term and 2 more columns, if its from 3rd term. To find out which term it is, I need to evaluate the TermNumber Property, and show the appropriate heading. This is my mark up code:

<% if ((int)Eval("TermNumber") == 2) %>
<% { %>
    <th> 1st Term Score</th>
<% } %>
<% else if ((int)Eval("TermNumber") == 3) %>
<% { %>
    <th> 1st Term Score</th>
    <th> 2nd Term Score</th>
<% } %>

This results in run time Error : Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

This is because Eval can only be part of a <%# %> tag.

Using <%# %> results in compile time error, and the VS13 intellisense, says its invalid term.

My Question is: How can I succeed in achieving my target of conditionally showing the columns. Can I use the if Condition in the markup of the Databound controls? Or is there any other way to achieve this, without creating separate formviews for each term.

The Form View, also has a repeater control to show the subject scores, and also needs to utilise the if Condition.

Allister answered 17/10, 2014 at 19:58 Comment(0)
A
2

So I am answering my own question. Thanks to the hint from Ruben's Accepted answer at: eval in if statement?

<th runat="server" visible='<%# (int)Eval("TermNumber") > 1 %>'>1st Term Score</th>
<th runat="server" visible='<%# (int)Eval("TermNumber") > 2 %>'>2nd Term Score</th>

This also works without any problems in the repeater control. As the Visible property with runat="Server" doesn't includes any markup, in the final output, it works perfectly fine. and the code is also more elegant the if blocks.

Allister answered 18/10, 2014 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.