Solved

Display image conditionally in dataList?

Posted on 2004-10-05
4
497 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now