mehrere Inhalt Platzhalter mit Benutzersteuerungen von Server-Seite unter Verwendung von C # laden

StackOverflow https://stackoverflow.com/questions/3529521

Frage

Wie mehrere Content-Platzhalter mit Benutzersteuerung von Server-Seite mit C # laden ..

zur Zeit von Serverside (in page_load) Ich lade ein Usercontrol wie folgt aus:

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);
}

Ich brauche 8 Benutzersteuerelemente in meiner Seite zu laden. Wie kann ich das gleiche erreichen? Vielen Dank im Voraus ..

War es hilfreich?

Lösung

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);
   }
}

oder im Fall, dass Sie pro Platzhalter ein Benutzerkontrolle möchten, können Sie eine Liste der Platzhalter als vell

erstellen
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);
       }
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top