Share via


How to: Add 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

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 of the PlaceHolder control's Controls collection, passing it the instance you created.

    The following example shows how to add two Button controls as children of a PlaceHolder control. The code also adds a Literal control in order to add a br tag between the buttons.

    Protected Sub Page_Load(ByVal sender As System.Object, _
           ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Button1 As Button = New Button()
        Button1.Text = "Button 1"
        PlaceHolder1.Controls.Add(Button1)
        Dim Literal1 As New Literal()
        Literal1.Text = "<br>"
        PlaceHolder1.Controls.Add(Literal1)
        Dim Button2 As New Button()
        Button2.Text = "Button 2"
        PlaceHolder1.Controls.Add(Button2)
        PlaceHolder1.Controls.Add(Button2)
    End Sub
    
    void Page_Load(object sender, EventArgs e)
    {
        Button Button1 = new Button();
        Button1.Text = "Button 1";
        PlaceHolder1.Controls.Add(Button1);
    
        Literal Literal1 = new Literal();
        Literal1.Text = "<br>";
        PlaceHolder1.Controls.Add(Literal1);
    
        Button Button2 = new Button();
        Button2.Text = "Button 2";
        PlaceHolder1.Controls.Add(Button2);
    }
    

See Also

Reference

PlaceHolder Web Server Control Overview