Iterateing through textboxes does not work

J

jw56578

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
}

}
 
S

Scott Allen

You'll need to use a cast operator, i.e.:

foreach(Control control in controls)
{
if(control is TextBox)
{
((TextBox)control).Text = String.Empty;
}
}
 
M

Mythran

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top