Asp.Net News
Free Tech Magazines
Asp.Net Web Hosting
Suggest Us
Link to Us
Feeds Subscription
Questions & Answers
Tips & Tricks
 Display photos from folder and database simultaneously in datalist. Challenge?
Posted by mathieu cupryk on  2/18/2010 8:16:49 PM Category: Asp.Net 2.0
Add to my favorites
Email to friend
I have to display binary images and images from a folder in a datalist.

I am almost there. But I need your help.

The code is pretty self explanatory.
<asp:DataList ID="DLThumbnails" runat="server" DataKeyField="img_id" RepeatDirection="Horizontal"
CellPadding="3" HorizontalAlign="Center" CssClass="view" RepeatColumns="5" Item-BorderWidth="1"
Item-BorderColor="#c0c0c0" OnItemDataBound="DLThumbnails_ItemDataBound" OnItemCommand="DLThumbnails_ItemCommand"
BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
GridLines="Horizontal" ShowFooter="False" ShowHeader="False">
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<table cellspacing="7" cellpadding="0" border="0">
<tr valign="top">
<tr height="150" valign="top">
<div class="itemTemplate">
<div style="position: relative; height: 148px;">
<asp:Image runat="server" ID="view_image" Width="118" Height="148" alt='Photo from Album Number <%# Eval("img_id") %>' />
<asp:Image ID="image1" runat="server" Visible="false" Width="118" Height="148" ImageUrl="~/Images/Upload/photo1.png" />
<asp:Image ID="image2" runat="server" Visible="false" Width="118" Height="148" ImageUrl="~/Images/Upload/photo2.png" />
<asp:Image ID="image3" runat="server" Visible="false" Width="118" Height="148" ImageUrl="~/Images/Upload/photo3.png" />
<asp:Image ID="image4" runat="server" Visible="false" Width="118" Height="148" ImageUrl="~/Images/Upload/photo4.png" />
<asp:Image ID="image5" runat="server" Visible="false" Width="118" Height="148" ImageUrl="~/Images/Upload/photo5.png" />
<br />
<div class="details">
<%# Convert.ToBoolean(DataBinder.Eval(Container, "DataItem.img_is_primary"))? "Primary Picture" :"" %>
<asp:LinkButton ID="lnkSetPrimary" CssClass="pagelink2" runat="server" OnClientClick="return confirm('Do you want to make this image your primary?');"
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "img_id")%>' Visible='<%# !Convert.ToBoolean(DataBinder.Eval(Container, "DataItem.img_is_primary")) %>'
Text="Set Primary Photo" OnClick="SetPrimary"></asp:LinkButton><br />
<asp:LinkButton ID="lnkRemove" CssClass="pagelink2" runat="server" OnClientClick="return confirm('Do you want to delete image?');"
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "img_id")%>' Text="Remove"
<h4><%# Server.HtmlEncode(Eval("img_caption").ToString()) %></a></h4>
<%# Eval("NumberOfPhotos")%> Photo(s)
<ItemStyle Width="20%" BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />

protected void DLThumbnails_ItemDataBound(object sender, DataListItemEventArgs e)

System.Web.UI.WebControls.Image img = null;
// make sure this is an item in the data list (not header etc.)
if ((e.Item.ItemType == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.AlternatingItem))
//lblCaption.Text = "Image # " + e.CommandArgument;
// get a reference to the image used for the bar in the row
img = (System.Web.UI.WebControls.Image)

//get the img_pk from DataRow being bound
int itemIndex = e.Item.ItemIndex;
string img_pk = DLThumbnails.DataKeys[itemIndex].ToString();
img.ImageUrl = "Handler.ashx?img_id=" + img_pk + "&" + "imagesize=F";
img.ID = img_pk;
img.AlternateText = "Thumbnail of Photo Number =" + img_pk;
img.ToolTip = "Thumbnail of Photo Number =" + img_pk;

DataSet ds = new DataSet();
ds = GetBinaryImagesByProfileID.FetchAllImagesInfo(intGetProfileID, "omegaloveConnectionString");
CountPhotos = Convert.ToInt32(ds.Tables[0].Rows[0]["NumberOfPhotos"].ToString());
//Get the count of images to be displayed
int imgCountToBeDisplayed = 5 - CountPhotos;
//In the for loop
for (int i = 5; i >= imgCountToBeDisplayed; i--)
string imageId = string.Format("image{0}", i);
System.Web.UI.WebControls.Image image =(System.Web.UI.WebControls.Image) e.Item.FindControl(imageId);
image.Visible = true;


if ((e.Item.ItemType == ListItemType.Footer))
if ((DLThumbnails.Items.Count == 0))
Panel1.Visible = true;


Post Your Reply Post New Question

 Viewer's Reply
Answered By Shanu_Sudha on 2/19/2010 3:03:50 AM
Hi.. I think, you failed explain your problem clearly...

I think, you've tried too many process to just bind the image into datalist.. Very simple coding is available..

Anyway can u explain your problem clearly...?
Post Your Reply If you still have problem, post it again under this reply for better response.

Sponsored by
The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks - Free 158 Page Preview
Easing the Migration to Microsoft SQL Server 2005
Building a Web Application Security Program