Answered by:
Dynamic Photogallery in asp.net with c#

Question
-
User-962352816 posted
I have a database with urls of image. Based on the number of images i would like them to be posted in the web form as thumbnails where in each row 3 photos must be displayed.
if 10 images are there then it should be automatically read and displayed. If 15 are there then all of the 15 should be read and displ;ayed automatically in 5 rows each containing 3 images.
Please help me out with this.
Thanks in advance.
Pls do reply and ask if you need further clarification.
Thursday, July 25, 2013 1:34 AM
Answers
-
User1353852561 posted
This is the html page
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ul>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%Eval("ImageString")%>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</form>
</body>
</html>C# code is
int count = 20;
DataTable dt = new DataTable();
dt.Columns.Add("Images");
for (int i = 0; i < count; i++)
{
string images = "";
images += "<img src=\"dynamicValue"+i+"\" ></img>";
if (i < count - 1)
{
images += "<img src=\"dynamicValue" + i + "\" ></img>";
i++;
}
if (i < count - 1)
{
images += "<img src=\"dynamicValue" + i + "\" ></img>";
i++;
}
dt.Rows.Add(images);
}
Repeater1.DataSource = dt;
Repeater1.DataBind();
please feel free to ask if any clarification is needed. In this i have hard coded the count and url. that should be given dynamically
dont forget to mark this as answer if it helps
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 25, 2013 2:01 AM -
User-962352816 posted
Thanks to k.panchal, logees and ramu.net for replying.
got the code which i wanted with modifications from the one posted by logees. The code is
con = new SqlConnection("server=ILS-SYSTEM2;database=registrations;Integrated Security=true");
con.Open();
DataTable dt = new DataTable();
da = new SqlDataAdapter("select * from product", con);
da.Fill(dt);
StringBuilder sb = new StringBuilder();
string imgFormat = "<td align='center'><img src=\"{0}\" /></img></td>";string imgpath;
sb.Append("<table cellpadding=5 cellspacing=5><tr>");
for (int i = 0; i < dt.Rows.Count - 1; i++)
{
imgpath = dt.Rows[i][6].ToString();
sb.AppendFormat(imgFormat, imgpath);
}sb.Append("</tr><table cellpadding=5 cellspacing=5");
divGallery.InnerHtml = sb.ToString();
Thanks a lot
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 25, 2013 7:33 AM
All replies
-
User-265225665 posted
I suggest that use data list control, which lets u to fulfill your requirements, and its dynamic too.
http://kishor-naik-dotnet.blogspot.in/2011/10/aspnet-image-gallery-using-datalist-in.html
Thursday, July 25, 2013 1:50 AM -
User1353852561 posted
This is the html page
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ul>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%Eval("ImageString")%>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</form>
</body>
</html>C# code is
int count = 20;
DataTable dt = new DataTable();
dt.Columns.Add("Images");
for (int i = 0; i < count; i++)
{
string images = "";
images += "<img src=\"dynamicValue"+i+"\" ></img>";
if (i < count - 1)
{
images += "<img src=\"dynamicValue" + i + "\" ></img>";
i++;
}
if (i < count - 1)
{
images += "<img src=\"dynamicValue" + i + "\" ></img>";
i++;
}
dt.Rows.Add(images);
}
Repeater1.DataSource = dt;
Repeater1.DataBind();
please feel free to ask if any clarification is needed. In this i have hard coded the count and url. that should be given dynamically
dont forget to mark this as answer if it helps
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 25, 2013 2:01 AM -
User1353852561 posted
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ul>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%Eval("ImageString")%>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</form>
</body>
</html>C# Code
int count = 20;
DataTable dt = new DataTable();
dt.Columns.Add("Images");
for (int i = 0; i < count; i++)
{
string images = "";
images += "<img src=\"dynamicValue"+i+"\" ></img>";
if (i < count - 1)
{
images += "<img src=\"dynamicValue" + i + "\" ></img>";
i++;
}
if (i < count - 1)
{
images += "<img src=\"dynamicValue" + i + "\" ></img>";
i++;
}
dt.Rows.Add(images);
}
Repeater1.DataSource = dt;
Repeater1.DataBind();
Thursday, July 25, 2013 2:07 AM -
User-306750178 posted
Datalist control will be the best option for your requirement.
The sample code is below
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3"> <ItemTemplate> Here your control </ItemTemplate> </asp:DataList>
And refer below link for better understanding
Thursday, July 25, 2013 2:13 AM -
User-962352816 posted
I get your logic....but still am not able to get the dynamicValue from the image from the database. If image url is in the 7th column then
string imgpath=dr.getstring(6);
will get me value from one row. How to get the value from all the rows?
Thank you for your answer. Please help me with this.
Thursday, July 25, 2013 5:25 AM -
User-962352816 posted
Thanks to k.panchal, logees and ramu.net for replying.
got the code which i wanted with modifications from the one posted by logees. The code is
con = new SqlConnection("server=ILS-SYSTEM2;database=registrations;Integrated Security=true");
con.Open();
DataTable dt = new DataTable();
da = new SqlDataAdapter("select * from product", con);
da.Fill(dt);
StringBuilder sb = new StringBuilder();
string imgFormat = "<td align='center'><img src=\"{0}\" /></img></td>";string imgpath;
sb.Append("<table cellpadding=5 cellspacing=5><tr>");
for (int i = 0; i < dt.Rows.Count - 1; i++)
{
imgpath = dt.Rows[i][6].ToString();
sb.AppendFormat(imgFormat, imgpath);
}sb.Append("</tr><table cellpadding=5 cellspacing=5");
divGallery.InnerHtml = sb.ToString();
Thanks a lot
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 25, 2013 7:33 AM