PlaceHolder in ASP.NET using VB.NET

In this article you will learn about what is PlaceHolder control,its class hierarchy and how you can use this control.
  • 8542
Download Files:
 

The PlaceHolder control can be used  as a container control within a document to dynamically load other controls. The PlaceHolder control has no HTML based output and is used only to mark a spot for other controls that can be added to the control collection of the PlaceHolder during Execution. This control is used to preserve space for controls added by code. This control remains invisible when added to the page. The advantage of using PlaceHolder control is that the TextBox, Button and other content contained within the PlaceHolder opening and closing HTML Tags are Kept as a Single Entity.
 
Class Hierarchy for PlaceHolder control:-

  • Object 
  • Control 
  • PlaceHolder
     

How to use PlaceHolder Control:-
  • Simply open a new ASP.NET Web Application. 
  • Drag the PlaceHolder control and a Button on design page. The page will look like below.

    PlaceHolder1.gif
     
  • The below code will appear on Default.aspx page after adding controls to page.

    <%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
        CodeBehind="Default.aspx.vb" Inherits="AdRotator._Default" %>
    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <
    asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </asp:Content>
     
  • Then add the below code on code behind file of the Web page.

       
    Dim TextBox11, TextBox12 As New System.Web.UI.WebControls.TextBox()
        Dim Button11 As New System.Web.UI.WebControls.Button()
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As  _
            System.EventArgs) Handles MyBase.Load
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As  _
            System.EventArgs) Handles Button1.Click
            TextBox11.Text = "Created TextBox11"
            TextBox12.Text = "Created TextBox12"
            Button11.Text = "Created Button"
            TextBox11.Columns = 15
            TextBox11.ReadOnly = True
            TextBox12.Columns = 20
            PlaceHolder1.Controls.Add(TextBox11)
            PlaceHolder1.Controls.Add(TextBox12)
            PlaceHolder1.Controls.Add(Button11)
         End Sub

Output:-

PlaceHolder2.gif

PlaceHolder3.gif

PlaceHolder4.gif
PlaceHolder5.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.