0

C# を使用してサーバー側からユーザー コントロールを含む複数のコンテンツ プレースホルダーを読み込む方法..

現在サーバー側(page_load内)から、次のように1つのユーザーコントロールをロードしています:

ContentPlaceHolder cph = this.Master.FindControl("TopContentPlaceHolder") as ContentPlaceHolder;
UserControl uc = this.LoadControl("~/webusercontrols/topmenu.ascx") as UserControl;
if ((cph != null) && (uc != null))
{
    cph.Controls.Add(uc);
}

ページに 8 つのユーザー コントロールをロードする必要があります。どうすれば同じことを達成できますか? 前もって感謝します..

4

1 に答える 1

1
string[] listOfControls; //initialize as you will
ContentPlaceHolder cph = this.Master.FindControl("TopContentPlaceHolder") as ContentPlaceHolder;
for (int i=0; i<listOfControls.Length; i++)
{
   UserControl uc = this.LoadControl(listOfControls[i]) as UserControl;
   if ((cph != null) && (uc != null))
   {
       cph.Controls.Add(uc);
   }
}

または、プレースホルダーごとに 1 つのユーザー コントロールが必要な場合は、プレースホルダーのリストを vell として作成できます。

string[] placeholders;
string[] listOfControls; //initialize as you will

    for (int i=0; i<listOfControls.Length; i++)
    {
       ContentPlaceHolder cph = this.Master.FindControl(placeholders[i]) as ContentPlaceHolder;
       UserControl uc = this.LoadControl(listOfControls[i]) as UserControl;
       if ((cph != null) && (uc != null))
       {
           cph.Controls.Add(uc);
       }
    }
于 2010-08-20T09:27:11.820 に答える