私は .Net プラットフォームを初めて使用し、.aspx ページからコントロールを取得するのに 2 日間かかっています。
私は自分のウェブサイトのすべてのページcontrolsからすべてを取得しようとしています。.aspxそのために、データベースから取得したクラス名の文字列から Page のオブジェクトを作成します。class names.aspx.cs ファイルをデータベースに既に保存してい ます
C# のコードは次のとおりです。
Page obj = (Page)Activator.CreateInstance(null, string ClassName).Unwrap();
文字列"ClassName"はデータベースから取得されます。
デバッグ時間中に にあることがわかりますがcontrols、objに0入りcontrols.countます。これは、コントロールがまだnot initialized.
デバッグ時の画像 1
:
自分のコントロールを表示しているデバッグ時の画像 2
私のコードは次のようになります。
Page obj = (Page)Activator.CreateInstance(null, string ClassName).Unwrap()
List<string[]> fieldsNotInDB = GetControlCollections(obj)
これは、からすべてを取得する私のfunctionものですcontrolsPage obj
public List<string[]> GetControlCollections(Page p)
{
List<string[]> controlList = new List<string[]>();
IterateControls(p.Controls, controlList);
return controlList;
}
public void IterateControls(System.Web.UI.ControlCollection page, List<string[]> controlList)
{
foreach (System.Web.UI.Control c in page)
{
if (c.ID != null)
{
string []s=new string[2];
s[0]=c.ID;
s[1]=c.GetType().ToString();
controlList.Add(s);
}
if (c.HasControls())
{
IterateControls(c.Controls, controlList);
}
}
}
Controlsからを取得するにはどうすればよいobjですか?