You can use OnItemDataBound to check the Session is present or not of you can use server side code in aspx side to check the Session value only one time.
HTML
<asp:DataList ID="DataList1" runat="server">
<HeaderTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="500px;">
<tr>
<td>
Id
</td>
<td>
Name
</td>
<td>
Country
</td>
<td>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblId" Text='<%# Eval("Id") %>' runat="server" />
</td>
<td>
<asp:Label ID="lblName" Text='<%# Eval("Name") %>' runat="server" />
</td>
<td>
<asp:Label ID="lblCountry" Text='<%# Eval("Country") %>' runat="server" />
</td>
<td>
<%if (Session["UserId"] == null)
{%>
<asp:HyperLink ID="lnkViewDetail" Text="Login and View" runat="server" />
<%}
else
{ %>
<asp:HyperLink ID="HyperLink1" Text="View" runat="server" />
<%} %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
NameSpace
using System.Data;
C#
If the session is present and its not equal to null then it will show you View otherwise Login and View
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Session["UserId"] = "Azim";
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
Screenshot