Loop through webform controls - please help me

G

Guest

Hi everyone,
I'd like to interrogate a web form to check for values of the controls on
the form. The following codes worked when I execute it in a User Control that
has some web controls on it but I can't seem to get it to work directly on
the form itself.

using System.Web;
using System.Web.UI;
using System.Web.UI,WebControls;

foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox)
do something
else if (ctrl is DropDownList)
do something else
}

When the above code is done at the Page_Load() of the aspx file, the
this.Controls return System.Web.UI.LiteralControl, System.Web.UI.HTMLControls
etc in the Controls collection but not the Textboxes and other Web Controls.
Is this correct or have I done something wrong?
It'd be greatly appreciated if someone out there can enlighten me please.
KD
 
K

Kevin Spencer

Hi Calvin,

In "this" case, "this" is a reference to the Page itself. The Form is a
Control IN the Page. All Controls inside the Form are inside the Form's
Controls Collection, not the Page's.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
G

Guest

Thanks so much Kevin. Is this by design or is it a bug by MS? What's really
frustrating is that the Watch Windows does not have the correct Controls
collection so I could not find out that there exists a "second level" and
this is certainly not the case for Windows forms and may be VB.Net ?
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top