5
foreach(textbox t in this.controls)
{
t.text=" ";
}

I want to clear all the textboxes in my page at one time.

I am getting an error:

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.

5 Answers 5

13

The error is because your controls collection doesn't only contain text boxes. Try this instead.

foreach (Control c in this.Controls)
{
    TextBox t = c as TextBox;

    if (t != null)
    {
        t.text=" ";
    }
}
3
  • I'd have written this as foreach(TextBox box in textBoxes.Where(c => c is TextBox)){box.text = " "}
    – user24359
    Apr 28, 2010 at 7:18
  • 2
    @Isaac: if you have LINQ this works, but if not... Daniels approach also works in .Net 2.0
    – Oliver
    Apr 28, 2010 at 7:27
  • Oliver, who said you can't use LINQ to Objects in .NET 2.0? code.google.com/p/linqbridge Apr 28, 2010 at 9:21
6

You are going through the controls, and not all controls are necessarily textboxes, so the foreach loop cannot be compiled.

The straightforward approach is to do a foreach (Control c in this.Controls) and then check whether the control is a textbox.

You can also try this in later versions of .NET:

foreach(TextBox t in this.Controls.OfType<TextBox>())
4

Your page probably has child controls that has child controls too. Therefor the best way to do this is with recursion. I have written a function that reset all my controls for a certain page, panel or other control I define as the parent.

/// <summary>
/// Resets all the controls in a parent control to their default values.
/// </summary>
/// <param name="parent">Parent of controls to reset</param>
protected void ResetChildControls(Control parent)
{
    if (parent.Controls.Count == 0)
        return;

    foreach (Control child in parent.Controls)
    {

        if (child is TextBox)
        {
            ((TextBox)child).Text = "";
        }
        else if (child is HiddenField)
        {
            ((HiddenField)child).Value = "";
        }
        else if (child is DropDownList)
        {
            DropDownList dropdown = (DropDownList)child;

            if (dropdown.Items.Count > 0)
            {
                dropdown.SelectedIndex = 0;
            }
        }
        else if (child is RadioButton)
        {
            ((RadioButton)child).Checked = false;
        }
        else if (child is RadioButtonList)
        {
            RadioButtonList rbl = (RadioButtonList)child;
            rbl.SelectedIndex = rbl.Items.Count > 0 ? 0 : -1;
        }
        else if (child is CheckBox)
        {
            ((CheckBox)child).Checked = false;
        }
        else if (child is CheckBoxList)
        {
            CheckBoxList cbl = (CheckBoxList)child;
            cbl.ClearSelection();
        }
        else if (child is DataGrid)
        {
            ((DataGrid)child).SelectedIndex = -1;
        }

        ResetChildControls(child);
    }
}
0
3

Based on the code that has already been posted on this page I would say the final piece of code to achieve what you asked (reset all text controls on a page) would look like:

protected void ResetTextBoxes(Control parent)
{
    if(parent is TextBox)
    {
        ((TextBox)parent).Text = string.Empty;
    }

    foreach(Control child in parent.Controls)
    {
        if (child is TextBox)
        {
            ((TextBox)child).Text = string.Empty;
        }

        ResetTextBoxes(child);
    }
}

This method can be used on any control or group of controls to reset all child textboxes. It takes into account:

  1. The control passed (parent) may by a TextBox
  2. That only textboxes being reset was requested in the original question
  3. That the user hasn't specified if linq is allowed
  4. That the control may have child controls.

You could use this like so:

ResetTextBoxes(this); // reset all TextBox's on a page
ResetTextBoxes(somePanel);  // reset all TextBox's within a single <asp:Panel>

Other Options

Other options to reset textbox controls are:

  • Issue a Response.Redirect("~/ThisPageUrl.aspx"); to reload the current page
  • Disable viewstate on either the page or individual controls so that its state is lost after postback
2
  • This must be the fastest way to clear everything, but unfortunately you loose everything, even the viewstate data you want to keep.
    – Patrick
    Apr 28, 2010 at 7:29
  • In your for loop, why not just do ResetTextBoxes(child) instead of checking if it is a TextBox, it minimizes code and works just as good. And you probably want a return or else if the control is a TextBox, otherwise you are going through a textbox's "controls".
    – Patrick
    Apr 29, 2010 at 21:38
1

using LINQ:

foreach (TextBox t in this.Controls.Where(c => c is TextBox))
{
   //...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.