我想清空Panel中的文本框控件。 请解释一下。
下面是我的代码:
<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 = "";
}
}
}
}
所有这些都不起作用。请帮忙。
答案 0 :(得分:1)
这样只会清除直接与你相关的文本框。在你的代码中,如果你做了类似流动的工作
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)
}
}
}
答案 1 :(得分:1)
您需要一个递归方法来获取所有控件,否则您将只获得面板下的控件。要get all controls with a recursive method,你可以使用它:
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>()
并使用面板内的所有文本框。
答案 2 :(得分:1)
以递归方式按类型获取所有控件的扩展方法:
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 = "";
}
答案 3 :(得分:-1)
如何清空表格中的所有文本框,然后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;
}
}
}
}
}
}
}