473,785 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1623
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**********@y ahoo.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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 ContentPlaceHol der. You mean I would have to look inside
ContentPlaceHol derID ?

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.ControlN ame.

--
HTH,

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

"Rodusa" <rc**********@y ahoo.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
I see. So, because I am using Master Page, Page.Controls only recognize
one ContentPlaceHol der. You mean I would have to look inside
ContentPlaceHol derID ?

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**********@y ahoo.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
I see. So, because I am using Master Page, Page.Controls only recognize
one ContentPlaceHol der. You mean I would have to look inside
ContentPlaceHol derID ?

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(c ontrol 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.Contro ls)
{
if (c is TextBox)
{
TextBox t = (TextBox)c.Find Control(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
2584
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 controls in a specific frame on a form and unselects the option buttons. .NET barks at this line of code: thiscontrol.value = False Private Sub UnSelectOpts(ByVal passedframeCaption As String) Dim thiscontrol As Control For Each thiscontrol In Me...
4
3471
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 first one unless I use Alt + Tab to go reverse direction. How can I write C# code to allow user just to use Tab kek to loop though the controls? Thanks!
2
1958
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 assembly, but the controls collection count was zero. While this method gives details of the private controls in each page, I need access to actual instances of the controls to interrogate their individual properties.
4
6375
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 page and how do you loop through the controls on the content page when the content is inside the ContentHolder control? Below is an example of the control loop before using a master page. Sub FindControlsInWebForm() Dim strValue As String = ""
2
7364
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 because the control contains code blocks (i.e. <% ... %>)." All of my pages contain these kind of blocks, for several reasons. For one, I don't know any other way to dynamically add header information like e.g. style sheet links to a web page. So, is...
11
1607
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
2528
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 forms (or panels), i have many controls (inherited controls from base controls : MyTextbox, MyCombobox ..). I add a new property on these controls : .BeginningGroup (boolean : yes or no).
0
1272
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 to show/hide controls. private void displayOrHideControls(string ctrlNames, string Mode) { string arControlNames = ctrlNames.Split(new char { ';' });
5
1819
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 the following from Steven Cheng, but am having a hard time following: In ASP.NET 2.0, the pages and usercontrols and their codebehind classes are dynamically compiled at runtime. However, the dynamic compilation is possible to compile the page or...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.