473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Can't Loop Page.Controls

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

Nov 19 '05 #1
7 1606
Rodusa wrote:
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
Nov 19 '05 #2
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.

"Rodusa" <rc**********@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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

Nov 19 '05 #3
Thanks Hans Kesting. I returns only one control ctl00, even having
three textboxes. I am using Master Page in Asp.net 2.0.

Rod

Nov 19 '05 #4
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

Nov 19 '05 #5
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.

"Rodusa" <rc**********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Nov 19 '05 #6
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.

"Rodusa" <rc**********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Nov 19 '05 #7
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

Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joe | last post by:
I'm wondering how to loop through controls in VB.NET. I have the code from VB6 ok, but I can't figure out how to do it correctly in .NET. This is an example from my VB6 code that loops through...
4
by: Ryan Liu | last post by:
How to loop though controls on the form? I set each control's TabStop and TabIndex, but at run time, I press Tab key, it jump though first control to last control and does not go back to the...
2
by: James Doran | last post by:
Hello, I'd like to iterate through each Page of my ASP.NET project from within a Custom web control and access the Page.Controls collection. I've tried using Reflection on the web project...
4
by: sck10 | last post by:
I changed my aspx page to use a master page. The problem is that I can no longer loop through the controls on the content page. My question is how do you loop through the controls on the master...
2
by: Axel Dahmen | last post by:
HI, I want to dynamically add controls to a web page from within a common base class. Unfortunately, ASP.NET fails with "System.Web.HttpException: The Controls collection cannot be modified...
11
by: Frank Esser | last post by:
Hi, I created an ASP.NET page (test.aspx) with some web controls in GridLayout (Design time). When I look at the collection page.controls by foreach (Control ctrl in Page.Controls) { ....
8
by: dominique | last post by:
Hi, Is it possible (in vb.net with WinForms) to loop throw controls inside a container (form or panel) sorting the controls on a property (.tabindex for example) ? My problem : on several...
0
by: pargat.singh | last post by:
Hi : I am using Master/Child page in VS2005 and i wrote generic function to show/hide controls which works fine if i don't use master/child .Below is my function which work fine for single page...
5
by: sck10 | last post by:
Hello, I am trying to add the following to a App_Code class. The error I am getting references "Page.Controls". I would like to call this from my content page which uses MasterPages I read...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.