Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Set repeater row background color based on data value - how?

Reply
Thread Tools

Set repeater row background color based on data value - how?

 
 
James Radke
Guest
Posts: n/a
 
      01-20-2004
Hello,

If I have a repeater with an ItemTemplate like:

<ItemTemplate>
<tr valign="Top" class="normal">
<td width="70px" align="center" valign="middle">
<asp:TextBox ID="txtEstDate1" runat="server" Text='<%#
format(DataBinder.Eval(Container, "DataItem.EstProduction"), "MM/dd/yy") %>'
CssClass="ReportDataSmall" Width="60px" MaxLength="8">
</asp:TextBox>
</td>
<td valign="middle" width="70px"><%#
Format(DataBinder.Eval(Container.DataItem, "DueDate"),"MM/dd/yy") %></td>
<td align="center" valign="middle" width="200px"><%#
DataBinder.Eval(Container.DataItem, "Name") %></td>
<td align="center" valign="middle" width="180px"><%#
DataBinder.Eval(Container.DataItem, "Grade") %></td>
<td align="center" valign="middle" width="110px"><%#
DataBinder.Eval(Container.DataItem, "Color") %></td>
<td align="center" valign="middle" width="110px"><%#
DataBinder.Eval(Container.DataItem, "Pattern") %></td>
<td align="center" valign="middle" width="80px"><%#
DataBinder.Eval(Container.DataItem, "SlitWidth") %></td>
<td align="center" valign="middle" width="120px"><%#
DataBinder.Eval(Container.DataItem, "OrderNum") & "-" &
DataBinder.Eval(Container.DataItem, "Line") & "-" &
DataBinder.Eval(Container.DataItem, "Delivery") %></td>
<td align="center" valign="middle" width="85px"><%#
DataBinder.Eval(Container.DataItem, "quantity") %></td>
</tr>
</ItemTemplate>

And I have another value field in the datatable called "Scheduled" (boolean
field), is there a way for me, in the OnItemDataBound procedure to compare
the value of "Scheduled" and if true then set the items TR class to
something else which would set the background color of the entire row (ie
"backgroundhighlighted")?

Or, is there some other way that I can set the background color of the row
based on the value of an item in the data?

Any examples / pointers would be wonderful!

Thanks!

Jim


 
Reply With Quote
 
 
 
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      01-21-2004

Hi James,

Thank you for using MSDN Newsgroup! My name is Jeffrey, and I will be
assisting you on this issue.

Based on my understanding, you want to set the <tr> back color determine
through the database value in server side.

================================================== ====================
Based on my research and repeater code below(I bind the repeater control to
SqlServer default table"jobs" in database "pubs" ):
<HeaderTemplate>
<table border="1">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td width="70px" align="center" valign="middle">
<asp:TextBox ID="txtEstDate1" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "job_id") %>' Width="60px"
MaxLength="8">
</asp:TextBox>
</td>
<td valign="middle" width="70px">
<%#DataBinder.Eval(Container.DataItem, "job_desc") %>
</td>
<td valign="middle" width="70px">
<%#DataBinder.Eval(Container.DataItem, "min_lvl") %>
</td>
<td valign="middle" width="70px" onclick="javascript:alert('x')">
<%#DataBinder.Eval(Container.DataItem, "max_lvl") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>

If you want to change the textbox's back judge by "job_id", you can do like
this:
private void Repeater1_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
RepeaterItem ri=e.Item;

if(ri.ItemType==ListItemType.Item||ri.ItemType==Li stItemType.AlternatingItem
)
{
foreach(Control c in ri.Controls)
{
if(c is System.Web.UI.WebControls.TextBox)
{
if(c is System.Web.UI.WebControls.TextBox)
{
if(Convert.ToInt32(((TextBox)c).Text)>5)
{
((TextBox)c).BackColor=Color.Red;
}
}
}
}
}
}

Note: Asp.Net will not encapsulate the html tag without "runat=server"
attribute into server control, so all the <tr><td> will not treat as server
control.
So if you want to change the entire <tr> back color, you should add
"runat=server" attribute to <tr> tag: <tr runat=server>
Then, when ItemDataBound event, you should do like this:

private void Repeater1_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
RepeaterItem ri=e.Item;

if(ri.ItemType==ListItemType.Item||ri.ItemType==Li stItemType.AlternatingItem
)
{
foreach(Control c in ri.Controls)
{
if(c is System.Web.UI.HtmlControls.HtmlTableRow)
{
HtmlTableRow tr=(HtmlTableRow)c;
foreach(Control c1 in tr.Controls)
{
if(c1 is System.Web.UI.HtmlControls.HtmlTableCell)
{
foreach(Control c2 in c1.Controls)
{
if(c2 is System.Web.UI.WebControls.TextBox)
{
if(Convert.ToInt32(((TextBox)c2).Text)>5)
{
tr.BgColor="Red";
}
}
}
}
}
}
}
}
}

================================================== ===============
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Hope you have a nice experience on using Microsoft Newsgroup!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change the row color of the Repeater based on some condition? Michael ASP .Net 4 06-20-2008 06:29 AM
Set Background Color Using Column Value? TF ASP .Net 10 03-20-2007 11:24 AM
repeater control - change the background color depending on a value bill ASP .Net Web Controls 2 03-20-2006 08:00 PM
GridView: Row background color= red if row ID = 3 G. Whiz ASP .Net 1 02-24-2006 03:52 PM
ok I can do a totals row but how about a percentage row after each data row D ASP .Net Datagrid Control 0 05-23-2005 04:10 PM



Advertisments