This article shows how to show images in a data list from a directory and on the click of an image how to enlarge that image using JQuery.

The following is my aspx code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EnlargeImageOnClick.Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>jQuery: Enlarge an Image</title>
  6. <link href="StyleSheet.css" rel="stylesheet" />
  7. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  8. <style type="text/css">
  9. .imgthumb {
  10. height: 100px;
  11. width: 100px;
  12. }
  13. .imgdiv {
  14. background-color: White;
  15. margin-left: auto;
  16. margin-right: auto;
  17. padding: 10px;
  18. border: solid 1px #c6cfe1;
  19. height: 500px;
  20. width: 450px;
  21. }
  22. </style>
  23. <script type="text/javascript">
  24. $(function () {
  25. $("img.imgthumb").click(function (e) {
  26. var enlargeImage = '<img src=Images/' + $(this).attr("alt") + '></img>';
  27. $('#divImgEnlarge')
  28. .html($(enlargeImage)
  29. .animate({ height: '300', width: '450' }, 1500));
  30. });
  31. });
  32. </script>
  33. </head>
  34. <body>
  35. <form id="form1" runat="server">
  36. <table cellpadding="1" cellspacing="1" width="880px" align="center" class="BlueBorder"
  37. style="background: White;">
  38. <tr>
  39. <td style="height: 50px; background-color: skyblue; padding-left: 10px;">
  40. <span style="font-size: 20pt; font-weight: bold; color: blue;">jQuery: Image Enlarge On Image Click</span>
  41. </td>
  42. </tr>
  43. <tr>
  44. <td>
  45. <asp:DataList ID="dListImages" runat="server" RepeatColumns="8">
  46. <ItemTemplate>
  47. <table border="1">
  48. <tr>
  49. <td>
  50. <img alt='<%#Eval("Text") %>' src='<%#Eval("Value") %>' class="imgthumb" /></td>
  51. </tr>
  52. </table>
  53. </ItemTemplate>
  54. </asp:DataList>
  55. </td>
  56. </tr>
  57. <tr>
  58. <td style="text-align: center; vertical-align: central; border: solid 2px red;">
  59. <div id="divImgEnlarge"></div>
  60. </td>
  61. </tr>
  62. </table>
  63. </form>
  64. </body>
  65. </html>
My aspx.cs code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. namespace EnlargeImageOnClick
  9. {
  10. public partial class Default : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. if (!IsPostBack)
  15. {
  16. BindImages();
  17. }
  18. }
  19. public void BindImages()
  20. {
  21. string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
  22. List<ListItem> files = new List<ListItem>();
  23. foreach (string filePath in filePaths)
  24. {
  25. files.Add(new ListItem(Path.GetFileName(filePath), filePath));
  26. }
  27. dListImages.DataSource = files;
  28. dListImages.DataBind();
  29. }
  30. }
  31. }
Now run the application:

jquery
Image 1.

Click on an image.

image
Image 2.

image enlarge
Image 3.

image click
Image 4.

show image
Image 5.