"如果" Formview标记中的条件 [英] "if" Conditions in Formview Markup

查看:167
本文介绍了"如果" Formview标记中的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用表单视图显示学生报告卡。如果报告卡是第二期的报告卡,还有另外两个列(如果是第三个),则报告卡将会有其他列。要找出它是哪个词,我需要评估TermNumber属性,并显示相应的标题。这是我的标记代码:

 <%if((int)Eval(TermNumber)== 2)% > 
<%{%>
< th>第一学分分数< / th>
<%}%>
<%else if((int)Eval(TermNumber)== 3)%>
<%{%>
< th>第一学分分数< / th>
< th>第二期评分< / th>
<%}%>

这导致运行时错误:数据绑定方法如Eval(),XPath()和绑定()只能在数据绑定控件的上下文中使用。



这是因为Eval只能是<%#%>标签的一部分。 >

使用<%#%>导致编译时错误,VS13智能感知术语表示无效的术语。



我的问题是:如何成功实现我有条件地显示列的目标。可以在数据绑定控件的标记中使用if条件吗?或者有其他方法来实现这一点,而不是为每个术语创建单独的表单。



表单视图还具有中继器控件来显示主题分数,还需要利用if条件。

解决方案

所以我回答了自己的问题。感谢Ruben接受的答案的提示: eval if语句?

 < th runat =servervisible ='<%#(int)Eval(TermNumber)> 1%>'>第一术语分数< / th> 
< th runat =servervisible ='<%#(int)Eval(TermNumber)> 2%>'>第二术语得分< / th>

这也可以在中继器控件中没有任何问题。
由于具有runat =Server的Visible属性不包含任何标记,在最终输出中,它的工作效果非常好。而代码也更优雅如果块。


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.

解决方案

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.

这篇关于&QUOT;如果&QUOT; Formview标记中的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆