Repeater : Format Particular Cell in ItemDataBound using HtmlTableCell


In My one of the Project i need to display Data in repeater control with certain criteria.Criteria like i need to highlight particular cell when it is null so i want to change background of that cell so that it hightlight.so i feel this might be helpful to you also. so i m writing this post.It really very useful , u can also use this in yr application when need arise.

======================================================================
ASPX :
======================================================================

<asp:Repeater ID=”rpt” runat=”server” OnItemDataBound=”rpt_ItemDataBound” >
<ItemTemplate>
<tr>
<td id=”tdmon” runat=”server” align=”center”>
<asp:Label ID=”lblMon” runat=”server” Text='<%# Eval(“monReason”) %>’></asp:Label>
<asp:LinkButton ID=”lnkMon” runat=”server”></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>

======================================================================
CS :
======================================================================

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (string.IsNullOrEmpty(((Label)e.Item.FindControl(“lblMon”)).Text))
{
((LinkButton)e.Item.FindControl(“lnkMon”)).Text = “Select”;

}
else
{

((HtmlTableCell)e.Item.FindControl(“tdmon”)).Attributes.Add(“class”, “Cell_Sceduleing_Highlight”);
}
}

}

Happy Programming !!!

Advertisements

4 thoughts on “Repeater : Format Particular Cell in ItemDataBound using HtmlTableCell

  1. Thanks for this code… but i have a doubt .
    Pl check the line

    Dim cell As HtmlTableCell
    If UCase(e.Item.DataItem(“Category”)).ToString.Trim = UCase(“Actual”).Trim Then
    cell = CType(e.Item.FindControl(“PreApr_Bud”), HtmlTableCell)

    ** If Not String.IsNullOrEmpty(cell.InnerHtml.ToString) Then
    cell.Attributes.Add(“color”, “red”)
    Else
    Some code
    Endif
    i have to color a particular cell based on the above condn but it gives me an error on the line marked ** as “Object reference not set to an instance of an object. ”

    Pl help

  2. Hello there, You have done an excellent job. I’ll certainly digg it and personally suggest to my friends. I’m
    confident they will be benefited from this site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s