HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Photo gallery user control

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
usergallerycontrolphoto

Problem

I am trying to create something that is very reusable here so that I can use it in other sites for other pages etc.

This code is for a simple photo gallery,

I took the code from here, but I changed it a bit and would like to add more functionality to it eventually, though.

PhotoGallery.ascx



#ImageGallery {
overflow:auto;
height:500px;
width:330px;
display:block;
}
#GalleryContainer {
width:300px;
align-content:center;
border-right:3px solid #EEEEEE;
}
.thumbnails {
cursor:pointer;
width:100px;
}

$(document).ready(function () {
$('#ImageGallery img').click(function () {
var bigImagePath = $(this).attr('src');
$('#bigImage').attr('src', bigImagePath);
});
});

Before and After Pictures






' alt='' />











PhotoGallery.ascx.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PhotoGallery : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateImages();
        }
    }

    public string FolderPath {get;set;}

    private void PopulateImages()
    {
        List myImages = new List();
        DirectoryInfo DI = new DirectoryInfo(Server.MapPath(FolderPath));
        foreach (var file in DI.GetFiles())
        {
            myImages.Add(new Image { ImageUrl = FolderPath + file.Name });
        }
        Repeater1.DataSource = myImages;
        Repeater1.DataBind();
    }
}

Solution

-
the variable holding the DirectoryInfo object should be named using lower case.

-
you should always use Sytem.IO.Path.Combine() to combine a folder with a filename.

-
you could use DirectoryInfo.EnumerateFiles combined with the Select() and assign the returned IEnumerable to the DataSource property of the Repeater control.

-
you should add spacing to the property FolderPath. Instead of

public string FolderPath {get;set;}


you should use

public string FolderPath { get; set; }


-
Repeater1 is an ugly name which isn't decsriptive either. Change it to something more descriptive.

Code Snippets

public string FolderPath {get;set;}
public string FolderPath { get; set; }

Context

StackExchange Code Review Q#74841, answer score: 4

Revisions (0)

No revisions yet.