Repeater Variables

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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.
 
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>
 
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.
 
#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.
 
Back
Top