Find controls ?

K

Kevin Spencer

You may be going to more trouble than necessary. Every System.Web.UI.Control
has a Controls Collection, made up of all the System.Web.UI.Controls that
are immediately contained within that Control. Note that this is NOT all the
Controls that are contained within that Control, but only IMMEDIATELY
contained. If, for example, you have a Control that contains another Control
inside a Control, only the top-level Control belongs to the Controls
Collection of the containing Control The one uder it belongs to ITs Controls
Collection.

Let's say, for example, that you loop through the Control Collection of a
Control, such as a WebForm. You may be able to get hold of an instance of
that Control, but how do you know WHICH Control it is? Different Controls
are treated differently. You would have to use some mechanism (such as
Reflection) to determine what kind of Control it is, cast it as that Type,
find out which Control it is, and what its value is. That is a lot more
resource usage (and work for you) than just identifying the Control and
getting something out of it, as in your example:

someString = TextBoxFirstName.Text;

There ARE situations in which this sort of thing is useful. If, for example,
you have a situation in which you don't know what or how many Controls are
going to be used, using Reflection would be one of the ONLY ways of handling
it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
G

Guest

I use for example a HTML grid layout panel and change the property (right
mouse click) to "run as server control". In your code you get a
System.Web.UI.HtmlControls.HtmlGenericControl.

Then you can use the Control property.

Example:
System.Web.UI.HtmlControls.HtmlGenericControl myGrid;
for(int 1=0; i<myGrid.Controls.Count; i++) {
... //look at myGrid.Controls.
}
 
T

TM

What is the method used to find all asp.net server controls (run at server)
such as TextBox, ListBox, CheckBox... currently defined on an Asp.Net page
at run time. I need it inside of a controlled loop (for int i=...).

Reason: I need to fill up an updatable dataset with all fields entered on
the asp.net page instead of manually going 1 field at a time and hard code
the control name (i.e. TextBoxFirstName.Text=something and so on)

Thanks for your help.

Tom
 

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

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top