C# 如何使用foreach循环编辑文本框
我想一次清除页面中的所有文本框 我得到一个错误: 无法将“System.Web.UI.LiteralControl”类型的对象强制转换为 “System.Web.UI.WebControls.TextBox”C# 如何使用foreach循环编辑文本框,c#,C#,我想一次清除页面中的所有文本框 我得到一个错误: 无法将“System.Web.UI.LiteralControl”类型的对象强制转换为 “System.Web.UI.WebControls.TextBox” 该错误是因为控件集合不仅包含文本框。试试这个 foreach(textbox t in this.controls) { t.text=" "; } 您正在遍历这些控件,并且并非所有控件都必须是文本框,因此无法编译foreach循环 简单的方法是对每个控件执行foreach(此.Cont
该错误是因为控件集合不仅包含文本框。试试这个
foreach(textbox t in this.controls)
{
t.text=" ";
}
您正在遍历这些控件,并且并非所有控件都必须是文本框,因此无法编译foreach循环 简单的方法是对每个控件执行
foreach(此.Controls中的控件c)
,然后检查该控件是否为文本框
您也可以在.NET的更高版本中尝试此操作:
foreach (Control c in this.Controls)
{
TextBox t = c as TextBox;
if (t != null)
{
t.text=" ";
}
}
foreach(this.Controls.OfType()中的文本框t)
基于此页面上已经发布的代码,我想说实现您要求的最后一段代码(重置页面上的所有文本控件)如下所示:
foreach(TextBox t in this.Controls.OfType<TextBox>())
此方法可用于任何控件或控件组,以重置所有子文本框。它考虑到:
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);
}
}
resettextboxs(此);//重置页面上的所有文本框
重置文本框(somePanel);//在单个文本框内重置所有文本框
其他选择
重置文本框控件的其他选项包括:
- 发出一个
以重新加载当前页面Response.Redirect(“~/ThisPageUrl.aspx”)
- 在页面或单个控件上禁用viewstate,以便回发后其状态丢失
ResetTextBoxes(this); // reset all TextBox's on a page
ResetTextBoxes(somePanel); // reset all TextBox's within a single <asp:Panel>
您的页面可能有也有子控件的子控件。因此,最好的方法是使用递归。我已经编写了一个函数,可以重置我定义为父控件的某个页面、面板或其他控件的所有控件
foreach (TextBox t in this.Controls.Where(c => c is TextBox))
{
//...
}
//
///将父控件中的所有控件重置为其默认值。
///
///要重置的控件的父级
受保护的void ResetChildControls(控件父级)
{
如果(parent.Controls.Count==0)
返回;
foreach(父控件中的控件子控件)
{
如果(子对象是文本框)
{
((文本框)child.Text=”“;
}
else if(子项为HiddenField)
{
((HiddenField)child.Value=“”;
}
else if(子项为下拉列表)
{
DropDownList dropdown=(DropDownList)子级;
如果(dropdown.Items.Count>0)
{
dropdown.SelectedIndex=0;
}
}
else if(孩子是单选按钮)
{
((RadioButton)子项)。选中=false;
}
else if(子项为RadioButtonList)
{
RadioButtonList rbl=(RadioButtonList)子项;
rbl.SelectedIndex=rbl.Items.Count>0?0:-1;
}
else if(子项为复选框)
{
((复选框)child).Checked=false;
}
else if(子项为复选框列表)
{
CheckBoxList cbl=(CheckBoxList)子项;
cbl.ClearSelection();
}
else if(子项为DataGrid)
{
((DataGrid)子级);
}
重置儿童控制(儿童);
}
}
我会把它写成foreach(textboxs中的TextBox.Where(c=>c是TextBox)){box.text=“”}
@Isaac:如果你有LINQ,这是有效的,但如果没有。。。Daniels方法也适用于.NET2.0Oliver,他说不能在.NET2.0中使用LINQto对象?这一定是清除所有内容的最快方法,但不幸的是,您丢失了所有内容,甚至是要保留的viewstate数据。在for循环中,为什么不直接执行resetTextBox(child)
,而不是检查它是否是一个文本框,这样可以最小化代码,效果也一样好。如果控件是文本框,您可能需要返回或else
,否则您将通过文本框的“控件”。
/// <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);
}
}