To understand how the tag works <asp:Content...>
it is necessary to understand the concepts of master page and content page.
The Asp.Net
allows a page to be segmented into small parts, being a "fixed" part that is called master page, which may be a layout.
Imagine that all or several pages of your site will have one layout similar, like header and menu at the top and a footer at the end. To avoid having to "duplicate" this code on each page, you create a "master page" with this layout, and create other pages only with content that will change.
Take this example:
This page, in addition to the html content, will have a structure similar to this one, and we will call it "Master1.master":
<%@ Master Language="CS" %>
<body>
<form id="form1" runat="server">
<div><asp:contentplaceholder id="Header" runat="server" /></div>
<div><asp:contentplaceholder id="PageSpecific" runat="server" /></div>
<div><asp:contentplaceholder id="Advertisement" runat="server" /></div>
<div><asp:contentplaceholder id="Recomended" runat="server" /></div>
<div><asp:contentplaceholder id="Footer" runat="server" /></div>
</form>
</body>
Note two things:
- the tag
<%@ Master
at the beginning, indicating that it is a master page
- the tag
<asp:contentplaceholder
, indicating that some page will "implement" that part
Now comes the second part, the content page, which is the content page that will "compose" a part of the master page, and this is where the tag of your question comes in, <asp:Content>
.
Let’s see a page that will compose the "header" for example:
<%@ Page Language="CS" MasterPageFile="~/MasterPages/Master1.master" Title="Content Page" %>
<asp:Content ID="Header1" ContentPlaceHolderID="Header" Runat="Server">
<header>Aqui vai o header </header>
</asp:Content>
Note here two important things:
- the attribute
MasterPageFile="~/MasterPages/Master1.master"
, saying that this page will compose a part of the master page "Master1.master", the one created above.
- the tag
<asp:Content ID="Header1" ContentPlaceHolderID="Header"
, indicating that the contents of this page will compose the "Header" of master page.
That is, it is a partial page, hence the limitation on the tags. As it is part of a master, tags like <html>
, <head>
and <body>
should not exist here, but in master page, because at the end both will be rendered as a single page, and we cannot have these duplicated tags in the same document, but you can add css
to it and modify the style normally.
If you want to know more, here’s a reference from the Microsoft documentation: ASP.NET Master Pages Overview