Export (0) Print
Expand All

How to: Add PlaceHolder Web Server Controls to a Web Forms Page (Visual Studio)

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

To add a PlaceHolder Web server control to a Web Forms page

  • From the Standard tab of the Toolbox, drag a PlaceHolder control onto the page.

To add child controls to a PlaceHolder control at run time

  1. Create an instance of the control that you want to add to the PlaceHolder control.

  2. Call the Add method of the PlaceHolder control's Controls property, passing it the instance that you created in the previous step.

    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

Community Additions

ADD
Show:
© 2014 Microsoft