ASP.NET直放站评估和演示 [英] ASP.NET Repeater Eval
问题描述
我想在.aspx页面中执行以下code:
I am trying to execute the following code in a .aspx page:
<asp:Repeater ID="rptComentarios" runat="server">
<ItemTemplate>
<% if (Convert.ToInt32(Eval("int_tipo")) == 1)
{ %>
<div class="resp">
<div class="top">
</div>
<div class="cont-resp">
<h3>
<%# Eval("txt_nome") %></h3>
<p>
<%# Eval("txt_comentario") %></p>
</div>
</div>
<% }
else
{%>
<div class="usuario">
<div class="top">
</div>
<div class="cont-usuario">
<h3>
<%# Eval("txt_nome") %></h3>
<p>
<%# Eval("txt_comentario") %></p>
</div>
</div>
<% } %>
</ItemTemplate>
</asp:Repeater>
它抛出一个运行时异常中的第一行:
It throws a runtime exception in the first line:
<% if (Convert.ToInt32(Eval("int_tipo")) == 1)
System.InvalidOperationException:数据绑定方法如的eval(),XPath的()和bind()只能在数据绑定控件的上下文中使用
System.InvalidOperationException: Databinding methods such as Eval(), XPath() and Bind() can only be used in the context of a databound control.
有什么不对?任何想法?
What's wrong? Any ideas?
推荐答案
我有一个类似的问题,以下code为我工作:
I had a similar problem and the following code worked for me:
<asp:Repeater ID="rptComentarios" runat="server">
<ItemTemplate>
<asp:PlaceHolder ID="placeholderBlaBlaBla" runat="server" Visible='<%# Convert.ToInt32(Eval("int_tipo")) == 1 %>'>
Your optional HTML
</asp:placeholder>
Other HTML
</ItemTemplate>
</asp:Repeater>
一些更多的评论:
Some more comments:
请注意,单引号用于定义值
。我试着双引号也和他们没有工作。可见
属性ASP:占位符
Please note that single quotes are used to define the value Visible
attribute of asp:placeholder
. I tried double quotes too and they didn't work.
任何时候你想要获得一些可选显示HTML您应该使用控件来显示/隐藏它。 ASP:占位符
为此工作正常。永远不要做&LT;%IF(..){%方式&gt;
- 这是邪恶的。
Anytime you want to get some optionally displayed HTML you should use a control to show/hide it. asp:placeholder
works fine for that purpose. Don't ever do <% if(..) { %>
- this is evil.
&LT;%#......%&GT;
用于计算和显示前pressions一个中继器内。这些前pressions可以显示为HTML或服务器端控件的属性通过。不能使用如果
里面。
<%# ... %>
is used to calculate or display expressions inside a repeater. These expressions can be displayed as HTML or passed as attributes of server side controls. You can't use if
inside it.
这篇关于ASP.NET直放站评估和演示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!