Solved

Display image conditionally in dataList?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

22 Experts available now in Live!

Get 1:1 Help Now