TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Bind or Display Images from Folder to Datalist Using ASP.NET
Pintoo Yadav
Mar 19
2015
Code
5.7
k
0
2
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
HTML code
<
div
>
<
div
class
=
"clear hideSkiplink"
>
<
asp:Menu
ID
=
"NavigationMenu"
runat
=
"server"
CssClass
=
"menu"
EnableViewState
=
"false"
IncludeStyleBlock
=
"false"
Orientation
=
"Horizontal"
>
<
Items
>
<
asp:MenuItem
NavigateUrl
=
"~/Default.aspx"
Text
=
"Home"
/>
<
asp:MenuItem
NavigateUrl
=
"~/About.aspx"
Text
=
"About"
/>
</
Items
>
</
asp:Menu
>
</
div
>
</
div
>
<
div
>
<
asp:FileUpload
ID
=
"fileupload1"
runat
=
"server"
/>
<
asp:Button
ID
=
"btnsave"
runat
=
"server"
Text
=
"Upload"
onclick
=
"btnsave_Click"
/>
</
div
>
<
div
>
<
asp:DataList
ID
=
"dtlist"
runat
=
"server"
RepeatColumns
=
"4"
CellPadding
=
"5"
>
<
ItemTemplate
>
<
asp:Image
Width
=
"100"
ID
=
"Image1"
ImageUrl
=
'<%# Bind("Name", "~/Images/{0}") %>'
runat
=
"server"
/>
<
br
/>
<
asp:HyperLink
ID
=
"HyperLink1"
Text
=
'<%# Bind("Name") %>'
NavigateUrl
=
'<%# Bind("Name", "~/Images/{0}") %>'
runat
=
"server"
/>
</
ItemTemplate
>
<
ItemStyle
BorderColor
=
"Brown"
BorderStyle
=
"dotted"
BorderWidth
=
"3px"
HorizontalAlign
=
"Center"
VerticalAlign
=
"Bottom"
/>
</
asp:DataList
>
</
div
>
C# code
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.IO;
using
System.Collections;
namespace
DemoGrid {
public
partial
class
index: System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindDataList();
}
}
protected
void
BindDataList()
{
DirectoryInfo dir =
new
DirectoryInfo(MapPath(
"Images"
));
FileInfo[] files = dir.GetFiles();
ArrayList listItems =
new
ArrayList();
foreach
(FileInfo info
in
files)
{
listItems.Add(info);
}
dtlist.DataSource = listItems;
dtlist.DataBind();
}
protected
void
btnsave_Click(
object
sender, EventArgs e)
{
string
filename = Path.GetFileName(fileupload1.PostedFile.FileName);
fileupload1.SaveAs(Server.MapPath(
"Images/"
+ filename));
BindDataList();
}
}
}
Display Images from Folder
ASP.NET
Datalist Images