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.
Code Block
Default.aspx
Add a PlaceHolder control.
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="False">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="PlaceHolderBtn" runat="server" />
    <br />
    <br />
    <asp:Literal ID="InsideLit" runat="server"></asp:Literal>
</asp:PlaceHolder>
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.”
Code Block
Default.aspx
Add controls outside of the PlaceHolder.
<asp:Literal ID="OutsideLit" runat="server"></asp:Literal>
<br />
<br />
<asp:Button ID="ShowBtn" runat="server" Text="Show Place Holder" />
<br />
<br />
<asp:Button ID="HideBtn" runat="server" Text="Hide Place Holder" />

We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud hosting!

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.’
Code Block
Default.aspx.vb
The ShowBtn click event method.
Protected Sub ShowBtn_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles ShowBtn.Click
InsideLit.Text = "Inside of Place Holder"
PlaceHolderBtn.Text = "Send"
PlaceHolder1.Visible = True
End Sub
Switch back to design view and double click the “HideBtn” control to generate a click event. In this event we will do the opposite of the ShowBtn event and set the Visible attribute of the PlaceHolder to ‘False’.
Code Block
Default.aspx.vb
The HideBtn click event method.
Protected Sub HideBtn_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles HideBtn.Click
PlaceHolder1.Visible = False
End Sub
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.”
Code Block
Default.aspx.vb
PlaceHolderBtn_Click event method.
Protected Sub PlaceHolderBtn_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles PlaceHolderBtn.Click
TextBox1.Text = "Outside of Place Holder"
OutsideLit.Text = TextBox1.Text
End Sub

We stand behind Server Intellect and their support team. They offer dedicated servers, and they are now offering cloud hosting

Output

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



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.

                       

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.



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

PlaceHolderControl-VB.zip