Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
<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
<asp:Label runat="server" Text='<%# if (pnlFeaturesText.Visible) { Eval("Feature1") } %>' Style="font-weight: 700;" Font-Names="Verdana" />


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
Posted
Updated 1-Sep-11 6:04am
v2

1 solution

Write a method in your code behind, put the if in there, and call your method from the markup.

C#
public void ControlEval(string evalThis)
{
    if (pnlFeaturesText.Visible)
    {
        Eval(evalThis);
    }
}


and then you can do this:

XML
<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>
 
Share this answer
 
v3
Comments
RaisKazi 1-Sep-11 14:48pm    
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900