Can't Loop Page.Controls

R

Rodusa

I am trying to loop all controls in a form with no success. For
example, Let's say we place three TextBoxes in the form and we use the
code below:
foreach (Control c in Page.Controls)
{
Response.Write(c.ID);
}

It is returning nothing. I check the <form runat="server"> and it is in
there.

What is wrong?

Rod
 
H

Hans Kesting

Rodusa said:
I am trying to loop all controls in a form with no success. For
example, Let's say we place three TextBoxes in the form and we use the
code below:
foreach (Control c in Page.Controls)
{
Response.Write(c.ID);
}

It is returning nothing. I check the <form runat="server"> and it is
in there.

What is wrong?

Rod

You might need to use recursion to find controls at a "deeper level".
How many items are in this "Page.Controls"? What's the Type?

Hans Kesting
 
K

Kevin Spencer

First, the Page does not contain very many Control in its Controls
Collection. Usually it contains a few Literal Controls and a WebForm
Control. The WebForm Control contains most of the Controls in the Page.
However, some of the Control are probably nested as well. For example, if
you use any UserControls, they almost always contain other Controls.

Now, the Controls Collection of any Control is limited to the Controls that
are immediately under that Control, not those that are nested inside them.
So, you would need to use Recursion to see all of the Controls in the Page.
This is generally done via a recursive function (a function that calls
itself) which takes a Control as a parameter, loops through the Controls in
the Control's Controls Collection, calling itself for each Control.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.
 
R

Rodusa

Thanks Hans Kesting. I returns only one control ctl00, even having
three textboxes. I am using Master Page in Asp.net 2.0.

Rod
 
R

Rodusa

I see. So, because I am using Master Page, Page.Controls only recognize
one ContentPlaceHolder. You mean I would have to look inside
ContentPlaceHolderID ?

Rod
 
K

Kevin Spencer

Hi Rod,

Well, now, you didn't mention that you're working with the .Net Platform
2.0, or that you're using MasterPages. While that makes things a bit more
complicated, the nice part is that at run-time, everything is merged into a
single Page class. So, you can reference any Controls in your Content Page
the same way you would in ASP.Net 1.1. Any Controls in the MasterPage must
be referenced as Master.ControlName.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.
 
K

Kevin Spencer

Well, I should clarify myself. When you're writing code in the MasterPage's
CodeBehind, you don't need to reference Master. But in the Control Page, you
do.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.
 
R

Rodusa

Thanks Kevin, that gave me a clue to build this method. However I would
like to extend it to be used with different controls, for example, a
CheckBox control, and call it ResetControls(control oControl, object
ControlToReset), but have the ability to specify which controls to
reset. The problem is that I don't know how to pass the object
ControlToReset in its native type so that I could use it in the "if (c
is CheckBox)" piece of code and then reset the CheckBox. I don't want
to pass the control type as a string like "CheckBox", for instance,
because it would add redundancy to the code since I know which object I
want to reset.

/* Function to Clear TextBoxes. */
static public void ResetTextBoxes(Control oControl)
{
foreach (Control c in oControl.Controls)
{
if (c is TextBox)
{
TextBox t = (TextBox)c.FindControl(c.ID);
t.Text = "";
}
if (c.HasControls())
{
ResetTextBoxes(c, TxtStrPart);
}
}
}

To call it, in button click event , I would use :

ResetTextBoxes(Master)

Rod
 

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
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top