DontVoteMeDown
DontVoteMeDown

Reputation: 21465

Including headers and footers from another files in ASP.NET

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?

Upvotes: 4

Views: 15636

Answers (5)

Idrees Khan
Idrees Khan

Reputation: 7752

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

Upvotes: 2

GVashist
GVashist

Reputation: 427

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.

Upvotes: 1

DotNetFreak
DotNetFreak

Reputation: 176

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.

Upvotes: 1

Jeremy Wiggins
Jeremy Wiggins

Reputation: 7299

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.

Upvotes: 8

Chilly
Chilly

Reputation: 41

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>  

Upvotes: 4

Related Questions