使用母版页清除ASP.NET页面中的所有控件

4 c# asp.net

我有一个asp.net页面继承自母版页.我想清除此页面中的所有控件.我尝试使用bellow方法.如果有母版页,这是行不通的.否则它的工作正常吗?

private void ClearControls()
{
    foreach(Control c in Page.Controls)
    {
        foreach (Control ctrl in c.Controls)
        {
            if (ctrl is TextBox)
            {
                ((TextBox)ctrl).Text = string.Empty;
            }
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

ogu*_*gun 6

试试这个:

public void FindAllTextBox(Control ctrl)
{
    if (ctrl != null)
    {
        foreach (Control c in ctrl.Controls)
        {
            if (c is TextBox)
                ((TextBox)c).Text = string.empty;
            FindAllTextBox(c);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

例:

Control ctrl = this.FindControl("content");
FindAllTextBox(ctrl);
Run Code Online (Sandbox Code Playgroud)