Declare controls in code-behind

James

Well-known member
Joined
Oct 3, 2002
Messages
78
I am building a web app and learning asp.net as I go along. From what I understand I must declare every control that is on my presentation in my code-behind. My problem is I have many web controls and think its a big task to declare every control. Is there a quick and easy way that I can declare them in my code-behind? I wanted to use a for loop however, I dont know the best way to use the for loop. Please help.

James
 
I dont know ASP.NET so Im going to give an answer from a VB.NET stand point and hope its doable in ASP.NET. :)

The simplet way is to make an array for the object, then loop through the array.. something like this;

Code:
10 controls.
Dim c(9) As SomeControl
Dim i As Integer
Loop through all 10.
For i = 0 to 9
    Make a new control!
    c(i) = New SomeControl()
Next

If the controls arent the same then you can declare the array as an Object and do seperate for loops for each type of control youd like to create within the array.

You get the idea, right?
 
I dont think that you can declare them with a for/loop, especially if you are going to add event handelers to the controls.

I may be wrong though.
 
Back
Top