Repeater Variables

G

Guest

<%
int currentYear = 0;
%>
<asp:Repeater ID="RepeaterEvents" runat="server"
DataSourceID="SqlDataSourceEvents">
<ItemTemplate>
<% Response.Write(currentYear); %>
</ItemTemplate>
</asp:Repeater>

I have the above code, for each record in the source i want to dump out a
variable. However i get an error that currentYear is not defined. How do i
need to structure this to get it to work?

Thanks
 
M

Mark Fitzpatrick

It looks like you're trying to marry some of the old ASP concepts into
ASP.Net. The cleanest way to do this would be to make use of the repeater
controls event model. For example:
Instead of trying to dump a value using response.write, create an asp label
control in the item template like so:

<ItemTemplate>
<asp:label id="curYear" runat="server" />
</ItemTemplate>

next, you'll want to create a function to handle the itemdatabound event for
the repeater control. This event gets called for every data item that is
being bound to the control. Then you can access the label control using the
findcontrol method like so:

Label curYear = (Label)e.Item.FindControl("curYear");
if(curYear != null)
{
curYear.Text = "0";
}

this would set the current year to zero in each case.
 
G

Guest

Howdy,

I'd recommend some reading about asp.net basics. You have to switch from
classic asp thinking to OO. Now, to make it work, change it to (forget about
response.write:) :

<script runat="server">
protected int currentYear = 0;
</script>

<asp:Repeater ID="RepeaterEvents" runat="server"
DataSourceID="SqlDataSourceEvents">
<ItemTemplate>
<%# currentYear.ToString() %>
</ItemTemplate>
</asp:Repeater>
 
S

Steven Cheng[MSFT]

Hi Nick,

As Mark and Milosz suggested, you need to declare the variable in aspx
inline server-code block (<script runat="server" >....</script>) or in the
codebehind page class so that the databinding express scope(in <%# %> )
can correctly reference that variable.

Also, for <% %> code block they're in different scope from <%# %>
databinding expression. For <%# %> databinding expression, it can
reference those variables that are defined in databinding container object
or in super level object(such as the page instance).

BTW, <%= %> block can not be used to initialize ASP.NET server control
properties, however, in ASP.NET 2.0 , you can build custom expression
builder to do this:

#From the Suggestion Box: Why can't you use code expressions for properties?
http://weblogs.asp.net/leftslipper/archive/2007/01/16/Using-code-expressions
-in-properties.aspx

If you have anything else unclear, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

sloan

#1 Asp.Net concept.

Do NOT loop and use Response.Write statements.

You create GridView (2.0) , DataGrid(1.1) , DataList or Repeater objects,
and DataBind() to them.
 

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

Similar Threads


Members online

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top