Updated March 15, 2023
Introduction to ASP.NET PlaceHolder
The placeholder control in ASP.NET is inherited from the control class. This is one of the controls in the ASP.NET framework that acts as an accommodating control for other controls and does not produce any outputs on the web page. Of course, the placeholder control is not viewed as it is not visible to the user and does not produce any HTML. Still, this control is very important in developing dynamic web pages. One more control called literal control has a similar job as placeholder control which, unlike, does not add any server controls; hence it can be used to display only static text or HTML tags on the web page. In this topic, we are going to learn about ASP.NET PlaceHolder in detail.
Syntax
<asp:PlaceHolder EnableTheming="True|False" EnableViewState="True|False" ID="string" OnDataBinding="DataBinding event handler" OnDisposed="Disposed event handler" OnInit="Init event handler" OnLoad="Load event handler"
OnPreRender="Prerender event handler" OnUnload="Unload event handler" runat="server" SkinID="string" Visible="True|False” />
Example
<asp:PlaceHolder EnableTheming="False" EnableViewState="False" ID="plc_HolderBox" OnDisposed="plc_HolderBox_Disposed" OnInit="plc_HolderBox_Init" OnLoad="plc_HolderBox_Load" OnPreRender="plc_HolderBox_PreRender" OnUnload="plc_HolderBox_Unload" runat="server" SkinID="string" Visible="True" >
<asp:CheckBox ID="chkbx_ExampleBox" Text ="Check to say Yes" Checked ="false" BackColor="#33ccff" Font-Bold="true" runat="server" OnCheckedChanged="chkbx_ExampleBox_CheckedChanged" AutoPostBack="true"/>
</asp:PlaceHolder>
The above example creates a placeholder named plc_HolderBox, the ID property sets the name of the control, and the control can be accessed anywhere in the program using the ID property. For binding the data to the controls in the placeholder, the property named is ‘OnDataBinding.’ The data-bound procedure can be bound to an event handler using the ‘OnDataBinding’ event. This control can be single-ending control or dual-ending control. To add controls into the placeholder, the control can be written in double ending tags <asp:PlaceHolder ></asp:PlaceHolder>.The CSS can be applied to the placeholder using the properties like ‘EnableThemeing,’ ‘SkinID,’ etc. The checkbox buttons can be added in between the starting and ending tags.
Properties of ASP.NET PlaceHolder
The controls can also be added at the runtime to the placeholder control by using PlaceHolder.Controls.Add method while to find the dynamically created controls in place holder use PlaceHolder.FindControl method. The controls placed in the placeholder control act as a single entity that can be accessed throughout the page. If you want to hide the text boxes and the buttons in the form, it would be better to keep everything in placeholder control and set the visible property to false. The placeholder control cannot be resized as it does not produce output. The only option to resize the controls is to place the <div> tag and use all the styles as needed.
- EnableTheming
Indicates whether themes can be added to the controls or not.
- ID
An identifier is used for the control to access it throughout the program.
- SkinID
Returns the Skin ID that needs to be applied to the control.
- Visible
The above property can be used to set if the control will be visible or not throughout the page.
- ViewState
Returns the information that can be stored and restored across multiple requests.
- Page
Returns the reference to the page that has the server control.
- OnLoad
This property raises the OnLoad event of the control.
- Context
This property returns the HttpContext object associated with the current web page’s server control.
- Controls
It returns the ControlCollection object representing the current page’s child control.
- ViewStateMode
Returns the view state mode of the control.
- TemplateControl
Returns the reference of the template that contains this control.
- OnDispose
This event is called to do specified processing when the control is released from the memory.
- OnDataBinding
The event is raised when binding the data to the control.
Example of ASP.NET PlaceHolder
Given below are the example of ASP.NET PlaceHolder:
- Open Visual Studio 2017 -> File -> Project -> Select new ASP.NET application
- Select web forms as we are showing the example for simple placeholder control.
- The placeholder control can be added from the .aspx part of the web page using the following code. However, the PlaceHolder control can only be edited in the code view, unlike the panel control, which can also be edited in the design n view.
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="WebApplication2.Default"%>
<!DOCTYPEhtml>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:PlaceHolderEnableTheming="False"EnableViewState="False” ID="plc_HolderBox"OnDisposed="plc_HolderBox_Disposed"OnInit="plc_HolderBox_Init"OnLoad="plc_HolderBox_Load"
OnPreRender="plc_HolderBox_PreRender"OnUnload="plc_HolderBox_Unload” runat="server"SkinID="string"Visible="True">
<asp:LabelID="Label1"Text="Please check the checkbox."runat="server"></asp:Label>
<asp:CheckBoxID="chkbx_ExampleBox"Text="Check to say Yes"Checked="false"BackColor="#33ccff"Font-Bold="true"runat="server"OnCheckedChanged="chkbx_ExampleBox_CheckedChanged"AutoPostBack="true"/>
</asp:PlaceHolder>
</div>
</form>
</body>
</html>
The placeholder control here will hold the label that states, ‘Please check the checkbox and the checkbox named ‘chbk_ExampleBox’into it. The visible property of the PlaceHolder control is set to true here; hence the inner child controls are visible, if the visible property of the PlaceHolder is set to false, the inner child control will be hidden, and the user will not be able to see them on the web page. The visible property of PlaceHolder control here overrides the label and checkbox button’s visible properties (if any).
- The placeholder control can be added by dragging and dropping the control from the ASP.NET toolbox.
- Different properties mentioned above can be set from the properties window present on the right side of the visual studio IDE.
- The output of the above code would look like this. Even though the controls label and the checkbox button are kept in the PlaceHolder control, that output shows no visible difference as no output is produced by the control.
Once the checkbox is checked, the output will show
Conclusion
Another control called Panel control acts as same as holding other controls without producing any HTML. Still, one of the main drawbacks of using the Panel control is that panel control is when it comes to rendering; the panel control is very heavy. Therefore, it needs to be rendered as nothing, while the placeholder control is only replaced with child controls. One more advantage of placeholder control is that it can be placed anywhere in the HTML page, as in between <head> tags, too, but the Panel control needs to place only within ASP.NET web forms tags.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET PlaceHolder” was beneficial to you. You can view EDUCBA’s recommended articles for more information.