1

I have some user controls and some web controls on my page. To read the Visibility property of the each control using reflection I wrote below line :

Object v;
if (control.GetType().GetProperty("Visible") != null)
     v = control.GetType().GetProperty("Visible").GetValue(control, null);

but how can I read the value of Style["display"] attribute of each control using reflection?

Thanks in advance.

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
Pratik Gaikwad
  • 1,526
  • 2
  • 21
  • 44

1 Answers1

0

Here is a working example using a button for demonstration purposes only.

Style property with the attribute as a key we are looking for must be applied to it.

Markup:

<asp:Button ID="Button1" runat="server" Text="Button" Visible="false" style="display:block;" />

Code behind:

var styleProp = Button1.GetType().GetProperty("Style");
        if (styleProp != null)
            {
                var styleCollection = styleProp.GetValue(Button1, null) as CssStyleCollection;
                var value = styleCollection["display"];
            }

You would have to replace the button with which ever control you have already.

Pratik Gaikwad
  • 1,526
  • 2
  • 21
  • 44
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
  • Thanks for the reply. But styleCollection has 0 count and getting null in value after execution of last line. Is there anything that we are missing? – Pratik Gaikwad Nov 24 '13 at 11:48
  • Please ignore my earlier comment... Worked like a charm if control has style property applied to it with any css collectio...thanks for help. – Pratik Gaikwad Nov 24 '13 at 11:57