使用母版页清除表单上的所有字段

时间:2013-07-19 19:46:12

标签: c# asp.net .net

我想点击按钮上的表格,这是我现在的位置:

     ContentPlaceHolder mpContentPlaceHolder;
        mpContentPlaceHolder =
   (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");

        foreach (Control ctrl in mpContentPlaceHolder.Controls)
        {
            lblMessageX.Text = ctrl.ToString();
            if (ctrl is TextBox)
                ((TextBox)ctrl).Text = string.Empty;
            else if (ctrl is DropDownList)
                ((DropDownList)ctrl).ClearSelection();
        }

它只循环一次,并找到:System.Web.UI.LiteralControl

是不是在正确的地方搜索?

我的模拟看起来像:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 <asp:Accordion   
    ID="Accordion1"   
    CssClass="accordion"  
    HeaderCssClass="accordionHeader"  
    HeaderSelectedCssClass="accordionHeaderSelected"  
    ContentCssClass="accordionContent"   
    runat="server">  

<Panes >
<asp:AccordionPane runat="server" >
    <asp:DropDownList ID="ddlOrg" runat="server"  AutoPostBack="true" CssClass="myinputstyle"
                   OnSelectedIndexChanged="ddlOrg_SelectedIndexChanged" />  
      <asp:TextBox ID="txtCase" runat="server" CssClass="myinputstyleS" MaxLength="8" />  
//and so on and so forth

1 个答案:

答案 0 :(得分:1)

您需要进行递归搜索以获取页面上的所有控件,因为并非所有控件都在ContentPlaceHolder下。相反,它包含一些控件本身包含其他控件。

Content Place Holder
-->LiteralControl
   -->Other Control 1
   -->Other Control 2
   -->Other Control 3
      -->Maybe another level 1
      -->Maybe another level 2
   -->Other Control 4

LiteralControl.Controls将返回“Other Control 1”到4,但不是“Maybe another 1”或“Maybe another level 2”。要获得这些,您需要调用“其他控件3”。控件

在调试器中查看确切的层次结构。