ASP:TableRow with dynamic design

G

Guest

I have a repeater and want to set design on table row based on data values.
I found the <ASP:TableRow> which is a good candidate, but then I am forced
to have the <ASP:Table> tags within the ItemTemplate!
This is very stupid, because then I will have no correlation of the columns
in the header and the items. It worked fine to seperate the tags when I used
normal <Table> and <TR> tags.
Can I set background color or foreground color on a <TR> from code?
 
G

Guest

Yes, you can. Try something like this:
<asp:Repeater ID="Repeater1" runat="server" OnItemCreated
="Repeater1_ItemCreated">
<HeaderTemplate>
<table >
<thead>
<tr runat="server" id="tr0">
<th runat="server" id="td0">
Table header Column
</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr runat="server" id="row1">
<td runat="server" id="td1">
<asp:Label ID="lblCompany" runat="server" Text='<%#
Eval("FieldName") %>'></asp:Label>
</td>
</tr>
</tbody>
</ItemTemplate>
<FooterTemplate >
</table>
</FooterTemplate>
</asp:Repeater>

and in the codebehind you can acces the HTMLTableRows to change their style
based on data like this:

protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
HtmlTableRow row1 = (HtmlTableRow)e.Item.FindControl("row1");
if (row1 != null)
{
//chane the style based on the data
row1.Attributes.Add("class", "MyClass");
}
}
}
 
G

Guest

Beautiful!
I didn't know the HTML elements could be reached in code too ....
How easy it is when you know how to do it!
 

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