How to Clear the TextBox Controls in a Panel
I want to empty textbox controls in a Panel. Please explain.
below is my code:
<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>
I have used a method clearText(Panel_CreateLead); on page load which does not work.
the code used in 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 = "";
}
}
}
}
All these are not working. Please help.
You need a recursive method to get all controls, or else you are going to get only the controls immediately under the panel. To get all controls with a recursive method you can use this:
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;
}
}
}
You can modify to find only textbox, or you can use this method as Panel.GetAllControls.OfType<TextBox>()
and work with all textboxes inside the panel.
Extension method to get all controls by type recursively:
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);
}
Usage:
IEnumerable<TextBox> textBoxes = panel.Controls.GetChildControls().OfType<TextBox>();
foreach (TextBox tb in textBoxes)
{
tb.Text = "";
}
This will only clear the text boxes that are direct children of you pannell. In you code if you did something like the flowing it would work
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)
}
}
}
How to Empty the all textbox inside table, then 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;
}
}
}
}
}
}
}
精彩评论