Connecting Tech Pros Worldwide Help | Site Map

How to create arraylist of controls?

Newbie
 
Join Date: Apr 2008
Posts: 27
#1: Oct 7 '09
Hi All,
I want to create an arraylist in asp.net which will contain multiple controls.
e.g
Arraylist.add("Textbox")
Arraylist.add("CheckBox")
Arraylist.add("Label")
Arraylist.add("DropDownList")


Is it possible?
Thanks for any help.
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 218
#2: Oct 7 '09

re: How to create arraylist of controls?


You should be able to... I don't know any asp.net but it should still be roughly the same code, right? Just make sure you instantiate a new array list and put actual objects into it, not the string name of the object.

To be honest, I'm not sure what the difference between an ArrayList and a List<> is (I should look that up later) but if the ArrayList doesn't work, try using List<Control>.

Good luck!
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,156
#3: Oct 7 '09

re: How to create arraylist of controls?


List<> is bounded to the type you supply and returns the same type.
ArrayList takes any object and always returns the object as an object, requiring you to cast it to its correct type.

A List<object> behaves roughly the same as an ArrayList.

In this situation using List<Control> might be usefull, as everything you put in there should inherit from control.
Member
 
Join Date: Aug 2009
Posts: 33
#4: Oct 8 '09

re: How to create arraylist of controls?


Nice Plater..
That was simple and help ful...

thanks
Reply