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

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

问题描述

我想使用表单视图显示学生成绩单.成绩单将有额外的栏,如果它是第二学期的成绩单,如果是第三学期的成绩单,还有 2 栏.要找出它是哪个术语,我需要评估 TermNumber 属性,并显示适当的标题.这是我的标记代码:

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>
<% } %>

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

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.

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

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

使用 <%# %> 会导致编译时错误,并且 VS13 智能感知显示其无效术语.

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

我的问题是:我怎样才能成功实现有条件地显示列的目标.我可以在 Databound 控件的标记中使用 if 条件吗?或者有没有其他方法可以实现这一点,而无需为每个术语创建单独的表单视图.

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.

Form View,也有一个repeater控件来显示科目分数,也需要使用if条件.

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

推荐答案

所以我正在回答我自己的问题.感谢鲁本接受的答案的提示:eval in if statement?

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>

这在中继器控制中也没有任何问题.由于带有 runat="Server" 的 Visible 属性不包含任何标记,因此在最终输出中,它运行良好.而且 if 块的代码也更优雅.

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.

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

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