473,399 Members | 3,302 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,399 software developers and data experts.

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
Nov 19 '05 #1
2 1124
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" <Ca******@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
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

Nov 19 '05 #2
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" <Ca******@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
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


Nov 19 '05 #3

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

Similar topics

6
by: Phillip N Rounds | last post by:
I have a webform, from which I have to submit info to another site. Their instructions are to have a html form, with the following as the submit: <form method="post"...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
6
by: Zingam | last post by:
Is it possible to insert a WinForm in a WebForm? I'd like for example to insert a managed directx viewer in a webform, that would display 3d graphics on the form? Is this possible? Regards,...
3
by: Ipsita | last post by:
Hi, I am explaining the scenario of what I am trying to do: - I have a webform with some controls, from which I am taking the data. eg say Username, password - I am passing these data as...
7
by: Rodusa | last post by:
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) {...
3
by: mr_Grip | last post by:
Hi! I´ve just joined here and thinks this seems like a good place to be :) I´m working on a project using webforms and one thing that I would like to know is, if I copy a few controls and...
3
by: BJ | last post by:
I am trying to loop through the control collection of my ASP.NET web form in order to access the contents of each field. It seems as if there is a parent control (Literal?) in ASP.NET. Attached...
1
by: Kevin | last post by:
ASP.NET 2.0 I have code that updates a database from a number of textboxes on a web form. I've had to hard coded references to my web form textboxes. I'd like to know how I can reference them...
2
by: Luqman | last post by:
How can I open another WebForm in ASP.Net / VS 2005 without closing the Current Webform. I tried using Server.Transfer("Form2.Aspx",True) on Button_Click of Form1 but it close the current web...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.