New2This
New2This

Reputation: 253

trying to find an alternative to foreach

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?

Upvotes: 2

Views: 246

Answers (6)

musefan
musefan

Reputation: 48415

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)

Upvotes: 7

Marco
Marco

Reputation: 23937

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;
}

Upvotes: 1

James
James

Reputation: 82096

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();
}

Upvotes: 4

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73442

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();
}

Upvotes: 5

Moo-Juice
Moo-Juice

Reputation: 38825

I think you meant to go:

for each(Control c in parent.Controls)

Upvotes: 2

Vladimir
Vladimir

Reputation: 7475

foreach (var c in parent.Controls)
{
    var tb = c as TextBox;
    if (tb != null) tb.Text = string.Empty;
}

Upvotes: 2

Related Questions