Create A Masonry Image Gallery In ASP.NET

Image gallery is what we often see on sites, and I personally like the masonry image gallery. I thought to create it and when I saw the demo I didn't get the idea at first.  How could I create this div position randomly, as fitting with each other seems to be impossible without CSS or jQuery. I thought I would have to use a jQuery plugin for this, then an idea came in my mind: Why div?
 
We just need the output, so why shouldn't I do this with a table? We can do this in div also but I am not too good in CSS, so I preferred a table.
 
Here's the image and code of  the aspx page,
 
Image Aspx Page

 
 
Aspx code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>  
  2.     <!DOCTYPE html>  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title></title>  
  7.         <style>  
  8.         .inner-tr td {  
  9.             vertical-align: top;  
  10.         }  
  11.           
  12.         .one-tiles {  
  13.             height: 170px !important;  
  14.             background-color: #d77575;  
  15.         }  
  16.           
  17.         .two-tiles {  
  18.             height: 130px !important;  
  19.             background-color: #dcbc4c;  
  20.         }  
  21.           
  22.         .three-tiles {  
  23.             background-color: #a3ca3b;  
  24.             height: 300px !important;  
  25.         }  
  26.           
  27.         .four-tiles {  
  28.             background-color: #3daee3;  
  29.             height: 150px !important;  
  30.         }  
  31.           
  32.         .five-tiles {  
  33.             background-color: #bb8ed8;  
  34.             height: 270px !important;  
  35.         }  
  36.           
  37.         .six-tiles {  
  38.             background-color: #baafb1;  
  39.             height: 230px !important;  
  40.         }  
  41.         </style>  
  42.     </head>  
  43.   
  44.     <body>  
  45.         <form id="form1" runat="server">  
  46.             <div id="divmain" runat="server" class="box">  
  47.             </div>  
  48.         </form>  
  49.     </body>  
  50.   
  51. </html>  
I shared the .aspx page code above, we created six classes with different heights and background colors. I took a div, gave an id and made it runnable at the server side by giving runat="server", so that we can access this at code behind.
 
Code Behind
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading;  
  5. using System.Web;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. public partial class Default2: System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         String strGallery = String.Empty;  
  13.         strGallery = strGallery + "<table style='width:100%;height:800px; border-collapse: collapse;'><tr>";  
  14.         for (Int32 i = 0; i < 5; i++)  
  15.         {  
  16.             strGallery = strGallery + "<td style='vertical-align: top;'><table style='width:100%;' cellspacing='1'>";  
  17.             for (Int32 j = 0; j <= 5; j++)  
  18.             {  
  19.                 Thread.Sleep(10);  
  20.                 Int32 rndnumber = RandomNumber();  
  21.                 String ClassName = String.Empty;  
  22.                 if (rndnumber == 1)  
  23.                 {  
  24.                     ClassName = "one-tiles";  
  25.                 }  
  26.                 else if (rndnumber == 2)  
  27.                 {  
  28.                     ClassName = "two-tiles";  
  29.                 }  
  30.                 else if (rndnumber == 3)  
  31.                 {  
  32.                     ClassName = "three-tiles";  
  33.                 }  
  34.                 else if (rndnumber == 4)  
  35.                 {  
  36.                     ClassName = "four-tiles";  
  37.                 }  
  38.                 else if (rndnumber == 5)  
  39.                 {  
  40.                     ClassName = "five-tiles";  
  41.                 }  
  42.                 else  
  43.                 {  
  44.                     ClassName = "six-tiles";  
  45.                 }  
  46.                 strGallery = strGallery + "<tr class='inner-tr'><td class='" + ClassName + "'></td></tr>";  
  47.             }  
  48.             strGallery = strGallery + "</table></td>";  
  49.         }  
  50.         strGallery = strGallery + "</tr></table>";  
  51.         divmain.InnerHtml = strGallery;  
  52.     }  
  53.     public Int32 RandomNumber()  
  54.     {  
  55.         Random _r = new Random();  
  56.         Int32 number = _r.Next(1, 7);  
  57.         return number;  
  58.     }  
  59. }  
Description
 
In the above code we did the logic for the image gallery. We are generating a dynamic html table here and putting this in a string. We have taken two loops out here, the outer one for the number of columns we want to show in the page and the inner one for the number of rows we want in image gallery.
 
We created a random number generator function, which will generate a number between 1 to 6 and place class randomly according to random number through an if else condition, each time a random number will come, so we basically don't know what number will come but we know the number will be between 1 to 6 and we have logic for that. And at last we placed this within a div which we made runnable at server. Now our code is done and when we run the application it will generate the output like this,
 
 
You can place images in it, for now I am just giving the background color. Each time page refreshes, the random box position will be visible.