473,394 Members | 1,718 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,394 software developers and data experts.

How do I loop through TextBoxes and clear them?

I want to be able to loop through all the TextBoxes on a page and clear
their values. How can I write a function to do that?
Nov 19 '05 #1
6 3117

Paul D. Fox wrote:
I want to be able to loop through all the TextBoxes on a page and clear their values. How can I write a function to do that?


For Each myControl As Control In Page.Controls
If TypeOf myControl Is TextBox Then
CType(myControl, TextBox).Text = String.Empty '(or "")
End If
Next myControl

Lisa

Nov 19 '05 #2

"Paul D. Fox" <pd*****@rcn.com> wrote in message
news:eP**************@tk2msftngp13.phx.gbl...
I want to be able to loop through all the TextBoxes on a page and clear
their values. How can I write a function to do that?


C#:

public void ClearTextBoxes(WebControl Parent)
{
foreach (WebControl child in Parent.Controls) {
if (typeof(child) is TextBox) {
((TextBox) child).Text = String.Empty;
} else if (typeof(child) is IContainer) {
ClearTextBoxes(child);
}
}
}

VB.Net:

Public Sub ClearTextBoxes(ByVal Parent As WebControl)
For Each child As WebControl In Parent.Controls
If GetType(child) Is TextBox
DirectCast(child, TextBox).Text = String.Empty
ElseIf GetType(child) Is IContainer
ClearTextBoxes(child)
End If
Next
End Sub
Off the top of my head....also, I believe it's IContainer that the second if
needs to check. If not, it needs to be the interface that exposes the
"Controls" collection.

HTH,
Mythran

Nov 19 '05 #3

<li**@starways.net> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

Paul D. Fox wrote:
I want to be able to loop through all the TextBoxes on a page and

clear
their values. How can I write a function to do that?


For Each myControl As Control In Page.Controls
If TypeOf myControl Is TextBox Then
CType(myControl, TextBox).Text = String.Empty '(or "")
End If
Next myControl

Lisa


AFAIK, if there is an IContainer in the Controls collection and it has
Controls on it, the Controls property of the object implementing IContainer
will contain controls not found in the Controls collection of the Page.

Mythran

Nov 19 '05 #4
You'll need to do a recursive call, looping through the page's control
collection and the control collections of all container controls on the
page. It's not as hard as it sounds.

Here's an example:
http://SteveOrr.net/faq/ControlTreeRecursion.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Paul D. Fox" <pd*****@rcn.com> wrote in message
news:eP**************@tk2msftngp13.phx.gbl...
I want to be able to loop through all the TextBoxes on a page and clear
their values. How can I write a function to do that?

Nov 19 '05 #5
Sorry folks, but none of these methods seem to be working. This is a web
form and after running the routine, the values are stil there. I've even
set EnableViewState="False".

Paul
"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:u4**************@TK2MSFTNGP09.phx.gbl...
You'll need to do a recursive call, looping through the page's control
collection and the control collections of all container controls on the
page. It's not as hard as it sounds.

Here's an example:
http://SteveOrr.net/faq/ControlTreeRecursion.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Paul D. Fox" <pd*****@rcn.com> wrote in message
news:eP**************@tk2msftngp13.phx.gbl...
I want to be able to loop through all the TextBoxes on a page and clear
their values. How can I write a function to do that?


Nov 19 '05 #6
you actually need to use your form name as the collections object. I had a
lot of trouble with this until I found:

http://www.code101.com/Code101/Displ...le.aspx?cid=83

I did the following which works well:

Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm

....

For Each myControl As Control In Form1.Controls
If TypeOf myControl Is TextBox Then
CType(myControl, TextBox).Text = ""
End If
Next myControl

"Paul D. Fox" wrote:
I want to be able to loop through all the TextBoxes on a page and clear
their values. How can I write a function to do that?

Nov 19 '05 #7

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

Similar topics

4
by: T. Wintershoven | last post by:
Hello, I have a few textboxes placed on a SSTab control devided over 3 tabs Is there a way to clear all textboxes in one time in stead of one by one. Someone told me "Use for each item...
3
by: Belee | last post by:
I always clear textboxes by using eg this.txtName.clear(); What is the best way of doing this to about 20 boxes or asignig values to them in C#.
2
by: Jason | last post by:
I want to loop through all textbox controls to determine their .text value. If the .text value is not "" then display the textbox. I have tried the following with no luck. Any help would be...
0
by: TN Bella | last post by:
Hi, Here is part of one sub where I want to clear all textboxes (there are three subs where I do this, one contains more textboxes, the other is below and the final one looks the same but omits...
6
by: kberry | last post by:
I am clearing Textboxes on a form... this is loop I have came up with but was wondering if it can be shorter or not as long... Can anyone help? Dim controlOnForm As Control 'Places a control...
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...
1
by: Semajthewise | last post by:
Hi all!! What I am trying to do today is write to a textfile then read from the file get some lines from that file and edit them. and re-write them to the file. Not replace I want the edited lines...
2
by: englishman69 | last post by:
Hello, I have been banging my head against this one for a while... Searches online have revealed many different proposals for correcting my issue but none that I can follow! My basic situation...
7
by: Sriku | last post by:
I have created ten textboxes and name them as "txt1,txt2,..txt10" (i.e just changing suffix by consecutive number). Now I have to validate these ten textboxes on Submit event (Say..No textbox...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...
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
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...

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.