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

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

Post by Richard Li » Sat, 03 Sep 2005 06:25:44


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.
 
 
 

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

Post by ASP.NET M » Sat, 03 Sep 2005 07:20:48

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

 
 
 

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

Post by Bruce Bark » Sat, 03 Sep 2005 07:34:07

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)
 
 
 

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

Post by Richard Li » Sat, 03 Sep 2005 08:59:53

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
 
 
 

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

Post by Kevin Spen » Sat, 03 Sep 2005 20:28:34

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.
 
 
 

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

Post by Richard Li » Sat, 03 Sep 2005 22:25:55

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)

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