Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to Bind or Display Images from Folder to Datalist Using ASP.NET
WhatsApp
Pintoo Yadav
Mar 19
2015
5.8
k
0
2
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
Up Next
How to Bind or Display Images from Folder to Datalist Using ASP.NET