Search code examples
c#asp.netpropertiesvisible

Is there a way to get a list of all the visible properties on a page?


I want to get a list of all the visible properties on a page within the OnClick() method of a particular button. Is there a way to do this programmatically in c# within asp.net?


Solution

  • You'll need to recursivly itterate all the controls in the page and find the visible ones :

    List<Control> visibleList = null;
    protected void FindVisibleControls(Control parent) 
    {
        foreach(Control c in parent.Controls) 
        {
           if (c.Visible)
           {
              visibleList.Add(c);
           }
    
           if (c.HasControls())
              FindVisibleControls(c);
        }
    }
    

    Usage - In your button click call it like this:

    protected Button1_Click(object sender, EventArgs e)
    {
       visibleList = new List<Control>();
       FindVisibleControls(this);
    }