473,545 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
+ 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.W ebControls.Text Box ) )
{
//clear text
}

}
Nov 18 '05 #1
4 1229
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.goo gle.com...
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.W ebControls.Text Box ) )
{
//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)contr ol).Text = String.Empty;
}
}
--
Scott
http://www.OdeToCode.com/

On 4 Oct 2004 07:40:00 -0700, jw*****@gmail.c om 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(Contro l c in Page.Controls)
{
if( c.GetType() == typeof( System.Web.UI.W ebControls.Text Box ) )
{
//clear text
}

}


Nov 18 '05 #3

<jw*****@gmail. com> wrote in message
news:d3******** *************** ***@posting.goo gle.com...
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.W ebControls.Text Box ) )
{
//clear text
}

}


foreach (Control c in Page.Controls) {
if (c.GetType() == typeof(System.W eb.UI.WebContro ls.TextBox)) {
((System.Web.UI .WebControls.Te xtBox) 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.U I.Control ctrl){
foreach(System. Web.UI.Control c in ctrl.Controls){
if(c is TextBox){
((TextBox)c).Te xt = string.Empty;
}
aa(c);
}
}
then you should call this procedure aa(Page.Control s[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.W ebControls.Text Box ) )
{
//clear text
}

}

Nov 18 '05 #5

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

Similar topics

0
2333
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 If TypeName(MyControl) = "TextBox" Then MyControl.Enabled = False
7
4066
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
12353
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 calcTotalPub() { var tempFed = +document.Form1.value; var tempState = +document.Form1.value;
2
1741
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 textbox was left empty, the image to the left of the box would be a warning icon. If an entry was made in the textbox, that image would be a...
1
3374
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 function call that returns a string. The textboxes all have their cangrow property set and grow correctly to fit the information. The problem lies...
4
2810
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 company, but it tells me the form is read-only, even when i set the "locked" property for all of my textboxes to "no". What do I do? Is it because the...
4
1296
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 me why it doesn't work? Thanks Nick. Code:
33
1558
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 evertyhing works as expected. The BalDue field now has a value of 66. However, if I put another button on TabB with the same code in it and click...
6
6073
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 now as i debug it, it's telling me there are only a count of 3 controls in that for loop. and they have clientid values like: ...
0
7661
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. ...
0
7815
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7433
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5976
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4949
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...
0
3458
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...
1
1891
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
1
1020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
712
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...

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.