Asp.net Masterpage not working - asp.net

<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!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></title>
<link rel="stylesheet" type="text/css" href="Home.css" />
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
<div id="banner" style="font-family: Calibri, Serif; color: #FFFFFF">
blah blah
</div>
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Home.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home"
MasterPageFile="~/MasterPage.master" Title="Welcome to StuartStudios!"%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
</asp:Content>
Does Home.aspx not print whatever I define in the ContentPlaceHolder1 ? At the minute it prints nothing out. :S

When you specify markup in a ContentPlaceHolder inside of a MasterPage, that markup will only be rendered if the pages that use your master pages don't use the ContentPlaceHolder. So in Home.aspx, if you get rid of the Contact1 element, you should see your "banner" div.
So if you want your banner div to appear on ALL pages, you should move it to outside the ContentPlaceHolder control.
On your individual pages that use your master page, the Content tag should contain the stuff that's unique to the specific page.

No... ContentPlaceholder1 should be left blank in the masterpage.

I am no expert in ASP.Net, but as far as I understand the way <asp:ContentPlaceHolder /> works, when you add a <asp:Content> tag in your page file, you override the content of the ContentPlaceHolder in the master page. The contents of which will only be displayed if you do not have a corresponding Content tag.

Two things I see:
Your div in your master page has color set to white. So unless I'm mistaken, you're printing white on white.
Your Content item in Home.aspx IS empty, so why would it print anything else?

Related

Separate asp.net pages into parts

Hi can I break the aspx into parts? E.g., one page for head, one page for body, and one for footer, and then combine them? Like that I can hide my JavaScript. Please advise. I can do so in python.
use ASP.NET master pages:
ASP.NET Master Pages
these are designed exactly for what you need/asked
for example you define content place holders in the master page like this:
<%# Master Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server" >
<title>Master page title</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td><asp:contentplaceholder id="Main" runat="server" /></td>
<td><asp:contentplaceholder id="Footer" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>
and you populate from another page based on that master page the Main and Footer contents like this:
<% # Page Language="C#" MasterPageFile="~/Master.master" Title="Content Page 1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
Main content.
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Footer" Runat="Server" >
Footer content.
</asp:content>
You can do this with MasterPages and ContentPages
Have you tried to use a master page?
http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx
Also, if you are just trying to keep your javascript separate, you could put all of the javascript into a .js page and include that into you master.

Accessing Grandparent Content Placeholder in Master Pages

I've been searching the web and not finding any answers (there were a couple close questions on stack overflow but they didn't seem to get answered or be identical), so I thought I'd pose one of my own. It revolves around nested master pages and a content page accessing the Content PlaceHolder of the grandparent master even if it is not re-exposed in the parent nested master. I'm wondering if this is not possible.
Core Site.Master
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server">
<%= Html.GlobalModel().PageTitle %>
</asp:ContentPlaceHolder>
</title>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
<link rel="shortcut icon"
href="<%= ViewContext.ClientContent( "Content/Tiki.ico" ) %>"
type="image/x-icon"/>
</asp:ContentPlaceHolder>
</head>
<body>
<asp:ContentPlaceHolder ID="SiteContent" runat="server"/>
</body>
</html>
Nested Site.Master (notice how TitleContent and HeadContent weren't customized, so the 'default' content from Core Site.Master should take affect)
<%# Master Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewMasterPage" %>
<asp:Content ContentPlaceHolderID="SiteContent" runat="server">
<asp:ContentPlaceHolder ID="SiteContent" runat="server">
<h1>Nested Header</h1>
<asp:ContentPlaceHolder ID="NestedContent" runat="server"/>
</asp:ContentPlaceHolder>
</asp:ContentPlaceHolder>
ContentView.aspx (referencing Nested Site.Master, the attempted TitleContent replacement will not work.)
<%# Page Language="C#" MasterPageFile="Site.Master" %>
<asp:Content ContentPlaceHolderID="NestedContent" runat="server">
<p>Nested content. This will work.</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="TitleContent" runat="server">
Nested Title. This will **not** work.
</asp:Content>
ContentPlaceHolderIDs can only reference their immediate parent when listed declaratively.
The easiest fix, though not the most elegant, would be to copy the ContentPlaceHolders to Nested Site.Master with the same default code. Requires some code duplication, but gets the job done.
<%# Master Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewMasterPage" %>
<asp:Content ContentPlaceHolderID="TitleContent" runat="server">
<asp:ContentPlaceHolder ID="NestedTitleContent" runat="server">
<%= Html.GlobalModel().PageTitle %>
</asp:ContentPlaceHolder>
</asp:ContentPlaceHolder>
<asp:Content ContentPlaceHolderID="SiteContent" runat="server">
<asp:ContentPlaceHolder ID="SiteContent" runat="server">
<h1>Nested Header</h1>
<asp:ContentPlaceHolder ID="NestedContent" runat="server"/>
</asp:ContentPlaceHolder>
</asp:ContentPlaceHolder>
If you don't want to do that, you could replace the placeholders with custom controls that know what to show when.
Or if you need to keep it this way, you could run a bunch of code to force early rendering to an in-memory string/buffer and replace child controls with it — but that would be a ton of hassle, and it's doubtful if it would be worth the effort.
But any of those solutions depends on your situation. If you provided more context, we could provide more specific advise.

Events only firing on specific machines but not on others

I have been trying to solve this problem for several weeks now and it is getting really frustrating. Basically i have a simple project which includes one master page and one content page. the following code is what is found in the content page
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Button ID="Button1" runat="server" Text="ButtonContent"
onclick="Button1_Click" />
</asp:Content>
The button is bound to a simple event. A break point is set to detect whether the event fired as expected. The event does not fire when a form is placed above the content place holder.
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!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></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<form></form>
</div>
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
<div>
</div>
</form>
</body>
</html>
however the event is fired when the form is removed or place below the content place holder. On the other hand, on some machines i have found that it does not matter if a form is placed before the content place holder. I have tried this project on 4 machines (three windows 7 and one xp) it has worked regardless of form placement on two machines and has been subject to form placement on the other two.
Could some one please point me to some setting which could solve this problem or offer any advice to solve my problem. I need a form before the content place holder so removing is out of the question. It would also make sense that some sort of setting is preventing the project from working correctly on another machine
per HTML spec, you can not have form inside form. If I remember correctly some browsers break when you have this kind of structure. I think IE works fine.
If you need another form, put it outside of the <form id="form1" runat="server">
- above it, specifically. That is valid.

Is there a simple way to have multiple ContentPlaceHolder controls update independently of each other?

This is a very simple ASP.NET master page example. The master page displays 4 hyperlinks and has two ContentPlaceholder controls. The first two links are to content pages that will display in ContentPlaceHolder1, the second two links are to content pages that will display in ContentPlaceHolder2.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="ProofOfConcept.Site1" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
Demo site
</h1>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/HeadContent1.aspx">Head Content 1 in ContentPlaceHolder1</asp:HyperLink>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/HeadContent2.aspx">Head Content 2 in ContentPlaceHolder1</asp:HyperLink>
<asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/MainContent1.aspx">Main Content 1 in ContentPlaceHolder2</asp:HyperLink>
<asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/MainContent2.aspx">Main Content 2 in ContentPlaceHolder2</asp:HyperLink>
<br />
<div style="border: 1px dotted blue;">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
This is default text for ContentPlaceholder1
</asp:ContentPlaceHolder>
</div>
<br />
<div style="border: 1px dotted red;">
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
This is default text for ContentPlaceholder2
</asp:ContentPlaceHolder>
</div>
</div>
</form>
</body>
</html>
The four content pages themselves all look like this:
<%# Page Title="" Language="C#" MasterPageFile="~/Site1.master" AutoEventWireup="true" CodeBehind="MainContent1.aspx.cs" Inherits="ProofOfConcept.MainContent1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceholder2" runat="server">
This is text from MainContent1 in ContentPlaceholder2
</asp:Content>
...with the ContentPlaceholder ID setas appropriate. In other words, each content page only contains one Content control linked to the one of the ContentPlaceHolders on the Master page.
If I build the site and load HeadContent1.aspx (the first link), for instance, only the content from HeadContent1 is displayed (plus markup from the master page, obviously). If I click the third link, content in the second ContentPlaceHolder is displayed, but the first Placeholder reverts to its default markup.
This behaviour all appears to be as designed and is undoubtedly very useful in many scenarios, but what I'd like to do is have the two ContentPlaceholders refresh independently of each other. To work like old-fashioned HTML frames, more or less? Is this possible, or should I be using some other control (or a different setup instead of Master/Content)?
I would say you want to use iframes for your situation. Master pages don't function the way you are looking for.
I agree with Mike. You are going the long way around. In order for your example to work with Master Pages, you will need 16 different content pages. Each page would need to fill the content for PlaceHolder1 and PlaceHolder2 depending on which links were clicked and in what order.
An IFrame would be a better way to handle something like this, but I would first recommend you review your design to determine if there is a way that it can be simplified.

ASP.NET MasterPageFile causing error

I'm trying to use a master page in my website. I created a page and then created the master. I then added the tag MasterPageFile="~/master". I'm guessing that I need to do something to my main page, because I've now started getting the error:
The page contains markup that is not valid when attached to a master page
My main page is as follows:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/my.master" %>
<!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">
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
I tried deleting the tag thinking that this might be the issue. Can anyone point me in the right direction?
You need to change your body to content sections. The master page works with contentplaceholders: your content pages need to provide the content for these placeholders. See the msdn page on this topic.
Quoted from that link above, your master page could contain the following:
<td><asp:contentplaceholder id="Main" runat="server" /></td>
Which the content page would fill by supplying the following
<% # Page Language="C#" MasterPageFile="~/Master.master" Title="Content Page 1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
Main content.
</asp:Content>
Note that I included the main declaration at the top of the content page.
the pages inhering master page should have a <asp:Content as their root. this means no html tag no doctype etc.

Resources