【发布时间】:2016-08-19 07:06:57
【问题描述】:
我正在尝试从某个页面 (this.Page.Controls) 解析我所有控件的所有属性(例如 ascx 文件),以获得具有我指定的属性值的属性的名称,例如 - 具有“这是我的标题”值的属性的名称是什么? (很可能是一个包含该值的文本框)。
以下返回此错误:
无法获取的内部内容,因为内容不是文字。 System.Web.UI.HtmlControls.HtmlContainerControl.get_InnerHtml()
不确定它的含义或如何纠正它。
public String searchMethod(List<Control> listOfControls, String searchedValue)
{
String result = "";
foreach (var control in listOfControls)
{
PropertyInfo[] properties = control.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
if (property.PropertyType == typeof (string)) //added condition on this line *******************************
{
if (property.GetValue(control, null) != null)
if (property.GetValue(control, null).ToString().Contains("searched String"))
{
result = result + property.Name + "/" + property.GetValue(control, null) + "/";
}
}
}
}
return result;
}
我想它卡在了一个不符合要求的属性上,那么为什么不直接移动到下一个直到找到合适的位置?
显然,卡住的属性是System.String Inner.Html
PS。我已经测试了提供给该方法的 listOfControls 并且它正在正确生成
后期更新: 我正在尝试的另一种方法是:
public string Method1(List<Control> controlList, string propName)
{
string result = "";
foreach (var control in controlList)
{
foreach(var prop in control.GetType().GetProperties())
{
if(prop.PropertyType == typeof(string))
{
if((prop.GetValue(control,null).GetType()) == typeof(string))
if (prop.GetValue(control, null).ToString().Contains(propName))
result += prop.Name + "######";
}
}
}
return result;
}
但是有了这个我得到对象引用未设置为对象的实例。在线if((prop.GetValue(control,null).GetType()) == typeof(string))
【问题讨论】:
标签: c# user-controls controls propertyinfo