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

Discussion in 'ASP .Net' started by M_at, Oct 25, 2005.

  1. M_at

    M_at Guest

    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?
     
    M_at, Oct 25, 2005
    #1
    1. Advertising

  2. You may find the following article helpful:

    http://msdn.microsoft.com/msdnmag/issues/02/01/cutting/default.aspx

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    A watched clock never boils.

    "M_at" <> wrote in message
    news:...
    > 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?
    >
     
    Kevin Spencer, Oct 25, 2005
    #2
    1. Advertising

  3. M_at

    Bruce Barker Guest

    <% %> 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_at" <> wrote in message
    news:...
    > 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?
    >
     
    Bruce Barker, Oct 25, 2005
    #3
  4. M_at

    M_at Guest

    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?
     
    M_at, Oct 26, 2005
    #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. Charles A. Lackman
    Replies:
    1
    Views:
    1,402
    smith
    Dec 8, 2004
  2. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    620
    Alphonse Giambrone
    Jun 28, 2003
  3. SpamProof
    Replies:
    0
    Views:
    603
    SpamProof
    Oct 21, 2003
  4. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    509
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  5. matt
    Replies:
    1
    Views:
    284
    George Ogata
    Aug 6, 2004
Loading...

Share This Page