ASP:TableRow with dynamic design

Discussion in 'ASP .Net' started by =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?=, Jan 7, 2006.

  1. 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?
    =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?=, Jan 7, 2006
    #1
    1. Advertising

  2. 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?
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 7, 2006
    #2
    1. Advertising

  3. Correction:
    ------------
    the <tbody> section should have been split between the <HeaderTemplate> and
    the <ItemTemplate> in the code posted below.

    "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?
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 7, 2006
    #3
  4. 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!
    =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?=, Jan 7, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frank
    Replies:
    0
    Views:
    324
    frank
    Jul 11, 2003
  2. Steve - DND

    Click Event for TableRow?

    Steve - DND, Jul 31, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    497
    Steve - DND
    Jul 31, 2003
  3. Coleen

    Span in an ASP TableRow

    Coleen, Jan 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    428
    coleenholley
    Jan 26, 2004
  4. hb
    Replies:
    6
    Views:
    2,024
    =?Utf-8?B?IkplZmZyZXkgVGFuW01TRlRdIg==?=
    Mar 4, 2004
  5. keithb
    Replies:
    2
    Views:
    4,251
    Mark Rae
    Feb 16, 2006
Loading...

Share This Page