Hi micah,
Check this example. Now please take its reference and correct your code.
HTML
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table>
<tr>
<th>
Name
</th>
<th>
Country
</th>
<th>
Share
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblShareId" Text='<%# Eval("Id") %>' runat="server" Visible="false" />
<asp:Label ID="lblName" Text='<%# Eval("Name") %>' runat="server" />
</td>
<td>
<asp:Label ID="lblCountry" Text='<%# Eval("Country") %>' runat="server" />
</td>
<td>
<div id="hidsharepostimage" runat="server" class="pull-left" style="margin-left: 6px;">
<h4 id="name" class="list-group-item-heading" runat="server">
name</h4>
</div>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[3] {
new System.Data.DataColumn("Id"), new System.Data.DataColumn("Name"), new System.Data.DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add("", "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(null, "Robert Schidner", "Russia");
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
protected void OnItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (!string.IsNullOrEmpty((e.Item.FindControl("lblShareId") as Label).Text))
{
(e.Item.FindControl("hidsharepostimage") as System.Web.UI.HtmlControls.HtmlControl).Visible = true;
}
else
{
(e.Item.FindControl("hidsharepostimage") as System.Web.UI.HtmlControls.HtmlControl).Visible = false;
}
}
}
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 System.Data.DataTable = New System.Data.DataTable()
dt.Columns.AddRange(New System.Data.DataColumn(2) { _
New System.Data.DataColumn("Id"), New System.Data.DataColumn("Name"), New System.Data.DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add("", "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(Nothing, "Robert Schidner", "Russia")
DataList1.DataSource = dt
DataList1.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
If Not String.IsNullOrEmpty((TryCast(e.Item.FindControl("lblShareId"), Label)).Text) Then
TryCast(e.Item.FindControl("hidsharepostimage"), System.Web.UI.HtmlControls.HtmlControl).Visible = True
Else
TryCast(e.Item.FindControl("hidsharepostimage"), System.Web.UI.HtmlControls.HtmlControl).Visible = False
End If
End If
End Sub
Screenshot