How to use foreach loop to edit text fields

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

I want to delete all text fields on my page in one go.

I get an error message:

Unable to pass an object of type 'System.Web.UI.LiteralControl' to input of type 'System.Web.UI.WebControls.TextBox'.

+5
source share
5 answers

The error is that the collection of controls contains more than text fields. Try this instead.

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

    if (t != null)
    {
        t.text=" ";
    }
}
+13
source

You are viewing controls, and not all controls are necessarily text fields, so the foreach loop cannot be compiled.

foreach (Control c in this.Controls), , .

.NET:

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

, , , . - . , reset , , .

/// <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);
    }
}
+4

, , , , (reset ), :

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

reset . :

  • , (), TextBox
  • Only reset text fields were requested in the original question
  • So the user does not indicate whether linq is allowed
  • That a control can have child controls.

You can use it like this:

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

Other options

Other options for resetting text field controls:

  • Run a Response.Redirect("~/ThisPageUrl.aspx");to reload the current page
  • Disable viewstate on page or individual control so that its state is lost after postback
+3
source

using LINQ:

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

All Articles