Click here to Skip to main content
15,891,726 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How can I set a table td color in my repeater based on the values databound to that row in ASP.NET?
Posted
Comments
carlos.ma 8-Oct-12 15:26pm    
Could you share your code to see how you are trying to do that, please?

You can assign the controls in the td with an ID, Loop through the items in the repeater control and then use FindControl("<id>") to find the respective control and assign the background color property or change the css.

If the above didn't work, as an alternative
You can create a Label control inside the td and bind the text to it. Now try finding the Label control and change its color.

XML
<td>
 <asp:Label ID="lbl1" runat="server" Text= <%#Container.DataItem("title")%>>  </asp:Label>
</td>


 foreach (RepeaterItem item2 in Repeater1.Items) 
{ 
        if (Convert.ToInt32(dr["Quantity"].ToString()) > 1)
        { 
            Label lbl= (Label)item2.FindControl("lbl1");
            lbl.Attributes.Add("style", "background-color:Green;"); 
        } 
}
 
Share this answer
 
v3
Comments
Surendra0x2 9-Oct-12 2:05am    
public void changecolor()
{



string SqlConnect = System.Configuration.ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;
SqlConnection Sqlconn = new SqlConnection(SqlConnect);
SqlCommand sqlcomm = new SqlCommand("select * from Addstock", Sqlconn);
Sqlconn.Open();
SqlDataReader dr = sqlcomm.ExecuteReader();
if (dr.Read())
{


foreach (RepeaterItem item2 in Repeater1.Items)
{

if (Convert.ToInt32(dr["Quantity"].ToString()) > 1)
{
HtmlTableCell td = (HtmlTableCell)item2.FindControl("TD1");
td.BgColor = System.Drawing.Color.Green.ToString();
//td.Attributes.Add("style", "background-color:Green;");

}

}
Repeater1.DataSource = dr;
Repeater1.DataBind();

dr.Close();
Sqlconn.Close();

}


}

here is my code sir but its not working :( can u help me plz
Teenustar 9-Oct-12 10:08am    
I have updated my solution. check out.
Surendra0x2 11-Oct-12 7:58am    
public void changecolor()
{



string SqlConnect = System.Configuration.ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;
SqlConnection Sqlconn = new SqlConnection(SqlConnect);
SqlCommand sqlcomm = new SqlCommand("select * from Addstock", Sqlconn);
Sqlconn.Open();
SqlDataReader dr = sqlcomm.ExecuteReader();
if (dr.Read())
{


foreach (RepeaterItem item2 in Repeater1.Items)
{

if (Convert.ToInt32(dr["Quantity"].ToString()) > 2)
{
Label lbl = (Label)item2.FindControl("Label3");
lbl.Attributes.Add("style", "background-color:Green;");

}
else if (Convert.ToInt32(dr["Quantity"].ToString()) < 2)
{

Label lbl = (Label)item2.FindControl("Label3");
lbl.Attributes.Add("style", "background-color:Red;");

}


}
Repeater1.DataSource = dr;
Repeater1.DataBind();

dr.Close();
Sqlconn.Close();

}

My Cs page

and

My aspx code is
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table class="rounded-corners">
<tr style="background-color:#F58810" class="rounded-corners">
<th>Select</th>
<th>Stock ID</th>
<th>Category</th>
<th>Product Name</th>
<th>Price(in Rs)</th>
<th>Quantity</th>
<th>Warranty</th>
<th>Purchasing Date</th>
<th>Edit</th>

</tr>




</HeaderTemplate>

<itemtemplate>

<tr>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" /></td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></td>
<td><%#Eval("CatName") %></td>
<td><%#Eval("ProductName") %></td>
<td><%#Eval("ProductPrice") %></td>
<td >
<asp:Label ID="Label3" runat="server" Text='<%#Eval("Quantity") %>'></td>
<td><%#Eval("warranty") %></td>
<td><%#Eval("PurchasingDate") %></td>
<td><img src="Icons/1349549314_edit.ico" alt="" /></td>
</tr>

<%-- <itemtemplate>

--%>
<footertemplate>
</table>



}


but still not working sir can u plz check my code that what mistake i'm doing.
Surendra0x2 11-Oct-12 8:45am    
Thanks sir i Found the Solution
here it is
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Label lbl = (Label)e.Item.FindControl("Label3");
if (Convert.ToInt32(lbl.Text) > 2)
{
lbl.BackColor = System.Drawing.Color.Green;
}
}
}

thanks for giving me ur precious tym :) god bless you :)
Hi,

Please see the below link. It might be help you.
On ItemDataBound Event you can find the td, and you can set the color as per your required condition .

C#
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HtmlTableCell td = (HtmlTableCell)e.Item.FindControl("TD1"); //Where TD1 is the ID of the Table Cell
            if ("YOUR CONDITION")
            {
                td.Attributes.Add("style", "background-color:Green;");
            }
            else
            {
                td.Attributes.Add("style", "background-color:Yellow;");
            }
         }
}



http://www.asp.net/web-forms/tutorials/data-access/displaying-data-with-the-datalist-and-repeater/formatting-the-datalist-and-repeater-based-upon-data-vb[^]
 
Share this answer
 
Comments
Surendra0x2 11-Oct-12 8:44am    
Thanks a Ton Buddy Your Gr8 Sir :) You solved My problem :)

here is my code and its working now :)
Surendra0x2 11-Oct-12 8:44am    
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Label lbl = (Label)e.Item.FindControl("Label3");
if (Convert.ToInt32(lbl.Text) > 2)
{
lbl.BackColor = System.Drawing.Color.Green;
}
}
}
chandrabhan bhardwaj 4-May-16 5:47am    
can you sen me above code behind design view please?
chandrabhan bhardwaj 4-May-16 5:47am    
can u send me above code behind design view please?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900