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

  • Thread starter Richard Lionheart
  • 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.
 
J

John Timney \(ASP.NET MVP\)

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
 
B

Bruce Barker

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)
 
R

Richard Lionheart

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
 
R

Richard Lionheart

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
 
J

John Timney \(ASP.NET MVP\)

Have you read about datagrid or datalist controls. Once you have your data
you can bind this to a list/grid type control and the bind will
automatically create a table for you. Its immensly flexible.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Richard Lionheart said:
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


Richard Lionheart said:
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.
 
K

Kevin Spencer

You're not thinking fourth-dimensionally, Richard! ;-)

Actually, you're not thinking object-oriented. In fact, OOP not only allows
for programmers to escape repetitive coding, but allows for much leaner
coding, and easier code maintenance and extensibility. OOP is hard to get
your head around at first, but once you "get it," it all falls neatly into
place, and is actually much easier to work with than procedural programming.
The difficulty in understanding OOP is due to the fact that OOP requires a
much more abstract way of looking at programming. I would strongly recommend
making the effort to get your head around how OOP works. You'll be glad you
did!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
R

Richard Lionheart

Hi John,
Have you read about datagrid or datalist controls.

No, I haven't.

I've just started playing with User Controls as a means of encapsulating
some of the code I need.

I just decided/realized that table is a better approach than a matrix of
labels.

I did find the foreach command, so I was heading in the direction of using
an Access DB and sucking in the content for my matrix.

I'll check out datagrid and datalist controls today.

Thanks for the guidance. I needed some high level direction.

Best wishes,
Richard
 
R

Richard Lionheart

Hi Kevin,

Thanks for weighing in on my question.

I agree about the value of OOP and have had some experience in C++ business
applications. I'm just trying to get off the ground in this new (for me)
environment. As I said in in
response to John Timney's post this morning, I've had a few more thoughts
and will follow his suggestions.

I'm sure with the help of you guys I'll get launched in ASP.NET programming.

Best wishes,
Richard
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top