Vladimir Potapov
Vladimir Potapov

Reputation: 2437

If statement in HTML

Hello i have list of products this my html

 <telerik:RadListBox ID="RadListBox1" runat="server" DataSourceID="LinqDataSourceCategories"       Height="200px" Width="250px" CssClass="ui-droppable" >
                <EmptyMessageTemplate>
                No records to display.
                </EmptyMessageTemplate>
                <ItemTemplate >

                <div  class="ui-draggable ui-state-default" data-shortid="<%#Eval("ID")%>">
                <em>Active : </em><span><%# Eval("LoadProduct") %></span><br />
                <em>ProductId: </em><span><%# Eval("ProductId") %></span><br />
                 </div>
                </ItemTemplate>

         </telerik:RadListBox>

I have Active that is say if LoadProduct true or false in client it look like this

<em>Acrive : </em>
<span>False</span>
<br>
<em>ProductId: </em>
<span>101-01-056-02</span>
<br>
<em>ShortID: </em>
<span class="ShortID" data-shortid="0">0</span>
<br>

I want to replace text with img ,i need to check if <%# Eval("LoadProduct") %> ==true put img scr=/green.png/ else img scr=/red.png/ that clien will look like this

    <em>Acrive : </em>
        <span><img src='green.jpg'/></span>

 <br>
        <em>ProductId: </em>
        <span>101-01-056-02</span>
        <br>
        <em>ShortID: </em>
        <span class="ShortID" data-shortid="0">0</span>
        <br>

Sow how it can be done? To add if statement to HTML,

or to catch event that building all elements and there check if LoadProduct==true and append html to item?

Upvotes: 1

Views: 360

Answers (4)

Kamil T
Kamil T

Reputation: 2216

Come to think of it, I don't really like the idea of back-end conditionals in front-end. Code-behind is one thing and frontside is the other - it's not a good idea to mix it if it isn't inevitable. The purest way to get that would be to set the variable (or a function, in your case) in code-behind and just to show it in front-end, like:

Code behind:

protected string GreenOrRed(bool isLoadProduct)
{
    return isLoadProduct ? "green" : "red";
}

(the function or variable has to be at least protected in order to be accessible in aspx page, because the aspx page is the derivative from base class of aspx.cs )

front-end:

<span><img src='<%# GreenOrRed((bool)Eval("LoadProduct")) %>.jpg'/></span>

After that, remeber to add

this.DataBind();

in your Page_Load() function.

Let the code-behind decide, let the front-end only show the result.

Upvotes: 2

Satpal
Satpal

Reputation: 133453

Try this

 <img src='<%# (bool)Eval("LoadProduct") ? "green.jpg" : "red.jpg" %>'/>

Upvotes: 1

Kamil T
Kamil T

Reputation: 2216

<% 
    if((bool)Eval("LoadProduct") == true)
    {
        Response.Write("src='green.jpg'");
    }
    else Response.Write("src='red.jpg'");
%>

Upvotes: 1

sangram parmar
sangram parmar

Reputation: 8736

try this

<em>Active : </em><span><%# Convert.ToBoolean(Eval("LoadProduct")) ? "<img src='green.jpg'/>" : "<img src='red.jpg'/>"%></span><br />

Upvotes: 2

Related Questions