Hi sat,
Check below example.
HTML
<asp:DataList runat="server" ID="dlStudentsDetails" OnItemDataBound="OnItemDataBound">
<ItemTemplate>
<table>
<tr>
<td>StudentId</td>
<td>
<asp:Label ID="lblID" Text='<%# Eval("StudentID") %>' runat="server" /></td>
</tr>
<tr>
<td>Phy</td>
<td>
<asp:Label ID="lblPhy" Text='<%# Eval("Phy") %>' runat="server" /></td>
</tr>
<tr>
<td>Chm</td>
<td>
<asp:Label ID="lblChm" Text='<%# Eval("Chm") %>' runat="server" /></td>
</tr>
<tr>
<td>Mth</td>
<td>
<asp:Label ID="lblMth" Text='<%# Eval("Mth") %>' runat="server" /></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Namespaces
C#
using System.Data;
using System.Drawing;
VB.Net
Imports System.Data
Imports System.Drawing
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindStudentSession();
this.BindDetails();
}
}
private void BindStudentSession()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("StudentID"), new DataColumn("Attended"), new DataColumn("FeesPaid") });
dt.Rows.Add("0002", "Yes", "No");
dt.Rows.Add("0004", "No", "No");
Session["Students"] = dt;
}
private void BindDetails()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("StudentID"), new DataColumn("Phy"), new DataColumn("Chm"), new DataColumn("Mth") });
dt.Rows.Add("0001", "98", "95", "99");
dt.Rows.Add("0002", "95", "86", "94");
dt.Rows.Add("0003", "94", "97", "93");
dt.Rows.Add("0004", "95", "96", "95");
this.dlStudentsDetails.DataSource = dt;
this.dlStudentsDetails.DataBind();
}
protected void OnItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string id = ((e.Item as DataListItem).FindControl("lblID") as Label).Text;
DataTable dt = Session["Students"] as DataTable;
foreach (DataRow row in dt.Rows)
{
string sId = row["StudentID"].ToString();
if (id == sId)
{
((e.Item as DataListItem).FindControl("lblID") as Label).BackColor = Color.Yellow;
break;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Me.BindStudentSession()
Me.BindDetails()
End If
End Sub
Private Sub BindStudentSession()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("StudentID"), New DataColumn("Attended"), New DataColumn("FeesPaid")})
dt.Rows.Add("0002", "Yes", "No")
dt.Rows.Add("0004", "No", "No")
Session("Students") = dt
End Sub
Private Sub BindDetails()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("StudentID"), New DataColumn("Phy"), New DataColumn("Chm"), New DataColumn("Mth")})
dt.Rows.Add("0001", "98", "95", "99")
dt.Rows.Add("0002", "95", "86", "94")
dt.Rows.Add("0003", "94", "97", "93")
dt.Rows.Add("0004", "95", "96", "95")
Me.dlStudentsDetails.DataSource = dt
Me.dlStudentsDetails.DataBind()
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 id As String = TryCast((TryCast(e.Item, DataListItem)).FindControl("lblID"), Label).Text
Dim dt As DataTable = TryCast(Session("Students"), DataTable)
For Each row As DataRow In dt.Rows
Dim sId As String = row("StudentID").ToString()
If id = sId Then
TryCast((TryCast(e.Item, DataListItem)).FindControl("lblID"), Label).BackColor = Color.Yellow
Exit For
End If
Next
End If
End Sub
Screenshot