Hi Mehram,
Check this sample. now take its reference and correct your code.
HTML
<asp:DataList ID="dlImages" runat="server" DataKeyField="Name" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("ID") %>
</td>
<td>
<%# Eval("Name") %>
</td>
<td>
<asp:Image ID="Image1" runat="server" Width="100px" Height="100px" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("Path");
dt.Rows.Add("1", "Chrysanthemum", "Images\\Chrysanthemum.jpg");
dt.Rows.Add("2", "Desert", "Images\\Desert.jpg");
dt.Rows.Add("3", "Hydrangeas", "Images\\Hydrangeas.jpg");
dt.Rows.Add("4", "Jellyfish", "Images\\Jellyfish.jpg");
dt.Rows.Add("5", "Koala", "Images\\Koala.jpg");
dt.Rows.Add("6", "Lighthouse", "Images\\Lighthouse.jpg");
this.dlImages.DataSource = dt;
this.dlImages.DataBind();
}
}
protected void OnItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string name = this.dlImages.DataKeys[e.Item.ItemIndex].ToString();
Image img = e.Item.FindControl("Image1") as Image;
img.ImageUrl = "Images/" + name + ".jpg";
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.Add("ID")
dt.Columns.Add("Name")
dt.Columns.Add("Path")
dt.Rows.Add("1", "Chrysanthemum", "Images\Chrysanthemum.jpg")
dt.Rows.Add("2", "Desert", "Images\Desert.jpg")
dt.Rows.Add("3", "Hydrangeas", "Images\Hydrangeas.jpg")
dt.Rows.Add("4", "Jellyfish", "Images\Jellyfish.jpg")
dt.Rows.Add("5", "Koala", "Images\Koala.jpg")
dt.Rows.Add("6", "Lighthouse", "Images\Lighthouse.jpg")
Me.dlImages.DataSource = dt
Me.dlImages.DataBind()
End If
End Sub
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim name As String = Me.dlImages.DataKeys(e.Item.ItemIndex).ToString()
Dim img As Image = TryCast(e.Item.FindControl("Image1"), Image)
img.ImageUrl = "Images/" & name & ".jpg"
End If
End Sub
Screenshot