Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use foreach loop to edit textboxes

Tags:

c#

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'.

like image 231
Ramakrishna Avatar asked Apr 28 '10 07:04

Ramakrishna


5 Answers

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=" ";
    }
}
like image 66
Daniel Renshaw Avatar answered Nov 18 '22 09:11

Daniel Renshaw


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>())
like image 45
Joel Goodwin Avatar answered Nov 18 '22 09:11

Joel Goodwin


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);
    }
}
like image 34
Joop Avatar answered Nov 18 '22 09:11

Joop


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
like image 3
rtpHarry Avatar answered Nov 18 '22 07:11

rtpHarry


using LINQ:

foreach (TextBox t in this.Controls.Where(c => c is TextBox))
{
   //...
}
like image 1
pierroz Avatar answered Nov 18 '22 08:11

pierroz