473,473 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Iterateing through textboxes does not work

I want to iterate through the textboxes on a page and set their text
to an empty string. I do this on pre render. but if i use the
following code i cannot clear the text because a Control object has no
text property. What should i do.

foreach(Control c in Page.Controls)
{
if( c.GetType() == typeof( System.Web.UI.WebControls.TextBox ) )
{
//clear text
}

}
Nov 18 '05 #1
4 1225
So you have to then cast it to a textbox so you can set the text.

<jw*****@gmail.com> wrote in message
news:d3**************************@posting.google.c om...
I want to iterate through the textboxes on a page and set their text
to an empty string. I do this on pre render. but if i use the
following code i cannot clear the text because a Control object has no
text property. What should i do.

foreach(Control c in Page.Controls)
{
if( c.GetType() == typeof( System.Web.UI.WebControls.TextBox ) )
{
//clear text
}

}

Nov 18 '05 #2
You'll need to use a cast operator, i.e.:

foreach(Control control in controls)
{
if(control is TextBox)
{
((TextBox)control).Text = String.Empty;
}
}
--
Scott
http://www.OdeToCode.com/

On 4 Oct 2004 07:40:00 -0700, jw*****@gmail.com wr
ote:
I want to iterate through the textboxes on a page and set their text
to an empty string. I do this on pre render. but if i use the
following code i cannot clear the text because a Control object has no
text property. What should i do.

foreach(Control c in Page.Controls)
{
if( c.GetType() == typeof( System.Web.UI.WebControls.TextBox ) )
{
//clear text
}

}


Nov 18 '05 #3

<jw*****@gmail.com> wrote in message
news:d3**************************@posting.google.c om...
I want to iterate through the textboxes on a page and set their text
to an empty string. I do this on pre render. but if i use the
following code i cannot clear the text because a Control object has no
text property. What should i do.

foreach(Control c in Page.Controls)
{
if( c.GetType() == typeof( System.Web.UI.WebControls.TextBox ) )
{
//clear text
}

}


foreach (Control c in Page.Controls) {
if (c.GetType() == typeof(System.Web.UI.WebControls.TextBox)) {
((System.Web.UI.WebControls.TextBox) c).Clear();
}
}

Casting :)

Mythran
Nov 18 '05 #4
You should try to make a recursive procedure to iterate throu all of your
controls something like this:

private void aa(System.Web.UI.Control ctrl){
foreach(System.Web.UI.Control c in ctrl.Controls){
if(c is TextBox){
((TextBox)c).Text = string.Empty;
}
aa(c);
}
}
then you should call this procedure aa(Page.Controls[0]); when you need to
do this;

Page.Controls give you just the form.

"jw*****@gmail.com" wrote:
I want to iterate through the textboxes on a page and set their text
to an empty string. I do this on pre render. but if i use the
following code i cannot clear the text because a Control object has no
text property. What should i do.

foreach(Control c in Page.Controls)
{
if( c.GetType() == typeof( System.Web.UI.WebControls.TextBox ) )
{
//clear text
}

}

Nov 18 '05 #5

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

Similar topics

0
by: Geert-Pieter Hof | last post by:
Hi, If have a SSTab control MySSTab with several textboxes and I want to disable all textboxes. If was thinking of something like Dim MyControl As Control For Each MyControl In MySSTab ...
7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
15
by: crjunk | last post by:
I have 4 TextBoxes on my form that I'm trying to add together to get a grand total. Here is the code I'm using: <SCRIPT LANGUAGE="JavaScript"> <!-- Beginning of JavaScript - function...
2
by: SC | last post by:
I have two identical textboxes and two identical images that go are next to them. Only the names are different. I have onBlur trigger a function to make sure the boxes were not left empty. If a...
1
by: Mark | last post by:
Hello, I have a report in MS Access 2002 that has a list of textboxes in a row down the page. All these textboxes get variable length data from the Control Source Table of the report or from a...
4
by: Andy Weinmann | last post by:
I need to make a form through which I can edit the information for companies. I used a form in which the textboxes read from different columns from a listbox to display the information for a...
4
by: Nick | last post by:
Hi - I'm trying to loop through the textboxes on a page to unlock them for editing. The Enabled property is set to false, and the following code is on an 'Edit' button on the form. Can anyone tell...
33
by: Woody Splawn | last post by:
I have a winform with a tab on it. On tabA I have a field called nbrBalDue. In the Click event of a button on tabA I have the following code Me.nbrBalDue.Value = 66 When I click the button...
6
by: jobs | last post by:
This code was working, but then stopped working. I don't think I completely understand it. I pass it a formview name and it would loop through checking the value of textboxes. problem is...
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
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,...
1
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
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.