【问题标题】:How To Set Many Elements Visible Property at one time in ASP.NET如何在 ASP.NET 中一次设置多个元素的可见属性
【发布时间】:2023-03-17 13:49:01
【问题描述】:

假设我有 5 个元素 => Element1、Element2、Element3、Element4、Element5。

设置其可见属性的一般方法是:

 Element1.Visible = false;
 Element2.Visible = false;
 Element3.Visible = false;
 Element4.Visible = false;
 Element5.Visible = false;

或者另一种方式是

 Element1.Visible = Element2.Visible = Element3.Visible = Element4.Visible = Element5.Visible = false;

现在我的问题是:

是否可以同时设置所有元素属性 Visible 而无需对每个元素名称都写入 Visible。在 Simple Word 中只需要写 Visible 一次。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    你可以找到控件并设置它的可见属性

    这是示例代码

    protected void Page_Load(object sender, EventArgs e)
    {
        HideRadioButtonLists(Page.Controls);
    }
    
    private void HideRadioButtonLists(ControlCollection controls)
    {
        foreach (WebControl control in controls.OfType<WebControl>())
        {
            if (control is RadioButtonList)
                control.Visible = false;
            else if (control.HasControls())
                HideRadioButtonLists(control.Controls);
        }
    }
    

    看看对你有没有帮助。

    【讨论】:

    • 是否可以使用我的第二种方式并且不需要针对每个元素编写 Visible 来设置它们的属性。即编写最少的代码
    • 不,您需要明确设置值。你需要写几行代码。
    猜你喜欢
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 2016-10-05
    相关资源
    最近更新 更多