This documentation is archived and is not being maintained.

Adding PlaceHolder Web Server Controls to a Web Forms Page

Add a PlaceHolder Web server control to your page when you want to dynamically add, remove, or loop through controls at run time.

To add a PlaceHolder control to a Web Forms page

  • In Design view, drag a PlaceHolder control from the Web Forms tab of the Toolbox onto the page.

To add child controls to a PlaceHolder control at run time

  1. Create an instance of the control you want to add to the PlaceHolder control.
  2. Call the Add method of the PlaceHolder control's Controls property, passing it the instance you created.

    The following example shows how to add three HtmlButton controls as children of a PlaceHolder control.

    ' Visual Basic
    Private Sub Page_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
       Dim btnClick As HtmlButton = New HtmlButton()
       btnClick.InnerText = "Button 1"
       PlaceHolder1.Controls.Add(btnClick)
       btnClick = New HtmlButton()
       btnClick.InnerText = "Button 2"
       PlaceHolder1.Controls.Add(btnClick)
       btnClick = New HtmlButton()
       btnClick.InnerText = "Button 3"
       PlaceHolder1.Controls.Add(btnClick)
    End Sub
    
    // C#
    private void Page_Load(object sender, System.EventArgs e)
    {
       HtmlButton btnClick = new HtmlButton();
       btnClick.InnerText = "Button 1";
       PlaceHolder1.Controls.Add(btnClick);
       btnClick = new HtmlButton();
       btnClick.InnerText = "Button 2";
       PlaceHolder1.Controls.Add(btnClick);
       btnClick = new HtmlButton();
       btnClick.InnerText = "Button 3";
       PlaceHolder1.Controls.Add(btnClick);
    }
    

See Also

Introduction to the PlaceHolder Web Server Control | PlaceHolder Web Server Control | Adding Controls to a Web Forms Page Programmatically

Show: