473,325 Members | 2,872 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,325 software developers and data experts.

Looping through all controls on a webform

I have another question. I'm trying to loop through all the textboxes on a
web application. The snippet is below

//foreach(WebControl ctr in Page.Controls)
foreach(Control ctr in Page.Controls)
{
if(ctr is TextBox)
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}

Would you please tell me why it does not work?
Nov 16 '05 #1
7 16320
not sure this is the *best* way, but it works:

if(ctr.GetType().ToString() == "System.Windows.Forms.TextBox")

--------------------------------
| Scott C. Reynolds |
| Tales from the SharpSide |
| http://www.scottcreynolds.com |
--------------------------------

Hai Nguyen wrote:
I have another question. I'm trying to loop through all the textboxes on a
web application. The snippet is below

//foreach(WebControl ctr in Page.Controls)
foreach(Control ctr in Page.Controls)
{
if(ctr is TextBox)
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}

Would you please tell me why it does not work?

Nov 16 '05 #2

I assume by not working, you mean not all TextBoxes on the form are changed. basically, you are not checking grand-children and grand-grand-children and so forth. you need to recursively loop through the entire control hierarchy. frankly, it's too messy for my liking

----- Hai Nguyen wrote: ----

I have another question. I'm trying to loop through all the textboxes on
web application. The snippet is belo

//foreach(WebControl ctr in Page.Controls
foreach(Control ctr in Page.Controls

if(ctr is TextBox

TextBox t = (TextBox)ctr
t.BackColor = Color.AliceBlue
t.ReadOnly = false

Would you please tell me why it does not work

Nov 16 '05 #3
http://odetocode.com/Code/71.aspx

HTH,

--
Scott
http://www.OdeToCode.com

On Tue, 8 Jun 2004 12:33:40 -0500, "Hai Nguyen"
<ha******@neo.tamu.edu> wrote:
I have another question. I'm trying to loop through all the textboxes on a
web application. The snippet is below

//foreach(WebControl ctr in Page.Controls)
foreach(Control ctr in Page.Controls)
{
if(ctr is TextBox)
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}

Would you please tell me why it does not work?


Nov 16 '05 #4
Scott C. Reynolds <sc***@scottcreynolds.com> wrote:
not sure this is the *best* way, but it works:

if(ctr.GetType().ToString() == "System.Windows.Forms.TextBox")


That won't work any better than the code given (worse, as it's a webapp
- the controls *certainly* won't be System.Windows.Forms.TextBoxes) and
it'll be much slower too. It also won't pick up subclasses of TextBox.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
And here it says right in the subject: "webform".

Just ignore me. I'm not that bright!

--------------------------------
| Scott C. Reynolds |
| Tales from the SharpSide |
| http://www.scottcreynolds.com |
--------------------------------
Jon Skeet [C# MVP] wrote:
Scott C. Reynolds <sc***@scottcreynolds.com> wrote:
not sure this is the *best* way, but it works:

if(ctr.GetType().ToString() == "System.Windows.Forms.TextBox")

That won't work any better than the code given (worse, as it's a webapp
- the controls *certainly* won't be System.Windows.Forms.TextBoxes) and
it'll be much slower too. It also won't pick up subclasses of TextBox.

Nov 16 '05 #6
TC
"Hai Nguyen" <ha******@neo.tamu.edu> wrote in message news:<#m**************@TK2MSFTNGP11.phx.gbl>...
I have another question. I'm trying to loop through all the textboxes on a
web application. The snippet is below

//foreach(WebControl ctr in Page.Controls)
foreach(Control ctr in Page.Controls)
{
if(ctr is TextBox)
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}

Would you please tell me why it does not work?


Because you need to reference the controls of the HTML Form. Here is
an example:

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.HtmlControls.HtmlForm frmAccountingCodes;

private void Page_Load(object sender, System.EventArgs e)
{
foreach (Control c in frmAccountingCodes.Controls)
{
if (c.GetType() == typeof(TextBox))
{
TextBox t = (TextBox)c;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
}
}

And of course you must name your <form> tag the name used in the
codebehind. For this code you would have to use:

<form id="frmAccountingCodes" method="post" runat="server">

Hope this helps!
Nov 16 '05 #7
TC wrote:

Because you need to reference the controls of the HTML Form. Here is
an example:

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.HtmlControls.HtmlForm frmAccountingCodes;

private void Page_Load(object sender, System.EventArgs e)
{
foreach (Control c in frmAccountingCodes.Controls)
{
if (c.GetType() == typeof(TextBox))
{
TextBox t = (TextBox)c;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
}
}

And of course you must name your <form> tag the name used in the
codebehind. For this code you would have to use:

<form id="frmAccountingCodes" method="post" runat="server">

Hope this helps!

I forgot to clarify that it is not always the <form> control that you
will reference. As Daniel stated earlier, there is a control hierarcy
on every page (Parent -> Child -> Grandchild etc.). So you would want
to reference the parent container. For example, if your TextBoxes
were inside of an <asp:panel> control, then you would loop through the
ControlCollection of the panel.

Again, hope this helps!

-Todd
Nov 16 '05 #8

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

Similar topics

1
by: hybrid | last post by:
I have problems in understanding the behavior of the events triggered by dynamically created controls over a webform. Could you help me? In a webform, I have a static PlaceHolder PH containing...
1
by: Charles A. Lackman | last post by:
Hello, I have created a User Control within Visual Studio and it contains a button that allows the user to querry a database. I dynamically add additional controls to the page based on the...
0
by: MattB | last post by:
I've got a WebForm that loops through a repeater's controls to get values entered into text boxes by a user. I'm getting to the values OK, but I want to populate a 2 dimensional array with the...
1
by: Chris | last post by:
This may be more of a Visual Studio question but those groups seem to be full of unrelated stuff so hopefully this might be the right place. I have a class (no associated aspx file) which handles...
5
by: Craig G | last post by:
how do i go about this thru serverside code (VB.NET)? any links to any articles anywhere? basically i just want something simple that will loop thru all txt & cbo server side controls, and then...
5
by: Alex Nitulescu | last post by:
Hi. Because I'm a beginner in creating controls, I spent more than two *&^#$ hours to create this "login" as a custom control and to make it work properly: ...
4
by: Poppy | last post by:
How can I loop through controls on a form and find out what type they are. I want to loop through controls on a webform and if they are visible textboxes change there value if NULL to "na". Also...
4
by: WB | last post by:
Hi, How can I generate web controls such as textboxes and drop-menus on the fly? My web application allows users to fill out PDF forms online. There are many PDF forms, and my application...
8
by: MattB | last post by:
I have a asp.net 1.1/vb application that has a page with a bunch of dynamically added User Controls. When I add the controls, I set the UserControl.EnableViewState to true. For all my controls...
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...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.