473,836 Members | 1,433 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(WebCont rol 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 16364
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(WebCont rol 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(WebCont rol 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.t amu.edu> wrote:
I have another question. I'm trying to loop through all the textboxes on a
web application. The snippet is below

//foreach(WebCont rol ctr in Page.Controls)
foreach(Contro l 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***@scottcre ynolds.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.co m>
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***@scottcre ynolds.com> wrote:
not sure this is the *best* way, but it works:

if(ctr.GetTyp e().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.t amu.edu> wrote in message news:<#m******* *******@TK2MSFT NGP11.phx.gbl>. ..
I have another question. I'm trying to loop through all the textboxes on a
web application. The snippet is below

//foreach(WebCont rol 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.P age
{
protected System.Web.UI.W ebControls.Text Box TextBox1;
protected System.Web.UI.W ebControls.Text Box TextBox2;
protected System.Web.UI.W ebControls.Text Box TextBox3;
protected System.Web.UI.H tmlControls.Htm lForm frmAccountingCo des;

private void Page_Load(objec t sender, System.EventArg s e)
{
foreach (Control c in frmAccountingCo des.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="frmAccounti ngCodes" 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.P age
{
protected System.Web.UI.W ebControls.Text Box TextBox1;
protected System.Web.UI.W ebControls.Text Box TextBox2;
protected System.Web.UI.W ebControls.Text Box TextBox3;
protected System.Web.UI.H tmlControls.Htm lForm frmAccountingCo des;

private void Page_Load(objec t sender, System.EventArg s e)
{
foreach (Control c in frmAccountingCo des.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="frmAccounti ngCodes" 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
ControlCollecti on 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
2013
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 a table of controls, dynamically generated. Among these, a linkbutton (LinkBtn) to which have attached a click handler method, LinkBtn_Click(...) . Generally it works fine, but if in the LinkBtn_Click(...) code I try to access a webform control,...
1
1204
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 number of rows retured inside a dataset. All works well, but I have noticed that the control is rendered after the WebForm's load event is fired. This creates duplicate controls in certain situations. I have been able to prevent this from happening...
0
1076
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 value the users enters and another item from the in the same row in the table the repeater is bound to. This other item does not appear in a control, but it seems like I could access it in my loop using Repeater1.Something("id"). Obviously I don't...
1
1366
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 all the common startup code for the pages on our website. It is derived from System.Web.UI.Page and every page in 2 separate projects (web applications) derives from it. It contains several declarations of server controls such a navigation bar...
5
1744
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 set there back color dependant on whether they are enabled or not Cheers, Craig
5
1591
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: _________________________________________________________________________________________________________ Imports System.ComponentModel Imports System.Web.UI Imports System.Web.UI.WebControls <Description("Provides a login component"), DefaultProperty(""),
4
1792
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 If I placed this code in a module or class how would the code know which page I was refering to.
4
1885
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 reads the fields in the user's chosen PDF file, displays a webform with relevant fields (textbox or drop-menu or radio buttons etc) for user to fill out online. I'm using a 3rd party software to read and write to the PDF file, but I don't know how to...
8
1574
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 with TextBoxes, I can get the value back out of the controls just fine. One of my User Controls has some DropDownLists as well as a TextBox. I get a value from the TextBox on postback, but not the DropDownLists. Even if I see a value selected when...
0
9813
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
9665
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10834
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...
1
7782
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6976
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
5645
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...
0
5817
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4446
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
3108
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.