Hi ashish007,
Check this example. Now please take its reference and correct your code.
HTML
<asp:DataList ID="dlImages" runat="server" RepeatColumns="2"
RepeatDirection="Horizontal" CellSpacing="3" RepeatLayout="Table">
<ItemTemplate>
<img src='<%# Eval("Path") %>' height="100" width="100" />
</ItemTemplate>
</asp:DataList>
Namespaces
C#
using System.Data;
using System.IO;
VB.Net
Imports System.Data
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images"));
List<string> paths = new List<string>();
foreach (string filePath in filePaths)
{
paths.Add("Images/" + Path.GetFileName(filePath));
}
DataTable dt = new DataTable();
dt.Columns.Add("Path");
switch (paths.Count)
{
case 1:
dt.Rows.Add(paths[0]);
dt.Rows.Add("");
dt.Rows.Add("");
dt.Rows.Add("");
break;
case 2:
dt.Rows.Add(paths[0]);
dt.Rows.Add(paths[1]);
dt.Rows.Add("");
dt.Rows.Add("");
break;
case 3:
dt.Rows.Add(paths[0]);
dt.Rows.Add("");
dt.Rows.Add(paths[1]);
dt.Rows.Add(paths[2]);
break;
case 4:
dt.Rows.Add(paths[0]);
dt.Rows.Add(paths[1]);
dt.Rows.Add(paths[2]);
dt.Rows.Add(paths[3]);
break;
default:
break;
}
dlImages.DataSource = dt;
dlImages.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim filePaths As String() = Directory.GetFiles(Server.MapPath("~/Images"))
Dim paths As List(Of String) = New List(Of String)()
For Each filePath As String In filePaths
paths.Add("Images/" & Path.GetFileName(filePath))
Next
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Path")
Select Case paths.Count
Case 1
dt.Rows.Add(paths(0))
dt.Rows.Add("")
dt.Rows.Add("")
dt.Rows.Add("")
Case 2
dt.Rows.Add(paths(0))
dt.Rows.Add(paths(1))
dt.Rows.Add("")
dt.Rows.Add("")
Case 3
dt.Rows.Add(paths(0))
dt.Rows.Add("")
dt.Rows.Add(paths(1))
dt.Rows.Add(paths(2))
Case 4
dt.Rows.Add(paths(0))
dt.Rows.Add(paths(1))
dt.Rows.Add(paths(2))
dt.Rows.Add(paths(3))
Case Else
End Select
dlImages.DataSource = dt
dlImages.DataBind()
End If
End Sub
Screenshot