Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - Loop through webform controls - please help me

 
Thread Tools Search this Thread
Old 06-17-2005, 02:40 AM   #1
Default Loop through webform controls - please help me


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


=?Utf-8?B?Q2FsdmluIEtE?=
  Reply With Quote
Old 06-17-2005, 01:47 PM   #2
Kevin Spencer
 
Posts: n/a
Default Re: Loop through webform controls - please help me
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.

"Calvin KD" <> wrote in message
news:2DC1CD94-23FC-47AF-BF7B-...
> 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





Kevin Spencer
  Reply With Quote
Old 06-20-2005, 03:43 AM   #3
=?Utf-8?B?Q2FsdmluIEtE?=
 
Posts: n/a
Default Re: Loop through webform controls - please help me
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 ?

"Kevin Spencer" wrote:

> 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.
>
> "Calvin KD" <> wrote in message
> news:2DC1CD94-23FC-47AF-BF7B-...
> > 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

>
>
>



=?Utf-8?B?Q2FsdmluIEtE?=
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
loop according to the delay kavidream24 Software 0 12-23-2008 02:18 AM
I just want to know how to create a new webform in asp .net dynamically... nkamalraj Software 1 05-28-2006 04:28 AM
VLC controls DVDfanatico DVD Video 1 01-25-2006 05:22 PM
VLC controls DVDfanatico DVD Video 2 01-25-2006 12:07 AM
Getting Around Copy Controls Ablang DVD Video 0 04-01-2005 06:09 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46