c# - 将多个控件的特殊属性设置为相同的值

标签 c# asp.net

如何将多个控件的特殊属性设置为相同的值? 例如,将表单中所有标签的 visible 属性设置为 true。 我使用此代码,但标签似乎具有空值,但它们具有值。

protected void Page_Load(object sender, EventArgs e)
{
 foreach ( Label lbl in this.Controls.OfType<Label>()) {
         if (lbl == null) continue;
         lbl.Visible = false;
          } 
}

我应该提到我使用母版页。但我不想设置嵌套母版页的属性。我只想设置当前 ASP 页面的属性。

最佳答案

你可能在其他人中有一些控件,所以你需要递归地调用它....这是我使用的一个类似的方法......................

注意最后,如果有问题的控件有自己的控件,我会从自身内部调用它......

希望这对你有帮助......

private void ClearControls(ControlCollection controlCollection, bool ignoreddlNewOrExisting = false)
{
    foreach (Control control in controlCollection)
    {
        if (ignoreddlNewOrExisting)
        {
            if (control.ID != null)
            {
                if (control.ID.ToUpper() == "DDLNEWOREXISTING")
                {
                    continue;
                }
            }
        }

        if (control is TextBox)
        {
            ((TextBox)control).Text = "";
            ((TextBox)control).Font.Size = 10;
        }
        if (control is DropDownList)
        {
            ((DropDownList)control).SelectedIndex = 0;
            ((DropDownList)control).Font.Size = 10;
        }
        if (control is CheckBox)
        {
            ((CheckBox)control).Checked = false;
        }

        //A bit of recursion
        if (control.Controls != null)
        {
            this.ClearControls(control.Controls, ignoreddlNewOrExisting);
        }
    }
}

关于c# - 将多个控件的特殊属性设置为相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35476050/

相关文章:

javascript - 通过 ajax 将 LIst<t> 发送到复杂模型

ASP.Net 在代码隐藏中启用跟踪

c# - Gridview动态添加新行

c# - 无法在 LINQ to SQL 中将类型 'string' 隐式转换为 'System.Data.Linq.Binary

c# - OnRelease 期间的异常是否会导致组件无法正确处置?

javascript - 如何在浏览器关闭之前保留用户名?

javascript - 如何隐藏/显示在 ASP.NET 中动态创建的 HTML 元素

c# - 提交后如何在后面的代码中重置RadAsyncUpload?

c# - 预 ASP.NET Core MVC 应用程序中的 Razor 标记帮助程序

c# - VS2017错误调试元组任务