RayBanUK
asked on
IF condition in ASP.NET DataList
New to ASP.NET and programming in VB. I was wondering what is the easiest way to put IF condtions in DataLists? Look at the example below. The 'Online Price' is not always valid and neither is the 'Save 10%' (or whatever percentage) so I need to know aside, from not displaying data item, how I would go about in a DataList hiding the HTML code as well?
I tried a IF statement like in classic ASP but I think I may be stumbling on the syntax. Something along the lines of
<%IF DataBinder.Eval(Container. DataItem, "DISCOUNT") <> 0 THEN%>
<%END IF%>
PLEASE HELP!
<asp:DataList class="MainText" id="NewList" runat="server" CellSpacing="2" CellPadding="2" width="100%">
<ItemTemplate>
<tr valign="top">
<td class="Listing">
<a href="bookdetails.aspx?ISB N=<%# DataBinder.Eval(Container. DataItem, "ISBN") %>"><img src="http://www.kogan-page.co.uk/images/<%# DataBinder.Eval(Container. DataItem, "ISBN") %>.jpg" class="BookImage" width="60"></a></td>
<td class="Listing">
<p>
<a href="bookdetails.aspx?ISB N=<%# DataBinder.Eval(Container. DataItem, "ISBN") %>"><strong><%# DataBinder.Eval(Container. DataItem, "Title") %></strong></a>
<br />
<%# DataBinder.Eval(Container. DataItem, "Subtitle") %>
<br />
<span class="SmallText"> Author: <a href=""><%# DataBinder.Eval(Container. DataItem, "Author") %></a>
<br />
ISBN: <%# DataBinder.Eval(Container. DataItem, "ISBN") %>
<br />
Binding: <%# DataBinder.Eval(Container. DataItem, "Binding") %>
<br />
Publication Date: <%# DataBinder.Eval(Container. DataItem, "PublicationDate") %> </span>
</p>
<p class="SmallText">
<a href="bookdetails.aspx?ISB N=<%# DataBinder.Eval(Container. DataItem, "ISBN") %>">More
Details</a>
</p>
</td>
<td class="Listing">
<p>
<strong>Online Price: <span class="RedText"></span></s trong>
<br />
Published Price: <span class="StrikeText"><%# DataBinder.Eval(Container. DataItem, "Price") %></span>
<br />
Save 10%
</p>
<p>
<a href=""><img src="images/addtobasket.gi f" alt="" width="100" height="19" border="0" class="BookImage" /></a>
</p>
</td>
</tr>
</ItemTemplate>
</asp:DataList>
I tried a IF statement like in classic ASP but I think I may be stumbling on the syntax. Something along the lines of
<%IF DataBinder.Eval(Container.
<%END IF%>
PLEASE HELP!
<asp:DataList class="MainText" id="NewList" runat="server" CellSpacing="2" CellPadding="2" width="100%">
<ItemTemplate>
<tr valign="top">
<td class="Listing">
<a href="bookdetails.aspx?ISB
<td class="Listing">
<p>
<a href="bookdetails.aspx?ISB
<br />
<%# DataBinder.Eval(Container.
<br />
<span class="SmallText"> Author: <a href=""><%# DataBinder.Eval(Container.
<br />
ISBN: <%# DataBinder.Eval(Container.
<br />
Binding: <%# DataBinder.Eval(Container.
<br />
Publication Date: <%# DataBinder.Eval(Container.
</p>
<p class="SmallText">
<a href="bookdetails.aspx?ISB
Details</a>
</p>
</td>
<td class="Listing">
<p>
<strong>Online Price: <span class="RedText"></span></s
<br />
Published Price: <span class="StrikeText"><%# DataBinder.Eval(Container.
<br />
Save 10%
</p>
<p>
<a href=""><img src="images/addtobasket.gi
</p>
</td>
</tr>
</ItemTemplate>
</asp:DataList>
ASKER
Thanks I have consider the above option but I need to hide/unhide the html as well. For instance, if there is a discount say "Special Price" and "Save x%".
What is the condition to hide/unhide a row?
Do you want to only show the row when their is a valid discount?
Do you want to only show the row when their is a valid discount?
ASKER
Yes basically the following bit of code
<strong>Online Price: <span class="RedText"><%# DataBinder.Eval(Container. DataItem, "DiscountPrice") %></span></strong>
<br />
Published Price: <span class="StrikeText"><%# DataBinder.Eval(Container. DataItem, "Price") %></span>
<br />
Save <%# DataBinder.Eval(Container. DataItem, "Discount") %>%
I want the 'Online Price' and 'Save %' not be displayed if no discount.
Traditionally in classic ASP I would have done something like
<%if Discount <> 0 then%>
Online Price: <span class="RedText"><%=rs("Dis countPrice ")%></span ><br>
<%end if%>
Published Price: <%=rs("Price")%><br>
<%if Discount <> 0 then%>
Save <%=rs("Discount")%> %
<%end if%>
Just trying to find out how to do similar in a datalist.
<strong>Online Price: <span class="RedText"><%# DataBinder.Eval(Container.
<br />
Published Price: <span class="StrikeText"><%# DataBinder.Eval(Container.
<br />
Save <%# DataBinder.Eval(Container.
I want the 'Online Price' and 'Save %' not be displayed if no discount.
Traditionally in classic ASP I would have done something like
<%if Discount <> 0 then%>
Online Price: <span class="RedText"><%=rs("Dis
<%end if%>
Published Price: <%=rs("Price")%><br>
<%if Discount <> 0 then%>
Save <%=rs("Discount")%> %
<%end if%>
Just trying to find out how to do similar in a datalist.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You will have to adjust the code to how your discount and price values are stored (string or int)
Put this code in your HTML page
<%# ReturnDiscount(Container.D
Put this function in your codebehind
Function ReturnDiscountPrice(ByVal intdiscount As interger, ByVal intprice As interger)
dim intnewprice
intnewprice = intprice
if intdiscount <> 0 then
intprice = intprice * intdiscount
end if
Return intprice
End Function
Cheers