Home > ASP.Net > Display all images from folder with Repeater

Display all images from folder with Repeater


Hi all,

try this example to Display all images from folder with Repeater

1) Open VS2008 and create a new website and add new web page name it “Default.aspx”

2) In Website add new folder name it “images” and add any images inside this folder

3) Open Default.aspx and add Repeater control from Toolbox


<asp:Repeater ID="FileRepeater" runat="server">
 <ItemTemplate>
 <asp:Images ID="img" ImageUrl='<%#"Images/" + Eval("Name") %>' runat="server" Width="120px" Hight="100px" />
 </ItemTemplate>
</asp:Repeater>

In code behind :-


public List<FileInfo> GetImageNames()
{
 string imgPath = Server.MapPath("~/Images/");
 List<FileInfo> images = new List<FileInfo>();
 DirectoryInfo directoryInfo = new DirectoryInfo(imgPath);
 FileInfo[] fileInfo = directoryInfo.GetFiles();
 foreach (FileInfo file in fileInfo)
 {
 images.Add(file);
 }
 return images;
}

protected void Page_Load(object sender, EventArgs e)
{
 FileRepeater.DataSource = GetImageNames();
 FileRepeater.DataBind();
}

Another Way :-


protected void Page_Load(object sender, EventArgs e)
{
 if (!IsPostBack)
 {
 ListImages();
 }
}
private void ListImages()
{
 DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/images/"));
 FileInfo[] file = dir.GetFiles();
 ArrayList list = new ArrayList();
 foreach (FileInfo info in file)
 {
 if (info.Extension == ".jpg" || info.Extension == ".jpeg" || info.Extension == ".gif")
 {
 list.Add(info);
 }
 }
 Repeater1.DataSource = list;
 Repeater1.DataBind();
}

Hope this help

Good Luck

Advertisements
Categories: ASP.Net
  1. khadija
    July 27, 2012 at 8:06 am

    Thanks Loads for your help, I have been looking or this solution all night, we always strive for the best.
    khadija

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: