Compiler disallows for loop in ASPX ... what's up?

  • Thread starter Thread starter Richard Lionheart
  • Start date Start date
R

Richard Lionheart

Hi All,

[See below for history of posting this question]

In effort to write a loop in ASPX to generate a set of labels, I tried this:

<form id="Form2" method="post" runat="server">
<script language="C#" runat="server">
int nCols = 8;
int nRows = 20;
int iHeight = 25;
int iWidth = 50;
int iVertGap = 5;
int iHorGap = 5;
for (int iRow=0; iRow<nRows; iRow++) // "for"
flagged as an error by the compiler
{
<asp:Label ID="Lbl" RunAt="server" />
[snip]

intending to assign values to the properties of the Lbl object. But the
compiler announced:

CS1519: Invalid token 'for' in class, struct, or interface member
declaration

What can I do to generate a bunch of object programmatically in ASPX?

Thanks in advance,
Richard

I posted this first in microsoft.public.dotnet.framework.
Then thought it would be more appropriate in
microsoft.public.dotnet.framework.windowsforms
Then I received advice that it would be more appropriate in the newsgroups
listed abovel.
 
You cant assign nested controls inline like in asp, you have to ctreate them
at run time add them to the forms collection or to a palceholder.

Label Label1 = New Label()
Label1.ID = "Label"

add it to the form

Form1.Controls.Add(Label1)

or add it to a placeholder

PlaceHolder1.Controls.Add(Label1);

You should create them in form load

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
unlike asp, only methods, properties and fields are allowed to be defined in
a runat=server script block with asp.net. inline code is only allowed with
the <% %> syntax (which does not allow method declarations).

-- bruce (sqlwork.com)
 
Dear Jim and Bruce,

Thanks for responding, and thanks for the facts ... though I'm bummed out.
I expected the fact that C# is embedded in ASP.NET would certainly allow for
programmers to escape repetative coding. C'est la vie.

Regards,
Richard Muller
 
Gentlemen,

I trying to create a matrix of labels on the server, cache the resulting
HTML, and let clients log in and download the generated web page.

Since I can't use "for", how about if I load the text destined for each
page into an Access 2003 or SQL Server 2000 database and executed a foreach
loop to access the text, dynamically create a Label object, assign the
relevant text and compute, then assign, the coordinates?

And I do all this in a PageLoad method?

Thanks in advance for any further consideration,
Richard Muller
 
Back
Top