How to Create a PlaceHolder in Visual Basic.NET
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.
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.
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="False">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="PlaceHolderBtn" runat="server" />
<asp:Literal ID="InsideLit" runat="server"></asp:Literal>
<asp:Literal ID="OutsideLit" runat="server"></asp:Literal>
<asp:Button ID="ShowBtn" runat="server" Text="Show Place Holder" />
<asp:Button ID="HideBtn" runat="server" Text="Hide Place Holder" />
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.’
Protected Sub ShowBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ShowBtn.Click
InsideLit.Text = "Inside of Place Holder"
PlaceHolderBtn.Text = "Send"
PlaceHolder1.Visible = True
Protected Sub HideBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles HideBtn.Click
PlaceHolder1.Visible = False
When a click event is in place, we will set the texts of TextBox1 to “Outside of Place Holder” and OutsideLit to “TextBox1.Text.”
Protected Sub PlaceHolderBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PlaceHolderBtn.Click
TextBox1.Text = "Outside of Place Holder"
OutsideLit.Text = TextBox1.Text
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.