2

im trying to write a public function which can call in all my pages to clear all my textboxes after data has been inserted. i've used it like so:

public void clean(Control parent)
{
    try
    {
        foreach (Control c in parent)
        {
            TextBox tb = c as TextBox; //if the control is a textbox
                tb.Text = String.Empty; //display nothing
            }
        }



    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }
}

however, the foreach is coming up with an error saying

"cannot operate on variable types system.web.ui.control because it does not contain a public function for getenumerator.

can anyone suggest an alternative?

New2This
  • 253
  • 1
  • 6
  • 22
  • @arkascha - my profiles says... im really new to this. suggestions/advice on helping to fix the problem welcome! – New2This Nov 21 '13 at 13:23

6 Answers6

7

Solution

You want to iterate the child controls of parent, so use this:

foreach (Control c in parent.Controls)
{
    //Do Stuff
}

Recommendation

I would also recommend checking if the Control is a TextBox too...

TextBox tb = c as TextBox;
if(tb != null)//Will be null if c is not a TextBox
{
    tb.Text = String.Empty;
}

OR

if(c is TextBox)//Check if c is TextBox before using it
{
    TextBox tb = c as TextBox;
    tb.Text = String.Empty;
}

(see comments for a good discussion and links about the differences between both approaches)

musefan
  • 47,875
  • 21
  • 135
  • 185
  • 4
    Why suggest wasteful `is` `as` route when you already have a suggested the more efficient `as` `!= null` route? – weston Nov 21 '13 at 13:26
  • 1
    `is` then `as` is expensive – Sriram Sakthivel Nov 21 '13 at 13:27
  • @weston: The recommendation is to test if the control is a TextBox, I am just offering a couple of options for doing that. The OP can decide which they prefer – musefan Nov 21 '13 at 13:28
  • Fair enough, just mentioning it so OP is aware which is better. – weston Nov 21 '13 at 13:28
  • OK, actually as mentioned [here](http://stackoverflow.com/a/496167/360211) by Skeet, I may be wrong about the performance, but there are other reasons to prefer the `as` `!= null` route. http://stackoverflow.com/a/496167/360211 – weston Nov 21 '13 at 13:33
  • @musefan - so i used the .controls. no error anymore. however it isnt clearing the textboxes. see update above – New2This Nov 21 '13 at 13:46
  • 1
    @New2This: You shouldn't edit questions like this. You should post a new question if you have a different problem. I would recommend you post a new question, and rollback this question so that the current answers remain valid – musefan Nov 21 '13 at 13:48
5

I'd use OfType extension method to filter only TextBoxes. Yours doesn't work because you can't enumerate over Control you need an IEnumerable typically(not required though).

foreach (TextBox tb in parent.Controls.OfType<TextBox>())
{
    tb.Text = String.Empty;
    //or
    tb.Clear();
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
4

That's because Control is not enumerable, however, it does have a Controls property which I assume is what you are after

foreach (Control c in parent.Controls)
{
    ...
}

You could make a useful extension method for better readability e.g.

public static class ControlExt
{
    public static IEnumerable<TextBox> TextBoxControls(this Control ctrl)
    {
        return ctrl.Controls.OfType<TextBox>();
    }
}
...
foreach (TextBox tb in parent.TextBoxControls())
{
    tb.Clear();
}
James
  • 80,725
  • 18
  • 167
  • 237
2
foreach (var c in parent.Controls)
{
    var tb = c as TextBox;
    if (tb != null) tb.Text = string.Empty;
}
Vladimir
  • 7,345
  • 4
  • 34
  • 39
2

I think you meant to go:

for each(Control c in parent.Controls)
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
1

I have posted a solution here, how you can iterate over all TextBoxes on a page. Note, that this code hsa been written by p.campbell.

public static IEnumerable<Control> FindAll(this ControlCollection collection)
{
    foreach (Control item in collection)
    {
        yield return item;

        if (item.HasControls())
        {
            foreach (var subItem in item.Controls.FindAll())
            {
                yield return subItem;
            }
        }
    }
}

After you have done this, you can iterate over all your Textbox controls on your page and ... do stuff, really. As you can see below, you can iterate over all types of controls.

foreach (var t in this.Controls.FindAll().OfType<TextBox>())
{ 
    t.Text = String.Empty;
}
Community
  • 1
  • 1
Marco
  • 22,856
  • 9
  • 75
  • 124