Click here to Skip to main content
15,891,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

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.
Posted
Comments
Kornfeld Eliyahu Peter 24-Nov-13 4:04am    
Why do you think it has a 'display' style on it?
'display' is a client side style that for most server side rendering will be ignored...
Pratik Gaikwad 24-Nov-13 7:05am    
Peter even though Style is client side scripting change,its possible to obtain values of attributs. see solution marked as answer
Kornfeld Eliyahu Peter 24-Nov-13 7:14am    
First, I didn't talk about Style, but 'display'. And I didn't say that you can't access it. What I was told that 'display' will have no value and will not be rendered to client in most of the cases. An more Visible has no effect on 'display'.
Visible tells server not to render the control at all...
'display' will declare how the client-side counterpart of this control will be placed on the page...
What I'm telling you that asking about 'display' in server-side code, and for sure using reflection - is mostly a waste of time...

Hi Patrik

C#
Style  is of type CssStyleCollection
, you cant get the values from reflection..
you can access the client side properties and values from the style name or key name


in code behind u can access the client side style by
C#
var display = txtbox.Style["display"];
 
Share this answer
 
Comments
Pratik Gaikwad 24-Nov-13 7:16am    
Karthik, I have different type of controls that i need to find... Hence your option can not be used since everyone of them might not have Style property assigned to them(for e.g. UserControls)... Thanks for reply.
Style property with the attribute as a key we are looking for must be applied to it.

Then use below code to get it.

C#
var styleProp = control.GetType().GetProperty("Style");
            if (styleProp != null)
            {
                var styleCollection = styleProp.GetValue(control, null) as CssStyleCollection;
                var value = styleCollection["display"];
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900