Filling in parent master page from child - asp.net

I'd like to have a page that uses a child master page, fill in a content placeholder of the parent, but I cannot get it to work. Whenever I try I get the error "Cannot find ContentPlaceHolder 'customHead' in the master page '/templates/info.master', verify content control's ContentPlaceHolderID attribute in the content page."
I have a master page (/templates/main.master) defined like this:
<%# Master Language="C#" %>
<head runat="server">
<title>foo</title>
<asp:contentplaceholder runat="server" id="customHead" />
</head>
<body>
<div id="content">
<asp:contentplaceholder runat="server" id="masterContent" />
</div>
Then I have a child master (/templates/info.master) defined like this:
<%# Master Language="C#" MasterPageFile="/templates/main.master" %>
<asp:content id="homeContent" contentPlaceHolderId="masterContent" Runat="server">
<div id="info-container">
<div id="info-content">
<asp:contentplaceholder runat="server" id="infoContent"/>
</div>
</div>
</asp:content>
And finally my page defined like this:
<%# Page Language="C#" MasterPageFile="/templates/info.master" %>
<asp:Content ID="head" ContentPlaceHolderID="customHead" runat="server">
<!-- Custom header area -->
<link rel="stylesheet" type="text/css" href="foo.css"/>
</asp:Content>
<asp:Content ID="content" ContentPlaceHolderID="childContent" runat="server">
This is my child content
</asp:Content>

You didn't define a "customeHead" in your child master page. If you want to expose the root master pages content area, you'll need to expose it in the child master page.
<%# Master Language="C#" MasterPageFile="/templates/main.master" %>
<asp:contentplaceholder runat="server" id="customHead" />
<asp:content id="homeContent" contentPlaceHolderId="masterContent" Runat="server">
<div id="info-container">
<div id="info-content">
<asp:contentplaceholder runat="server" id="infoContent"/>
</div>
</div>
</asp:content>

Are you setting it using this.Page.Master ?

Related

MVC Child Master page with Razor

I have an old MVC2 project that I gradulately convert to MVC 5, with Razor.
As the razor views with ASP Master Pages is not possible, I decided to duplicate the Site.Master into _Layout.cshtml.
Everything is OK But there is a problem, however.
I have an other Master page, TwoColumnsLayout.Master, a kind of "child" master page, that begins like this:
<%# Master MasterPageFile="~/Views/Shared/Site.Master" Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<asp:Content ContentPlaceHolderID="MetaContent" runat="server">
<asp:ContentPlaceHolder ID="MetaContent" runat="server" />
</asp:Content>
<asp:Content ContentPlaceHolderID="HeadContent" runat="server">
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<div class="breadcrumbs">
...
How is possible to convert and use this one with the Razor engine?
utilize #{ Layout = ""; } feature.
For Example:
~/Views/Shared/_LayoutMain.cshtml:
<html>
<head>
<link href="..." rel="stylesheet" type="text/css" />
</head>
<body>
<!-- header markup -->
#RenderBody()
<!-- footer markup -->
</body>
</html>
~/Views/Shared/_LayoutChild.cshtml:
#{
Layout = "~/Views/Shared/_LayoutMain.cshtml";
}
<!-- Child Layout content -->
<div>
#RenderBody()
</div>
~/Views/MyPage/_SomeViewPage.cshtml:
#{
Layout = "~/Views/Shared/_LayoutChild.cshtml";
}
<div>
#RenderBody()
</div>
You just need to use nested layout templates instead.
Create another layout _TwoColumnsLayout.cshtml with the following:
#{
Layout = "~/Shared/_Layout.cshtml";
}
//Nested specific markup ...
#RenderBody()
Then in your view specify your new nested template as follows:
#{
Layout = "~/Shared/_TwoColumnsLayout.cshtml.cshtml";
}

ASP.NET ContentPage with Form Controls

I created a content page that references a master page. In the content page I have a form and some form controls with a submit button. When I hit submit though the parameters are not detected on page load in the content page. On top of that the name of the parameters are all messed up.
What is the correct way of adding form controls to the content page and then using Request.QueryString[ID]?
I just realized that Microsoft throws all kinds of extra crap at you when you use master pages. It is absolutely ridiculous, is there a way for me not to go down this route of using all kinds of silly casting and inefficient overhead:
http://www.asp.net/web-forms/tutorials/master-pages/control-id-naming-in-content-pages-cs
My code (MASTER PAGE):
<%# Master Language="C#" AutoEventWireup="true" ClientIDMode="Static" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="Head" runat="server"></asp:ContentPlaceHolder>
</head>
<body>
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</body>
</html>
My code (Content Page):
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<label for="iSiteId">Site Id(s)</label>
<asp:TextBox ID="iSiteId" runat="server"></asp:TextBox>
<label for="iVersion">Version(s)</label>
<asp:TextBox ID="iVersion" runat="server"></asp:TextBox>
</asp:Content>
My Code (Content Page but the Code behind)
_siteId = Request.QueryString["iSiteId"];
_categoryId = Request.QueryString["iCategoryId"];
_version = Request.QueryString["iVersion"];
Try these instead:
siteId = iSiteId.Text;
_categoryId = iCategoryId.Text;
_version = iVersion.Text;

Error on adding <script> to master page

I have a Master page that have these ContentPlaceHolder:
<asp:ContentPlaceHolder ID="title" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="contentTitle" runat="server" ></asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="content" runat="server">
</asp:ContentPlaceHolder>
when I add these codes for jquery in<head> part of master page:
<script type="text/javascript" src="script/jquery-2.0.1.min.js" />
<link href="script/jquery.contextMenu.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="script/jquery.contextMenu.js" />
and create new aspx file that use that master page ;
<%# Page Title="" Language="C#" MasterPageFile="~/Default.Master" AutoEventWireup="true" CodeBehind="Bed.aspx.cs" Inherits="Zhotel.Lists.Bed" %>
<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="contentTitle" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="content" runat="server">
</asp:Content>
when I want to view Design part of file it show me this error:
and when I remove script tags then no more this error occurs.
How can I use script tag that this error not shown?
Add </script> to end of jquery definition, like this:
<script src="script/jquery-2.0.1.min.js" type="text/javascript"></script>
<script src="script/jquery.contextMenu.js" type="text/javascript"></script>
place your scripts outside of contentplaceholder 'title' in head section and make sure that your page has correct reference to your masterpage and also make sure that you are placing the contentplaceholders in their appropriate places

master page implementation in asp.net

i am using vb-2008 to create my application. i created master page in asp but i am not able to use it on other pages. i used :
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" MasterPageFile="~/Mail.Master" Inherits="webform1._Default" %>
i created master page as:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Mail.master.cs" Inherits="master1.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
<asp:Image ID="imghead" runat="server" ImageUrl="~/images/images1.jpeg" />
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
but this is not showing master page on other page where it is implemented..
how can i implement the master page..
Now you need to create ASPX pages that have the masterpage assigned and fill up the content placeholders
your new page called, for example, default.aspx will contain:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" MasterPageFile="~/Mail.Master" Inherits="webform1._Default" %>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
<!-- Add code here to add to the HeadContent section -->
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
<!-- Add code here to add to the MainContent section -->
<asp:Image ID="imghead" runat="server" ImageUrl="~/images/images1.jpeg" />
</asp:ContentPlaceHolder>
A MasterPage only holds the PlaceHolders for where other pages will inject content.
There is a hole Video on MasterPages that you can see here:
ASP.NET WebForms Part 5: MasterPages

Why do my ASP.NET Master pages dont compile in content pages?

I'm writing my first ASP.NET web application containing Master Pages. However, even though I seem to use them by the book, when running the project the Master Pages don't seem to work.
I have created a Default.MasterPage lite this:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Default.master.cs" Inherits="TimeTracker.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>My new pager</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<asp:ContentPlaceHolder ID="PageHeader" runat="server">
</asp:ContentPlaceHolder>
</tr>
<tr>
<asp:ContentPlaceHolder ID="Navigation" runat="server">
</asp:ContentPlaceHolder>
</tr>
<tr>
<asp:ContentPlaceHolder ID="Main" runat="server">
</asp:ContentPlaceHolder>
</tr>
<tr>
<asp:ContentPlaceHolder ID="Footer" runat="server">
</asp:ContentPlaceHolder>
</tr>
</table>
</div>
</form>
</body>
</html>
And two content pages like this:
(PageHeader.aspx)
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="PageHeader.aspx.cs" Inherits="TimeTracker.PageHeader" MasterPageFile="~/Default.Master" Title="Header"%>
<asp:Content ID="Header" ContentPlaceHolderID="PageHeader" runat="server">
Enalog Time-Tracker
</asp:Content>
And (Default.aspx)
<%# Page Language="C#" MasterPageFile="~/Default.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TimeTracker._Default" Title="Login"%>
<asp:Content ID="LoginPage" ContentPlaceHolderID="Main" runat="server">
Login page
</asp:Content>
But when running the project, the MasterPage don't compile in the content pages, even though I believe I've wired them up correctly. So if I for example runs Default.aspx, I only see is the content from the ContentPlaceHolderID="Main", and if I run PageHeader.aspx, I only see the content from the ContentPlaceHolderID="PageHeader".
Do anyone know why I get this behaviour, or perhaps what I'm doing wrong here?
Thanks in advance!
You need to put both sections in your content pages, like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="PageHeader.aspx.cs"
Inherits="TimeTracker._Default" MasterPageFile="~/Default.Master"
Title="Login"%>
<asp:Content ID="HeaderContent" ContentPlaceHolderID="PageHeader" runat="server">
This is my header
</asp:Content>
<asp:Content ID="MainContent" ContentPlaceHolderID="Main" runat="server">
My main page content is here.
</asp:Content>
You probably don't need the "Header" page.
Your master page does not contain any <td> tags, so the tables do not have any displayable content.

Resources