ASP.NET or MVC - How to Referece master or layout page's content

4/3/2014·5 min read

In ASP.NET and ASP.NET MVC there're cases when you want to reference master(Layout) page's content, there're few ways to doing that, this's article try to list all of them for referenece.

  • access Master Page Content
    • add to ContentPlaceHolder control(from your C# Server side)
    • another way to add to ContentPlaceHolder control (C# server side)
    • add value to master page from ASPX's design page
  • access Layout Page content (ASP.NET MVC)
    • add value to Layout Page (from client html side)
    • add value to layout Page (from C# Server side)

1. access Master Page Content

1) add to ContentPlaceHolder control(from your C# Server side)

add Javascript path or Css path

it's not only for javascript path, you could add anything to your master page.

aspx page example

 

Example code to add to your child page
  public partial class About : System.Web.UI.Page
     {
         protected void Page_Load(object sender, EventArgs e)
         {
 
             StringBuilder sb = new StringBuilder();
             sb.Append("<script type=\\"text/javascript\\" src=\\"Scripts/jquery-1.4.1.min.js\\"></script>");
 
             ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("scriptPath");
             if (cph != null)
             {
                 Literal literal = (Literal)cph.FindControl("scriptText");
                 literal.Text = sb.ToString();
             }
 
 
             ContentPlaceHolder cph2 = (ContentPlaceHolder)Master.FindControl("HeadContent");
             if (cph2 != null)
             {
                 Literal literal = new Literal();
                 literal.Text = "<link href=\\"~/Styles/Test.css\\" rel=\\"stylesheet\\" type=\\"text/css\\" />";
                 cph2.Controls.Add(literal);
 
             }
         }
     }
 

2) another way to add to ContentPlaceHolder control (C# server side)

example of using ClientScriptManager

This's good way to add script too, but if you want to add to specific place on you html, it's better to using the first one ContentPlaceHolder

           String name = "ButtonClickScript";
             String jspath = "~/script_include.js";
             Type type = this.GetType();
 
             ClientScriptManager csm = Page.ClientScript;
 
             if (!csm.IsClientScriptIncludeRegistered(type, name))
             {
                 csm.RegisterClientScriptInclude(type, name, ResolveClientUrl(jspath));
             }
 

3) add value to master page from ASPX's design page

add to your child page's Master Content.

following is add SiteTest2.css to master page from its child page.

  <% @ Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
     CodeBehind="About.aspx.cs" Inherits="AccessMasterLayoutValue.About"  %>
 
 <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
 
     <link href="~/Styles/SiteTest2.css" rel="stylesheet" type="text/css" />
 
 
 </asp:Content>
 <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
     <h2>
         About
     </h2>
     <p>
         Put content here.
     </p>
 </asp:Content>
 

2. access Layout Page content (ASP.NET MVC)

1) add value to Layout Page (from client html side)

if you want to add following jQuery path at the end of the body tag on ASP.NET MVC

following is exmaple to add to your layout page
 <body>
 
 
     @ RenderSection("pageFooter", false)
 </body>
 
following is example to add to your child page
  @section pageFooter
 {
     <script src=" @ Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
  }
 

2) add value to layout Page (from C# Server side)

There're few ways you could add value to you C# server side, here's provide one way to use ViewBag.

So, following're steps to do that.

add code to your controller

using dynamic, which won't cause run time error. so set the CssFile value at your action, you could add to your baseController, which if you have some value, every page or controller are able to access it. the demerit to use dynamic is, not strong type. So, if using a lot, it'll become messy on your application.

    public ActionResult Index()
         {
             ViewBag.CssFile = "<link href=\\"Styles/Site2.css\\" rel=\\"stylesheet\\" type=\\"text/css\\" />";
 
             return View();
         }
 
add code to layout page
 <head>
     <meta charset="utf-8" />
     <title> @ ViewBag.Title</title>
     <link href=" @ Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
     <script src=" @ Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
     <script src=" @ Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
 
      @ Html.Raw(ViewBag.CssFile)
 </head>
 <body>
 

In this way, you could add anything you want to your parents page.