如何使用foreach循环编辑文本框

时间:2010-04-28 07:05:25

标签: c#

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

我想一次清除页面中的所有文本框。

我收到错误:

  

无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型   'System.Web.UI.WebControls.TextBox'。

5 个答案:

答案 0 :(得分:13)

错误是因为您的控件集合不仅包含文本框。试试这个。

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

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

答案 1 :(得分:6)

您正在浏览控件,并非所有控件都必须是文本框,因此无法编译foreach循环。

直接的方法是执行foreach (Control c in this.Controls),然后检查控件是否是文本框。

您也可以在.NET的更高版本中尝试此操作:

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

答案 2 :(得分:4)

您的网页可能还有包含子控件的子控件。因此,最好的方法是使用递归。我编写了一个函数来重置我定义为父级的某个页面,面板或其他控件的所有控件。

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

答案 3 :(得分:3)

根据已经在此页面上发布的代码,我会说最后一段代码可以实现您的要求(重置页面上的所有文本控件),如下所示:

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

此方法可用于任何控件或控件组,以重置所有子文本框。它考虑到了:

  1. 通过(父)的控件可以通过TextBox
  2. 原始问题中仅请求重置文本框
  3. 用户未指定是否允许使用linq
  4. 控件可能有子控件。
  5. 您可以这样使用:

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

    其他选项

    重置文本框控件的其他选项包括:

    • 发出Response.Redirect("~/ThisPageUrl.aspx");以重新加载当前页面
    • 在页面或单个控件上禁用viewstate,以便在回发后丢失其状态

答案 4 :(得分:1)

使用LINQ:

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