Question on Asp:Repeater

Hi,

I have an product catalog page for online shopping.

This is the sample of the DataSet

ID Name ThumbnailImage BigImage

1 Product A productA.jpg productA_big.jpg

2 Product B productB.jpg <NULL>

3 Product C <NULL> <NULL>

I'd like to display the data in a way that the thumbnail and a link to the big image would only be shown when necessary, like this:

<a href="productA_big.jpg" target="_blank">

<img src="productA.jpg">

</a><br>

Product A

<hr>

<img src="productB.jpg"><br>

Product B

<hr>

Product C

<hr>

I'm using a repeater but it's trying to show the thumbnail even when there's no file, and provide the link even when I don't want it to...

Is there anyway I can control the Repeater?

WB.

[952 byte] By [namewb] at [2008-2-14]
# 1
Hi,

Create a function with two input arguments ThumbnailImage & BigImage and which return a string depending on these two inputs as you wish. then use the function in repeater data binding like below

<asp:Repeater ....>
<ItemTemplate>
<%#GetHtml(DataBinder.Eval(Container.DataItem,"ThumbnailImage"),DataBinder.Eval(Container.DataItem,"BigImage"))%>
</ItemTemplate>
</asp:Repeater>
Get Html function in code behind or in server side inline page script

public string GetHtml(object ThumbnailImage,object BigImage)
{
string result;
// Write your logic here ThumbnailImage.ToString() will return string value
//for example to display only link when thumbnailimage is available
if(ThumbnailImage!=null)
{
result = result + "<a href=\"" + ThumbnailImage.ToString() + "\" target=\"_blank\">";
}
}

sudkot at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2
Hi Sudkot,

Thanks for the advice.

Is there any way I can do it with server controls like <asp:Image> & <asp:hyperlink>?
Wang.

namewb at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3
Wang,

You can use <asp:Image> & <asp:hyperlink> tags inside the repeater, and then set their visibility in the code behind using the ItemDataBound event. Something like this:



protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ( e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item )
{
Image bigImage = (Image) e.Item.FindControl("ImageID");
bigImage.Visible = ImageShouldBeVisible(e.Item.DataItem);

Hyperlink link = (Hyperlink) e.Item.FindControl("HyperlinkID");
link.Visible = LinkShouldBeVisible(e.Item.DataItem);
}
}

robincurry at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4
Robincurry,

Great answer above! Here is my situation: I am trying to disply images in a repeater like above; however, my images are retrieved from an SQL database. The image binary data and the image content type are both retrieved from the SQL database. I want a thumbnail version of the image displayed in the repeater, but this thumbnail should be linked to a bigger version of the thumbnail.

Here is a sample of my data:

image_data cont_type image_desc
<bin> image/pjpeg description 1
<bin> image/pjpeg description 2
<bin> image/pjpeg description 3
<bin> image/pjpeg description 4
<bin> image/pjpeg description 5

I am confused on how to use the <asp:Image> tags in combination with DataBinder.Eval(Container.DataItem, "image_data") to display my images. I can display an image using:

Response.ContentType = reader["cont_type"].ToString();
Response.BinaryWrite( (byte[]) reader["img_data"] );

Thank you for your anticipated help and assistance,
Shola.

Shola at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 5
Shola,

Any future questions regarding ASP.NET should be directed to http://forums.asp.net.

DavidM.Kean at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified