How To Create Master Page In ASP.NET

Master page provides the layout and functionality to the other pages. Creating a master page in ASP.NET is very easy. Let's start creating master page step by step.
 
Step 1: Open new project in visual studio
 
New project->Installed->Web->ASP.NET Web Application (shown in the picture),
 
 
After clicking OK button in the Window, select Empty (shown in the picture),
 


After clicking OK button, project "masterpage" opens but no file is there (shown in the picture),
 
 
Step 2: Add new file in to our project.
 
Add the master page into our project.
 
Right click Project->Add->New item (shown in the picture),
 


After clicking on new item, Window will open, select Web Form->Web Forms Master Page (shown in the picture), 
 


After clicking the add button, master page 'site1.master' adds to our project.
 
Click on site1.master into Solution Explorer (shown in the picture),
 
 
Step 3: Design the master page, using HTML.
 
HTML code of my master page is, 
  1. <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="masterpage.Site1" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>c# corner</title>  
  8.     <link href="css/my.css" rel="stylesheet" />  
  9.     <asp:ContentPlaceHolder ID="head" runat="server">  
  10.     </asp:ContentPlaceHolder>  
  11. </head>  
  12. <body>  
  13.     <!DOCTYPE html>  
  14. <html>  
  15. <head>  
  16.     <title>my layout</title>  
  17.     <link rel="stylesheet" type="text/css" href="my.css">  
  18. </head>  
  19. <body>  
  20. <header id="header">  
  21. <h1>c# corner</h1>  
  22. </header>  
  23. <nav id="nav">  
  24.     <ul>  
  25.         <li><a href="home.aspx">Home</a></li>  
  26.         <li><a href="#">About</a></li>  
  27.         <li><a href="#">Article</a></li>  
  28.         <li><a href="#">Contact</a></li>  
  29.     </ul>  
  30. </nav>  
  31. <aside id="side">  
  32.     <h1>news</h1>  
  33.     <a href="#"><p>creating html website</p></a>  
  34.     <a href="#"><p>learn css</p></a>  
  35.     <a href="#">learn c#</a>  
  36. </aside>  
  37.   
  38.   
  39.     <div id="con">  
  40.         <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">  
  41.           
  42.         </asp:ContentPlaceHolder>  
  43.     </div>  
  44.   
  45.   
  46. <footer id="footer">  
  47.     copyright @c# corner  
  48. </footer>  
  49. </body>  
  50. </html>  
  51.     <form id="form1" runat="server">  
  52.       
  53.     </form>  
  54. </body>  
  55. </html>  
CSS Code
  1. #header{  
  2.     color#247BA0;  
  3.     text-aligncenter;  
  4.     font-size20px;  
  5. }  
  6. #nav{  
  7.     background-color:#FF1654;  
  8.     padding5px;  
  9. }  
  10. ul{  
  11.   
  12.     list-style-typenone;  
  13. }  
  14. li a {  
  15.     color#F1FAEE;  
  16. font-size30px;  
  17. column-width5%;  
  18.     }  
  19.     li  
  20.    {  
  21.     displayinline;  
  22.     padding-left2px;  
  23.     column-width20px;  
  24.    }  
  25.    a{  
  26.  text-decorationnone;  
  27.  margin-left:20px  
  28.     }  
  29.    li a:hover{  
  30.     background-color#F3FFBD;  
  31.     color#FF1654;  
  32.     padding:1%;  
  33.    }  
  34.    #side{  
  35.     text-aligncenter;  
  36.     floatright;  
  37.     width15%;  
  38.     padding-bottom79%;  
  39.     background-color#F1FAEE;  
  40.    }  
  41.    #article{  
  42.     background-color#EEF5DB;  
  43.     padding10px;  
  44.     padding-bottom75%;  
  45.    }  
  46.    #footer{  
  47.     background-color#C7EFCF;  
  48.     text-align:center;  
  49.     padding-bottom5%;  
  50.     font-size20px;  
  51. }  
  52.    #con{  
  53.        border:double;  
  54.        border-color:burlywood;  
  55.    }  
Our master page is designed. Move to the next step.
 
Step 4: Add web form in to our project.
 
Right click on the project->Add->New item (shown in the picture),

  
Select Web form with the master page.
 
 
 
After clicking on that, add the button Window, open the selected masterpage->site1.master and click OK.
 
 
 
Now, design our homepage.
 
Here, we write home page only,
 
Home.aspx 
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="home.aspx.cs" Inherits="masterpage.home" %>  
  2. <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">  
  3. </asp:Content>  
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">  
  5.     <h1>Home page</h1>  
  6. </asp:Content>  
Finally, our Master page is created; build and run the project.
 
The master page looks aas shown in the picture:

Up Next
    Ebook Download
    View all
    Learn
    View all