-->

How to Create Simple Image Gallery using repeater control & Jquery in ASP.NET

Introduction

In this post I am explain how to Create Simple Image Gallery using repeater control & Jquery in ASP.NET



Steps :

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Folder for Store Image Thumbnail.

Go to Solution Explorer > Right Click on Solution Explorer > Add > New Folder > Enter folder name. (Here in this example I have used "Thumbnail" folder for store image thumbnail)

Step-3: Add a Folder for Store Big Image.

Go to Solution Explorer > Right Click on Solution Explorer > Add > New Folder > Enter folder name. (Here in this example I have used "Images" folder for store Actual Image)

Step-4: Add a Webpage and Design for Sample Image Gallery

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

HTML Code
  1. <h3>ASP.NET Simple Image Gallery</h3>
  2. <table border="0" cellpadding="0" cellspacing="0" width="100%">
  3. <tr>
  4. <td width="150px" valign="top" align="center" style="border-right:3px solid #EEEEEE">
  5. <div id="ImageGallery" style="overflow:auto; height:350px; width:130px; display:inline-block;">
  6. <asp:Repeater ID="Repeater1" runat="server">
  7. <ItemTemplate>
  8. <img src='/Thumbnail/<%#Eval("FileName") %>' alt='<%#Eval("FileName") %>' width="100px" style="cursor:pointer" />
  9. </ItemTemplate>
  10. </asp:Repeater>
  11. </div>
  12. </td>
  13. <td valign="top" align="center">
  14. <img id="bigImage" alt="" />
  15. </td>
  16. </tr>
  17. </table>

Step-5: Write following jquery code for Simple image gallery.


  1. <script src="Scripts/jquery-1.7.1.js"></script>
  2. <script language="javascript" type="text/javascript">
  3. $(document).ready(function () {
  4. $('#ImageGallery img').click(function () {
  5. var bigImagePath = '/Images/' + $(this).attr('alt');
  6. $('#bigImage').attr('src', bigImagePath);
  7. });
  8. });
  9. </script>

Step-6: Write following code in page_load event for load thumbnail images.


  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. PopulateImages();
  6. }
  7. }
Here is the function...
  1. private void PopulateImages()
  2. {
  3. List<MyImages> myImages = new List<MyImages>();
  4. DirectoryInfo DI = new DirectoryInfo(Server.MapPath("~/Thumbnail"));
  5. foreach (var file in DI.GetFiles())
  6. {
  7. myImages.Add(new MyImages { FileName = file.Name });
  8. }
  9. Repeater1.DataSource = myImages;
  10. Repeater1.DataBind();
  11. }
In this example I have used this class.
  1. public class MyImages
  2. {
  3. public string FileName { get; set; }
  4. }

Step-7: Run Application.

Live Demo   Download


Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.

Related Posts: