Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

enumerating through page.controls

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
param@community.nospam - 25 Jul 2007 18:08 GMT
Hi all,

I have an asp.net 2.0 web app that has an aspx page that has a Master page.
The Master page has the <form runat="server"> defined on it. Now the aspx
page has a UserControl that has a bunch of fields and fieldvalidators.
Inside the code behind of the ascx usercontrol I am trying to loop through
all controls to find all the validators and for some reason I am unable to
find any. Here is the code I am calling on the PreRender event handler of
the usercontrol.

private void InitFieldValidators()

{

int i;

Control c;

for (i = 0; i < this.Controls.Count; i++)

{

c = this.Controls[i];

SetReqdControl(c);

}

}

private void SetReqdControl(Control o)

{

if (o.HasControls())

{

int i;

Control c;

for (i = 0; i < o.Controls.Count; i++)

{

c = o.Controls[i];

SetReqdControl(c);

}

}

else

{

Response.Write(o.ID);

Response.Write("<br/>");

}

}
bruce barker - 25 Jul 2007 20:42 GMT
your recursion is wrong. you need to check the current control and
recurse check on all children. assuming the print is where the logic
goes, you are only handling child controls with no children.

> Hi all,
>
[quoted text clipped - 61 lines]
>
> }
Göran Andersson - 25 Jul 2007 21:12 GMT
> I have an asp.net 2.0 web app that has an aspx page that has a Master page.
> The Master page has the <form runat="server"> defined on it. Now the aspx
> page has a UserControl that has a bunch of fields and fieldvalidators.
> Inside the code behind of the ascx usercontrol I am trying to loop through
> all controls to find all the validators and for some reason I am unable to
> find any.

That's because they aren't in the Controls collection of the page. Only
a few of the controls of the page are, the other controls are in the
Controls collection of other controls.

You have to loop the Controls collections recursively to reach all the
controls of the page.

Signature

Göran Andersson
_____
http://www.guffa.com

param@community.nospam - 25 Jul 2007 22:16 GMT
I see the mistake I am making. Thanks both of you.

>> I have an asp.net 2.0 web app that has an aspx page that has a Master
>> page. The Master page has the <form runat="server"> defined on it. Now
[quoted text clipped - 9 lines]
> You have to loop the Controls collections recursively to reach all the
> controls of the page.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.