Solved

Display image conditionally in dataList?

Posted on 2004-10-05
4
511 Views
Last Modified: 2011-10-03
Hey everyone,

I have a dataList that displays all my products for a particular category from the dB.  I'll post my dataList code and dB retrieval code below.  Some of the items are on sale and some of them are not.  I'd like to display a little .gif file if the item is on sale.  I'm not sure of the best way to do this?  I'm thinking I put a asp:image file up there and set it's visible property to False and then loop through the results and set it to True if it's a sale item?  I don't know how to do this.  Maybe I need to throw everything into a dataTable first or build a table on the fly or whatever?

 I'm open to whatever will solve my problem so if this isn't a good way to solve it, please tell me.  

Here is the code as it stands now:

---------front end display code------------------------
<div align="center">
<asp:datalist id="dlProductList" runat="server" enableviewstate="False" repeatcolumns="2" BorderColor="#999999"
BorderStyle="None" BackColor="White" CellPadding="3" GridLines="Vertical" BorderWidth="1px">
<selecteditemstyle font-bold="True" forecolor="White" backcolor="#008A8C"></selecteditemstyle>
<alternatingitemstyle backcolor="#DCDCDC"></alternatingitemstyle>
<itemstyle forecolor="Black" backcolor="#EEEEEE"></itemstyle>
<itemtemplate>
      <table width="345" cellpadding="2" cellspacing="2" border="0" align="center">
            <tr>
                  <td width="150"><a href='ProductDetail.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "ID") %>'><img src='Images/ProductImages/<%# DataBinder.Eval(Container.DataItem, "ImageName") %>' width="150" height="150" border="1"></a>
                  </td>
                  <td><a href='ProductDetail.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "ID") %>'>
                  <span class="ProductListHead"><%# DataBinder.Eval (Container.DataItem, "BriefDescription") %>      </span></a><br>                                                      <span class="ProductListItem">ProductID:<%# DataBinder.Eval(Container.DataItem, "ProductID") %></span><br>
                  </td>
            </tr>
      </table>
</itemtemplate>
<footerstyle forecolor="Black" backcolor="#CCCCCC"></footerstyle>
<headerstyle font-bold="True" forecolor="White" backcolor="#000084"></headerstyle>
<alternatingitemtemplate>
      <table width="345" cellpadding="2" cellspacing="2" border="0" align="center">
            <tr bgcolor="aliceblue">
                  <td width="150"><a href='ProductDetail.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "ID") %>'><img src='Images/ProductImages/<%# DataBinder.Eval(Container.DataItem, "ImageName") %>' width="150" height="150" border="1">      </a>
                  </td>
                  <td><a href='ProductDetail.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "ID") %>'>
                  <span class="ProductListHead"><%# DataBinder.Eval(Container.DataItem, "BriefDescription") %></span></a><br>                                                <span class="ProductListItem">ProductID:<%# DataBinder.Eval(Container.DataItem, "ProductID") %>
            </span><br>
                  </td>
            </tr>
      </table>
</alternatingitemtemplate>
</asp:datalist></div>
-------------------------------------------------

-----Here is the code behind function -----------------
Public Function ProductsByCategory(ByVal CatID As String)

        ' Create Instance of Connection and Command Object
        Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
        Dim myCommand As SqlCommand = New SqlCommand("ProductsByCategory", myConnection)

        ' Mark the Command as a SPROC
        myCommand.CommandType = CommandType.StoredProcedure

        ' Add Parameters to SPROC
        Dim parameterCategoryID As SqlParameter = New SqlParameter("@Category", SqlDbType.VarChar, 255)
        parameterCategoryID.Value = CatID
        myCommand.Parameters.Add(parameterCategoryID)

        ' Execute the command
        myConnection.Open()
        Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
        dlProductList.DataSource = result
        dlProductList.DataBind()

    End Function
----------------------------------------------------------

------- Here is the SPROC from SQL Server 2k---------
CREATE Procedure ProductsByCategory
(
    @Category varchar(255)
)
AS
SELECT
    ID,
    ProductID,
    ImageName,
    BriefDescription
FROM
    psiProducts
WHERE
    Category = @Category
AND
    VisibleToRetail = 1
GO
-------------------------------------------------------------

0
Comment
Question by:cmlawson
[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
4 Comments
 
LVL 6

Expert Comment

by:viola123
ID: 12233202
hi,mate:

give u a hint(that's what i did to display an image according to the status of a product):

--------------->>>>>>>>>>>>>in your dataList:
</HeaderTemplate>
<ItemTemplate>
      <tr valign="bottom">
            <td rowpsan="2"><IMG SRC="<%# findImage(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "pt_recno"))) %>" align="absMiddle" border="0"/></td>


--------------->>>>>>>>>>>>>in your code behind:
            public string findImage(int qhrecno)
            {
                  TechSellerWebLib.Commands oCmd = new TechSellerWebLib.Commands();
                  string lcWhere = "pl_ptrecno = " + qhrecno;
                  string lcResults = oCmd.getPartsLinks(lcWhere);

                  DataSet dsImageLinks = new DataSet();
                  dsImageLinks.ReadXml(new StringReader(lcResults));
                  string imageLink = "";

                  if (dsImageLinks.Tables["partslinks"].Rows.Count > 0)
                        imageLink = "../images/16x16/Plain/Image Available Icon.gif";
                  else
                        imageLink = "../images/16x16/Plain/image_not_available.gif";

                  return imageLink;      
            }

cheers
viola
0
 
LVL 9

Accepted Solution

by:
Joeisanerd earned 200 total points
ID: 12233333
You can simply data bind to the visible property with a bit field from the database. So if there is a true/false field for sale then just bind it to that value.

<asp:Image id="Image1" runat="server" visible=<%# DataBinder.Eval (Container.DataItem, "OnSale") %>></asp:Image>

The other way would be to use the ItemDataBound event to set the visibility property.

0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12234612
Hi,

you have to use DataList.ItemDataBound event
example:

<asp:DataList id="ItemsList"
           RepeatDirection="Vertical"
           OnItemDataBound="Item_Bound"
           runat="server">
               
         <ItemTemplate>
            <asp:Image id="ImageDataList" runat="server" />
         </ItemTemplate>
 
      </asp:DataList>


Sub Item_Bound(sender As Object, e As DataListItemEventArgs)

         If e.Item.ItemType = ListItemType.Item Or _
             e.Item.ItemType = ListItemType.AlternatingItem Then

            Dim imageTemp as Image = CType(e.FindControl("ImageDataList"), Image )
            if condition then
            imageTemp.ImageUrl = 'Set your url
            end if

         End If

      End Sub


Regards,
B..M
0
 

Author Comment

by:cmlawson
ID: 12251638

I think they're all good solutions but the one accepted is the best because it takes so little code.  Less is more....
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 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