名前を取得するために、特定のページ(this.Page.Controls)からすべてのコントロール(たとえばascxファイル)のすべてのプロパティを解析しようとしています。私が指定したプロパティ値を持つプロパティ。たとえば、 "this is my header"という値を持つプロパティの名前は? (これは、この値を含むテキストボックスである可能性があります)。C#の条件に基づいてコントロールのすべてのプロパティ名を一覧表示
以下、このエラーを返します:
Cannot get inner content of because the contents are not literal. 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;
}
が、これで私は、オブジェクト参照のインスタンスに設定されていない取得しますオブジェクト。後者のシナリオは、あなたのケースで発生しているよう
There is more than one HTML server control.
- or -
The HTML server control is not a System.Web.UI.LiteralControl or a System.Web.UI.DataBoundLiteralControl.
ですね: - ラインif((prop.GetValue(control,null).GetType()) == typeof(string))
ありがとう、Jon Skeet!私はあなたのアドバイスを適用しようとしていたし、私は私の最初の質問を再編集する2つのバージョン(少なくとも)を思い付いた。何かご意見は? 2番目のバージョンははるかに簡単できれいに見えますが、最終的にエラーをスローします...最終的に。私は文法に巧妙ではない。 – Halle
@ハール:プロパティがnullを返すとうまくいかないでしょう。しかし、それは不要です。プロパティ型が 'string'の場合、' string'やnull参照以外のものは返すことができません。さらに重要なことは、あなたはまだ初期の問題を引き起こした同じプロパティの値を尋ねることになるだろう。 –
@ハール:プロパティがnullを返す場合は、null参照-bangで 'GetType()'を呼び出しています。問題( 'HtmlContainer.InnerHtml')を引き起こしたプロパティは、「様々なタイプのもの」ではない - それは文字列型です。基本的に、あなたの変更は、私が見る限り、元の問題を回避するものではありません。 –