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

J

Joe Rattz

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.
 
T

Teemu Keiski

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
 
R

Robert Koritnik

<% %> 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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top