i'm trying to make an if statment in asp.net. the if statement is done in the listView . but its not working ! all the if statement is been viewed upon runtime

<ItemTemplate>
                <div class="productItem">
                    <div>
                            <img src='<%# Eval("ProductImagePath") %>' alt="<%#Eval("ProductName") %>" height="120"
                                width="120" />
                        </a>
                    </div>
                    <div>
                        <br />
                        <b>
                            <%# Eval("ProductName") %></b>
                            If <%# Eval("CurrentStockQty") %> < 1 || null
                            Then
                                Response.Write("Out of Stock")
                            Else if <%# Eval("CurrentStockQty") %> < 5
                                Response.Write("Less than 5 available")
                            Else
                                Response.Write("In Stock")
                            End If
                            </div>
                </div>
            </ItemTemplate>

Create a function in your code block, then from your itemtemplate, call that fuction.

depending on what you are trying to do...

<%# MyFunction(param) %>
<%= MyFunction(param) %>

It's not a good implementation what you're trying to do, and I don't know if it can be made like that, I think not.

I'd suggest one of two things: Either do it on the ItemDataBound event on your code behind OR, the easiest one, create a function that return the stock label.

Something like this:

Public Function GetStockLabel(ByVal qty As Int) As String

    If qty < 1 || null
    Then
        Return "Out of Stock"
    Else if qty < 5
        Return "Less than 5 available"
    Else
        Return "In Stock"
    End If

End Function

And call it like this inside <itemtemplate>:

<b><%# Eval("ProductName") %></b>
<%# GetStockLabel(Eval("CurrentStockQty")) %>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.