C# 如何使用反射读取webcontrol的style[“display”]属性的值
我的页面上有一些用户控件和一些web控件。要使用反射读取每个控件的可见性属性,我在下面的行中写道:C# 如何使用反射读取webcontrol的style[“display”]属性的值,c#,asp.net,reflection,asp.net-webcontrol,C#,Asp.net,Reflection,Asp.net Webcontrol,我的页面上有一些用户控件和一些web控件。要使用反射读取每个控件的可见性属性,我在下面的行中写道: Object v; if (control.GetType().GetProperty("Visible") != null) v = control.GetType().GetProperty("Visible").GetValue(control, null); 但是如何使用反射读取每个控件的Style[display]属性的值呢 提前感谢。这里是一个使用按钮的工作示例,仅用于演示
Object v;
if (control.GetType().GetProperty("Visible") != null)
v = control.GetType().GetProperty("Visible").GetValue(control, null);
但是如何使用反射读取每个控件的Style[display]属性的值呢
提前感谢。这里是一个使用按钮的工作示例,仅用于演示目的 必须将属性作为我们要查找的键的Style属性应用于它 标记:
<asp:Button ID="Button1" runat="server" Text="Button" Visible="false" style="display:block;" />
您将不得不用您已有的控件替换按钮。谢谢您的回复。但styleCollection的计数为0,在执行最后一行后,其值为null。有什么我们遗漏的吗?请忽略我之前的评论。。。如果控件在任何css集合中都应用了style属性,那么它的效果会非常好…谢谢您的帮助。
var styleProp = Button1.GetType().GetProperty("Style");
if (styleProp != null)
{
var styleCollection = styleProp.GetValue(Button1, null) as CssStyleCollection;
var value = styleCollection["display"];
}