簡體   English   中英

通過參考查找aspx頁面中的所有控件

[英]Finding all controls in aspx page by reference

我需要在aspx頁面中更改許多控件的可見性。

我發現了幾種獲得像這樣的控件的方法

但是我無法為此控件設置值,因為它們是通過val傳遞的,在這種情況下,我不知道如何添加ref關鍵字。

試試這個鏈接 ,它應該可以正常工作。 順便說一句,控制是引用類型而不是值類型。

從您問題的示例中,執行以下操作:

IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
    foreach (Control child in parent.Controls)
    {
        yield return child;
        foreach (Control descendant in EnumerateControlsRecursive(child))
            yield return descendant;
    }
}

用法:

foreach (Control c in EnumerateControlsRecursive(Page))
{
    if(c is TextBox)
    {
        var theTextBox = c as TextBox;
        theTextBox.Visible = false;
    }

    if(c is Label)
    {
        var theLabel = c as Label;
        theLabel.Visible = false;
    }

    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM