开发者

how to use foreach loop to edit textboxes

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 ca开发者_StackOverflow中文版st object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.


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


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


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


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


using LINQ:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜