Hey micah,
Please refer below sample.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.container
{
position: relative;
width: 100%;
}
.container .img
{
width: 100%;
height: auto;
}
.container .btn
{
position: absolute;
top: 80%;
left: 35%;
color: Red;
text-align: right;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<div class="container">
<asp:Label Text='<%#Eval("Desc") %>' ID="lblName" runat="server" />
<asp:Image ID="imgItem" runat="server" ImageUrl='<%#Eval("Url") %>' Alt='<%# Eval("Desc") %>'
CssClass="img" Style="width: 100%" />
<asp:Button Text="Show more" runat="server" ID="btnShow" Visible="false" />
</div>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataList1.DataSource = GetData();
DataList1.DataBind();
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Desc"), new DataColumn("Url") });
dt.Rows.Add("Image 1", "http://static.flickr.com/66/199481236_dc98b5abb3_s.jpg");
dt.Rows.Add("Image 2", "http://static.flickr.com/58/199481143_3c148d9dd3_s.jpg");
dt.Rows.Add("Image 3", "");
return dt;
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
DataTable dt = GetData();
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (string.IsNullOrEmpty((e.Item.FindControl("imgItem") as Image).ImageUrl))
{
(e.Item.FindControl("btnShow") as Button).Visible = false;
}
else
{
(e.Item.FindControl("btnShow") as Button).Visible = true;
(e.Item.FindControl("btnShow") as Button).CssClass = "buttonstyle";
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DataList1.DataSource = GetData()
DataList1.DataBind()
End If
End Sub
Private Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Desc"), New DataColumn("Url")})
dt.Rows.Add("Image 1", "http://static.flickr.com/66/199481236_dc98b5abb3_s.jpg")
dt.Rows.Add("Image 2", "http://static.flickr.com/58/199481143_3c148d9dd3_s.jpg")
dt.Rows.Add("Image 3", "")
Return dt
End Function
Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
Dim dt As DataTable = GetData()
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
If String.IsNullOrEmpty((TryCast(e.Item.FindControl("imgItem"), Image)).ImageUrl) Then
TryCast(e.Item.FindControl("btnShow"), Button).Visible = False
Else
TryCast(e.Item.FindControl("btnShow"), Button).Visible = True
TryCast(e.Item.FindControl("btnShow"), Button).CssClass = "buttonstyle"
End If
End If
End Sub
Screenshot