Home All Groups Group Topic Archive Search About

ASP:TableRow with dynamic design

Author
7 Jan 2006 12:41 PM
Jakob Lithner
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?

Author
7 Jan 2006 2:19 PM
Phillip Williams
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");
        }
    }
}
Show quoteHide quote
"Jakob Lithner" wrote:

> 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?
Are all your drivers up to date? click for free checkup

Author
7 Jan 2006 2:29 PM
Phillip Williams
Correction:
------------
the <tbody> section should have been split between the <HeaderTemplate> and
the <ItemTemplate> in the code posted below.

Show quoteHide quote
"Phillip Williams" wrote:

> 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");
>         }
>     }
> }
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Jakob Lithner" wrote:
>
> > 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?
Author
7 Jan 2006 4:22 PM
Jakob Lithner
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!

Bookmark and Share