[已解决]除非包含的Panel可见,如何防止Eval()被调用? [英] [Solved] How to prevent Eval() from being called unless the containing Panel is Visible?
问题描述
我有一个页面,如果该图像存在于Web服务器的磁盘上,我们希望显示该图像;否则,我们希望显示产品功能描述文本.
我的方法是实现两个asp:Panel
控件,一个用于保存包含asp:ListView
的<div>
,该asp:ListView
调用DB以显示产品功能文本.另一个asp:Panel
以显示包含图像的<img>
的<div>
.
但是,我也想避免调用Eval()
函数以调用DB来获取功能部件文本,除非显示了包含asp:Panel
的功能部件文本.
我对以下代码有误吗?
I have a page where we want to either display an image of a product if the image exists on the disk of the web server, or if not, we want product features description text displayed.
My approach was to implement two asp:Panel
controls, one to hold the <div>
containing the asp:ListView
that calls the DB to display the product feature text; and another asp:Panel
to display the <div>
that contains the <img>
for the image.
I want to also, though, avoid calling the Eval()
function to call into the DB to get the feature text unless its containing asp:Panel
is displayed.
Am I doing something wrong with the following code?
<asp:Panel runat="server" ID="pnlFeaturesText" Visible="false" CssClass="FeaturesList">
<div id="divFeaturesText">
<asp:ListView runat="server" ID="lstFeatures">
<layouttemplate>
<ul>
<li runat="server" id="itemPlaceholder" />
</ul>
</layouttemplate>
<itemtemplate>
<ul><li style="margin-removed 15px;">
<asp:Label runat="server" Text='<%# if (pnlFeaturesText.Visible) { Eval("Feature1") } %>' Style="font-weight: 700;" Font-Names="Verdana" /><br />
<asp:Label runat="server" Text='<%# if (pnlFeaturesText.Visible) { Eval("Benefit") } %>' Font-Names="Verdana" /></li></ul>
</itemtemplate>
</div>
我特别在谈论台词
I am specifically talking about the lines
<asp:Label runat="server" Text='<%# if (pnlFeaturesText.Visible) { Eval("Feature1") } %>' Style="font-weight: 700;" Font-Names="Verdana" />
由于某种原因,我不能在Text
属性中使用if
语句;该页面返回异常无效的表达式:如果".
有什么解决方法?在我的代码中手动设置文本?它的语法是什么?
谢谢!
Brian
I can''t use an if
statement in the Text
property for some reason; the page returns the exception "Invalid expression: if".
What would be a workaround? Set the text by hand, in my code? What''s the syntax for that?
Thanks!
Brian
推荐答案
在您的代码后写一个方法,在其中放置if
,然后从标记中调用您的方法.
Write a method in your code behind, put theif
in there, and call your method from the markup.
public void ControlEval(string evalThis)
{
if (pnlFeaturesText.Visible)
{
Eval(evalThis);
}
}
然后您可以执行以下操作:
and then you can do this:
<asp:Label runat="server" Text='<%# ControlEval("Feature1"); %>' Style="font-weight: 700;" Font-Names="Verdana" /><br />
<asp:Label runat="server" Text='<%# ControlEval("Benefit"); %>' Font-Names="Verdana" /></li></ul>
这篇关于[已解决]除非包含的Panel可见,如何防止Eval()被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!