如何解决如何清除面板中的TextBox控件
| 我想清空面板中的文本框控件。 请解释。 下面是我的代码:<asp:Panel ID=\"Panel_CreateLead\" runat=\"server\" Visible=\"false\" BackColor=\"#eff2f6\" Width=\"98%\">
<asp:Literal ID=\"LiteralNoLead\" runat=\"server\" Text=\"<span style=\'color:red\'>No Lead Exists. Please fill the Lead Template to Create New Lead.</span>\"></asp:Literal>
<asp:Table ID=\"Table_CreateLead\" runat=\"server\" Width=\"98%\" CellSpacing=\"1\" CellPadding=\"1\"
Font-Names=\"Tahoma\" BorderColor=\"#eff2f6\" BorderStyle=\"Dashed\">
<asp:TableHeaderRow HorizontalAlign=\"Center\">
<asp:TableHeaderCell Text=\"Lead Template\" Font-Bold=\"true\" ColumnSpan=\"4\" Font-Size=\"Large\" />
</asp:TableHeaderRow>
<asp:TableRow ID=\"TableRow11\" runat=\"server\">
<asp:TableCell Text=\"General\" Font-Bold=\"true\" ForeColor=\"Blue\"></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID=\"TableRow1\" runat=\"server\">
<asp:TableCell Text=\"Topic\">
<asp:Literal ID=\"Literal_Topic\" Text=\"<span style=\'color:red\'>*</span>\" runat=\"server\" />
</asp:TableCell>
<asp:TableCell ColumnSpan=\"3\">
<asp:TextBox ID=\"TextBox_leadname\" Width=\"80%\" runat=\"server\"></asp:TextBox>
<asp:RequiredFieldValidator ID=\"RequiredFieldValidator_TextBox_leadname\" runat=\"server\" ControlToValidate=\"TextBox_leadname\" ValidationGroup=\"LeadVaidation\" ErrorMessage=\"Enter Topic\" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID=\"TableRow23\" runat=\"server\">
<asp:TableCell Text=\"Currency\" ></asp:TableCell>
<asp:TableCell>
<asp:DropDownList ID=\"DropDownList_Currency\" runat=\"server\">
</asp:DropDownList>
</asp:TableCell>
<asp:TableCell Text=\"No. of Employees\" ></asp:TableCell>
<asp:TableCell>
<asp:TextBox ID=\"TextBox_Employees\" runat=\"server\"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:Button ID=\"Button_lead\" runat=\"server\" Text=\"Submit\" OnClick=\"create_lead\" ValidationGroup=\"LeadVaidation\"/>
</asp:TableCell>
<asp:TableCell>
<asp:Literal ID=\"Literal_lead\" runat=\"server\"></asp:Literal>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
我已经使用了方法clearText(Panel_CreateLead);页面加载不起作用。
cleartext()中使用的代码:
private void clearText(Panel PanelID)
{
foreach (Control c in PanelID.Controls)
{
if (c is TextBox)
{
TextBox questionTextBox = c as TextBox;
if (questionTextBox != null)
{
questionTextBox.Text = \"\";
}
}
}
}
所有这些都不起作用。请帮忙。
解决方法
您需要一种递归方法来获取所有控件,否则您将仅在面板下立即获取控件。要使用递归方法获取所有控件,可以使用以下命令:
public static IEnumerable<TextBox> GetAllControls(this Control parent)
{
foreach (Control control in parent.Controls)
{
yield return control;
foreach(Control descendant in control.GetAllControls())
{
yield return descendant;
}
}
}
您可以修改以仅查找文本框,也可以将此方法用作Panel.GetAllControls.OfType<TextBox>()
并使用面板中的所有文本框。
, 用于按类型递归获取所有控件的扩展方法:
public static IEnumerable<Control> GetChildControls(this Control control) where TControl : Control
{
var children = (control.Controls != null) ? control.Controls.OfType<Control>() : Enumerable.Empty<Control>();
return children.SelectMany(c => GetChildControls(c)).Concat(children);
}
用法:
IEnumerable<TextBox> textBoxes = panel.Controls.GetChildControls().OfType<TextBox>();
foreach (TextBox tb in textBoxes)
{
tb.Text = \"\";
}
, 这只会清除直接属于您的孩子的文本框。在您的代码中,如果您执行了类似流程这样的工作,它将起作用
private void clearText(control PanelID)
{
foreach (Control c in PanelID.Controls)
{
if (c is TextBox)
{
TextBox questionTextBox = c as TextBox;
if (questionTextBox != null)
{
questionTextBox.Text = \"\";
}
}
if(c.Controls.count > 0)
{
cleartest(c)
}
}
}
, 如何清空表格中的所有文本框,然后依次是tr
,td
...
foreach (Control c in Table3.Controls)
{
if (c is System.Web.UI.HtmlControls.HtmlTableRow)
{
System.Web.UI.HtmlControls.HtmlTableRow tr;
tr = (System.Web.UI.HtmlControls.HtmlTableRow)c;
foreach (Control td in tr.Controls)
{
if (td is System.Web.UI.HtmlControls.HtmlTableCell)
{
System.Web.UI.HtmlControls.HtmlTableCell td1;
td1 = (System.Web.UI.HtmlControls.HtmlTableCell)td;
foreach (Control txtBox in td1.Controls)
{
if(txtBox is TextBox)
{
TextBox tt = txtBox as TextBox;
tt.Text = string.Empty;
}
}
}
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。