Including headers and footers from another files in ASP.NET

14,424

Solution 1

Webforms - you'll want to use user controls.

ASP.NET MVC - you'll want to use partial views.

A quick note based on one of the other answers - if you're using webforms and using this strictly to use a common header and footer for many pages on your site, then master pages actually would be the best approach in my opinion. But if you are asking, in general, how to render re-usable blocks of code in an ASP.NET web application, similar to how you use includes in php, then user controls / partial views is what you're looking for.

Solution 2

In a master page register the control:

<%@ Register src="~/Controls/Header.ascx" tagname="Header" tagprefix="abc" %>
<%@ Register src="~/Controls/Footer.ascx" tagname="Footer" tagprefix="abc" %>

And in the master page's body:

<body>
        <form id="form1" runat="server">
            <div id="header">
                <abc:Header ID="abcHeader" runat="server" />
            </div> 


            <div id="footer">
                <abc:Footer ID="abcFooter" runat="server" />           
            </div> 
        </form>
</body>  

Solution 3

In php you have to use include() fuction but in asp.net you use the concept of master pages. Using master pages, you dont need to do this. Watch this http://www.youtube.com/watch?v=epaavQsb950

and also check out this http://www.youtube.com/watch?v=RHoyxLjcSYA

Solution 4

In normal ASP.NET websites, we achieve it by using User Controls. User Controls are nothing but partial set of controls which can be re-used in many screens. Header and Footer are the best examples of it. Please go through ASP.NET User Controls (MSDN) to know more about user controls and here is a great walk through which explains it in details.

Solution 5

Do you have the headers and footers created yet?

If so, what format are they in?

If you are starting from scratch, I would recommend using Master Pages. Creating a Site-Wide Layout Using Master Pages (C#) gets you started with using Master pages quickly.

Share:
14,424
DontVoteMeDown
Author by

DontVoteMeDown

Brazillian web developer. Tecnologies of interest: javascriptnodejskendo-uiasp.net-mvcphphtml5

Updated on June 04, 2022

Comments

  • DontVoteMeDown
    DontVoteMeDown almost 2 years

    I'm new with ASP.NET, and I want to use a simple technique that I used to do with PHP on a web site. That is, include files to render headers and footes. For example,

    <?php
        include "header.php";
        echo "Hello, world!";
        include "footer.php";
    ?>
    

    What's the right way to do something like that?

  • DontVoteMeDown
    DontVoteMeDown almost 12 years
    It's on Webforms and UserControls was my guess indeed.