Accessing public variables from code behind in in-line blocks.

M

M_at

I'm using a repeater with some conditional code - one of the things I
need to do is display the first X items slightly differently. To enable
this I have a public integer "county" and I use it as follows:

<asp:repeater id="Repeater1" runat="server">
<ITEMTEMPLATE>
<%county++; %>
<%# ImageOutput(Container, county ) %>
<%# DataBinder.Eval(Container.DataItem, "AreaName") %><br>
</ITEMTEMPLATE>
</asp:repeater>

As you can see - I increment "county" in the repeater. This is
available in the code behind but when I pass "county" over to my
ImageOutput routine it doesn't get passed properly.

I thin that it's something to do with the <%# ... %> style blocks but
to tell the truth my training nexver explained these 100% so I've just
been using them a little blindly.

Can anyone explain exactly why a variable would not be available within
them?
 
B

Bruce Barker

<% %> is inline code, <%# %> is a binding expression. inline is run prttey
much where you expect, in this case inside the repeater loop. a binding
expression is a string expression that is used to calc the actual value. it
must be assigned to a property that supports binding and is evaluated when
DataBind is called.

so in your case the counter has reached its max value before the binding
expression is evaluated, so they all get the same value.

-- bruce (sqlwork.com)
 
M

M_at

Actually the counter is zero at the time of binding but your
explanation makes sense :)

Is there any way to replace the binding expressions with inline code in
a repeater to do the same thing?
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top