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

J

James Radke

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
 
J

Jeffrey Tan[MSFT]

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==ListItemType.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==ListItemType.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top