This tutorial will show how to create a PlaceHolder in Visual Basic.NET. A PlaceHolder control can be used as a container control inside a document to dynamically load other controls. The PlaceHolder control does not have any visible output and is used to hold controls added at run time. In this tutorial we will use a PlaceHolder to hold TextBox, Button, and Literal controls and dynamically change attributes of the Button and Literal controls from the PlaceHolder control.

Setting Up

If you have not done so already, please create a new web site in Visual Studio. To do so, open Visual Studio and click File > New > Web Site. Using ‘Visual Basic’ as the template, select ASP.NET Empty Web Site, name the web site PlaceHolderControl-VB and click OK. Now right click the web site name in the solution explorer and click Add New Item.

Step One

Open Default.aspx in source view and place a PlaceHolder control inside the div tags. Now place a TextBox, Button, and Literal control inside the PlaceHolder. Change the ID of the Button control to “PlaceHolderBtn” and the ID of the Literal control to “InsideLit.” Make sure to also change the ‘Visible’ attribute of the PlaceHolder to ‘False’ as we don’t want to show the PlaceHolder on page load.

Below the closing PlaceHolder tag we will add two Buttons and a Literal control. We will change the Button IDs to “ShowBtn” and “HideBtn” and also change the texts to “Show Place Holder” and “Hide Place Holder” accordingly. We will also change the ID of the Literal control to “OutsideLit.”

Step Two

Switch to design view of Default.aspx and double click the “ShowBtn” control to generate a click event in the code behind. Inside this event, we will programmatically add some attributes to the InsideLit and PlaceHolderBtn controls. We will change the text of the InsideLit control to “Inside of Place Holder” as well as change the text of the PlaceHolderBtn control to “Send”. Since we will use this button to show the PlaceHolder control, we must set the Visible attribute of the PlaceHolder to ‘True.’

We must create a click event for the PlaceHolderBtn but because since it is inside the PlaceHolder we are unable to access it to generate a click event and therefore must create one. The easiest way to do this is to copy the ‘Protected Sub’ handles event from one of the previous click events and change the event name to “PlaceHolderBtn_Click.”

When a click event is in place, we will set the texts of TextBox1 to “Outside of Place Holder” and OutsideLit to “TextBox1.Text.”

Output

Save and run the web site. You will see two buttons, “Show Place Holder” and “Hide Place Holder.”

placeholder 1

Click the ‘Show Place Holder’ button and a three things will appear: a TextBox, Button, and Literal control with the text “Inside of Place Holder.” See how nothing is in the TextBox? Since we programmatically entered text in the TextBox we are able to click the ‘Send’ button and the message “Outside of Place Holder” will automatically appear in the TextBox and outside of the PlaceHolder.

placeholder 2

placeholder 3

Now click the ‘Hide Place Holder’ button and the “Outside of Place Holder” message will remain because it is not inside the PlaceHolder control which is now hidden from the page.

placeholder 4

Thanks for reading and make sure to download the source files to get a better understanding of how the code works.

Download Source Files