2011-05-30 139 views
1

我想清空面板中的文本框控件。 請解釋。如何清除面板中的文本框控件

下面

是我的代碼:

<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> 

我已經使用的方法明文(Panel_CreateLead);在頁面加載不起作用。

以明文的形式使用()的代碼:

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 = ""; 
      } 
     } 
    } 
} 

所有這些都不能正常工作。請幫忙。

+1

嘗試PreRender事件而不是Page_Load .... – 2GDev 2011-05-30 13:45:58

+0

請參見http:// st ackoverflow.com/questions/4863051/loop-through-textboxes – abatishchev 2011-05-30 14:11:47

回答

0

這隻會清除你pannell的直接子女的文本框。在你的代碼,如果你不喜歡的東西流動會工作

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) 
     } 
    } 
} 
+0

我添加了上面的代碼:和IIS崩潰了。 – Vineeta 2011-05-31 12:41:51

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>(),並與面板內的所有文本框的工作。

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 = ""; 
} 
+0

@BrunoLM使用'yield',我使用'SelectMany()' – abatishchev 2011-05-30 14:07:07

+0

這不是遞歸的。嵌套4面板,並從頂部面板開始查找最後一個嵌套面板內的「TextBox」。它找不到這些元素,因爲它只會查看'TextBox'裏面的元素,但是它們在'Panel'裏面。http://pastebin.com/8wJQEgmA – BrunoLM 2011-06-07 12:38:33

+0

@Bruno:你說得對。我修復了我的答案 – abatishchev 2011-06-07 13:27:36

-1

如何清空內表中的所有文本框,然後trtd,...

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