?
Solved

IF condition in ASP.NET DataList

Posted on 2004-09-09
9
Medium Priority
?
15,699 Views
Last Modified: 2007-12-19
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?ISBN=<%# 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?ISBN=<%# 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?ISBN=<%# DataBinder.Eval(Container.DataItem, "ISBN") %>">More
                                                    Details</a>
                                                </p>
                                            </td>
                                            <td class="Listing">
                                                <p>
                                                    <strong>Online Price: <span class="RedText"></span></strong>
                                                    <br />

                                                    Published Price: <span class="StrikeText"><%# DataBinder.Eval(Container.DataItem, "Price") %></span>

                                                    <br />
                                                    Save 10%

                                                </p>
                                                <p>
                                                    <a href=""><img src="images/addtobasket.gif" alt="" width="100" height="19" border="0" class="BookImage" /></a>
                                                </p>
                                            </td>
                                        </tr>
                                    </ItemTemplate>
                                </asp:DataList>
0
Comment
Question by:RayBanUK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
9 Comments
 
LVL 3

Expert Comment

by:gspronych
ID: 12018949
I would solve your problem in the codebehind, you can do it like this.
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.DataItem("DISCOUNT"),Container.DataItem("Price"))%>

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
0
 

Author Comment

by:RayBanUK
ID: 12025232
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%".
0
 
LVL 3

Expert Comment

by:gspronych
ID: 12026584
What is the condition to hide/unhide a row?

Do you want to only show the row when their is a valid discount?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:RayBanUK
ID: 12028199
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("DiscountPrice")%></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.
0
 
LVL 3

Accepted Solution

by:
gspronych earned 252 total points
ID: 12028581
Ok, I would modify the code as below, and declare a protected variable in your code behind

Protected strDiscountinfo As String

    Function ReturnDiscountPrice(ByVal intdiscount As interger, ByVal intprice As interger)
        dim intnewprice
        intnewprice = intprice

        if intdiscount <> 0 then
             intprice = intprice * intdiscount
             strDiscountinfo = "Online price $" & intprice & " Save %" & intdiscount
'set the discount text if a discount exists
        else
             strDiscountinfo = ""
'empty the discount text if no discount exists
        end if

        Return intprice

    End Function

And use this in your HTML page

<%# ReturnDiscount(Container.DataItem("DISCOUNT"),Container.DataItem("Price"))%>
<% response.write(strDiscountinfo) %>
'the strDiscountinfo variable will be available to the HTML page since the variable is form protected

This should be a good start
0
 
LVL 5

Assisted Solution

by:ajitanand
ajitanand earned 248 total points
ID: 12090795
hello,

Here is my approach if you really want to use the If Then kind of system as INLINE in your ASPX Code.

---------------------------------------------
<%# Iif( DataBinder.Eval(Container.DataItem, "DiscountPrice") = 0, "", "<strong>Online Price: <span class=""RedText"">" + DataBinder.Eval(Container.DataItem, "DiscountPrice").ToString() + "</span></strong><br />" %>

Published Price: <span class="StrikeText"><%# DataBinder.Eval(Container.DataItem, "Price") %></span>
<br />

<%# Iif( DataBinder.Eval(Container.DataItem, "Discount") = 0, "", "Save: " + DataBinder.Eval(Container.DataItem, "Discount").ToString() & "%" %>

------------------------------------------
This use of IIF Stamement is quite flexible, and can help you in a lot of situations.


Tip: Instead of using DataBinder.Eval, use something like:
<%# CType(Container.DataItem, DataRowView)("columnName") %>

It is MUCH (reallu much!!!) faster than DataBinder.Eval, which uses reflection during databinding.


Hope this helps.

warm regards,
Ajit Anand
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question