Solved

Display image conditionally in dataList?

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reset asp.net login password 4 42
Delete HTML table rows 12 29
Checkbox in a dynamic GridVIew is not clickable? 5 27
Problem to refer to value 8 50
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 …
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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