DataBinder.Eval(Container.DataItem ... What is the context of Container?

Discussion in 'ASP .Net Web Controls' started by Joe Rattz, Sep 3, 2004.

  1. Joe Rattz

    Joe Rattz Guest

    I am trying to perform some logic inside a Repeater control's template. I
    need that logic dependent on the bound data. If my bound data is < 0, I
    want to write one string (a CSS class), otherwise I want to write a
    different string (another CSS class). Here is what I am trying to do:

    <%
    if(Double.Parse(DataBinder.Eval(Container.DataItem, "Amount")) < 0)
    { Response.Write("<span class=\"NewspaperCreditItemAmountN\">"); }
    else
    { Response.Write("<span class=\"NewspaperCreditItemAmountP\">"); }
    %>

    I have formatted the code a little funky so it is easier to read. As you
    can see, I want to parse the Amount field into a double and if its < 0, I
    want to specify one CSS class, otherwise I want to specify a different
    class.

    Unfortunately, this code does not work...it doesn't like me accessing
    Container this way. I can't seem to find any decent doc on this, but what
    is the difference between a <% %> block and a <%# %> block (notice the #)?

    I assume the Container object is a member of something. What object can I
    get to the Container object from?

    Any help would be appreciated. Ultimately, I am trying to change the CSS
    style so the dollar amount can be red if negative, otherwise black.

    Thanks.
    Joe Rattz, Sep 3, 2004
    #1
    1. Advertising

  2. Joe Rattz

    Teemu Keiski Guest

    With Repeater Container refers to the current RepeaterItem (basically row)
    instance.

    You might want to have a look at so called helper function to modularize
    what you are trying to do. Here is an article discussing it:
    http://aspalliance.com/31

    Though the example is with DataGrid, the idea is same for all data-bound
    controls like DataGrid, DataList and Repeater.

    <% ... %> means just evaluating an expression <%# ... %> means data-binding
    expression

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke





    "Joe Rattz" <> wrote in message
    news:...
    > I am trying to perform some logic inside a Repeater control's template. I
    > need that logic dependent on the bound data. If my bound data is < 0, I
    > want to write one string (a CSS class), otherwise I want to write a
    > different string (another CSS class). Here is what I am trying to do:
    >
    > <%
    > if(Double.Parse(DataBinder.Eval(Container.DataItem, "Amount")) < 0)
    > { Response.Write("<span class=\"NewspaperCreditItemAmountN\">"); }
    > else
    > { Response.Write("<span class=\"NewspaperCreditItemAmountP\">"); }
    > %>
    >
    > I have formatted the code a little funky so it is easier to read. As you
    > can see, I want to parse the Amount field into a double and if its < 0, I
    > want to specify one CSS class, otherwise I want to specify a different
    > class.
    >
    > Unfortunately, this code does not work...it doesn't like me accessing
    > Container this way. I can't seem to find any decent doc on this, but what
    > is the difference between a <% %> block and a <%# %> block (notice the #)?
    >
    > I assume the Container object is a member of something. What object can I
    > get to the Container object from?
    >
    > Any help would be appreciated. Ultimately, I am trying to change the CSS
    > style so the dollar amount can be red if negative, otherwise black.
    >
    > Thanks.
    >
    >
    Teemu Keiski, Sep 5, 2004
    #2
    1. Advertising

  3. <% %> evaluates when the page gets parsed
    <%# %> evaluates when the DataBind() method is called

    So you should definitely use the # sign. Container gets whatever object that
    some collection of them is passed to DataSource. So if you set some
    DataTable to DataSource, DataItem is DataRowView...

    SOmething else. You could use <%# %> this server side notation regardless of
    the databound controls as long as you call the DataBind() method somewhere
    in the execution time.

    --
    RobertK
    { Clever? No just smart. }


    "Joe Rattz" <> wrote in message
    news:...
    > I am trying to perform some logic inside a Repeater control's template. I
    > need that logic dependent on the bound data. If my bound data is < 0, I
    > want to write one string (a CSS class), otherwise I want to write a
    > different string (another CSS class). Here is what I am trying to do:
    >
    > <%
    > if(Double.Parse(DataBinder.Eval(Container.DataItem, "Amount")) < 0)
    > { Response.Write("<span class=\"NewspaperCreditItemAmountN\">"); }
    > else
    > { Response.Write("<span class=\"NewspaperCreditItemAmountP\">"); }
    > %>
    >
    > I have formatted the code a little funky so it is easier to read. As you
    > can see, I want to parse the Amount field into a double and if its < 0, I
    > want to specify one CSS class, otherwise I want to specify a different
    > class.
    >
    > Unfortunately, this code does not work...it doesn't like me accessing
    > Container this way. I can't seem to find any decent doc on this, but what
    > is the difference between a <% %> block and a <%# %> block (notice the #)?
    >
    > I assume the Container object is a member of something. What object can I
    > get to the Container object from?
    >
    > Any help would be appreciated. Ultimately, I am trying to change the CSS
    > style so the dollar amount can be red if negative, otherwise black.
    >
    > Thanks.
    >
    >
    Robert Koritnik, Sep 10, 2004
    #3
    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. Lauchlan M
    Replies:
    1
    Views:
    7,216
    Lauchlan M
    Oct 15, 2003
  2. Tom Lee
    Replies:
    2
    Views:
    825
    Chris Carter
    Dec 9, 2003
  3. bg
    Replies:
    5
    Views:
    6,352
    Scott Allen
    Apr 16, 2004
  4. Ken Cox [Microsoft MVP]
    Replies:
    1
    Views:
    3,450
    Charlie@CBFC
    May 12, 2004
  5. Eric Newton
    Replies:
    3
    Views:
    9,357
    Brock Allen
    Apr 4, 2005
Loading...

Share This Page