ASP.NET MasterPageFile causing error - asp.net

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.

Related

Why is an added web form different from the default forms?

An ASP.NET Webforms project has some default pages ("About" and "Contact" to be specific), which have this type of content:
<%# Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="About.aspx.cs" Inherits="About" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %>.</h2>
<h3>Your application description page.</h3>
<p>Use this area to provide additional information.</p>
</asp:Content>
Yet when you select Add > Web Form, it creates this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="DuckbilledPlatypus.aspx.cs" Inherits="DuckbilledPlatypus" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Why the difference/inconsistency? Note: This is in Visual Studio 2013; I'm not positive other versions are the same.
You didn't create the Web Form using the Master Page. When you right click the project, click Add Item, then under Web it will have Web Form and then other classes as well. Using the Web Form using Master Page should give you the one that you want. The one that it sounds like you used is the blank web form and has no site.master linked up to it where the Web Form using Master Page links up the site.master for you.

ASP.NET adding a form to Master Page

So I've never really coded a website using ASP.NET. I'm trying to add a search form to a masterpage for my site. The problem is the whole body is wrapped in a form tag, which makes the functionality of my new form non-existant. I have a habbit of making bad situations worse by messing around with things I don't yet understand. So I thought I'd ask for your advice, my thoughts were:
Remove the runat="server" form tag completely.
Close it before my form and replace it at the end of my form.
Code within the button (which I later noticed you can't open.
Give up on ASP.NET and go back to PHP lol.
Hope you can help.
Thanks.
Master page will be like this
<%# 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>
<asp:ContentPlaceHolder id="bodycontent" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
you can write the search page like this
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>
<asp:Content ID="Content1" ContentPlaceHolderID="bodycontent" Runat="Server">
//dnt include form tag here having runat = server
</asp:Content>
don't add content to the master page (you can, but don't for this example), add it to the content-page (.aspx). having the form on the master page means you don't have to manually add a form to content-pages; it's there automatically. (everything on the master page will be on the content-page.)
as masood is showing, search.aspx (the content-page) is using a master page, MasterPageFile="~/MasterPage.master". content goes in the content-tag.
I realised as I am used to hard coding in open source languages, that I was using HTML specific syntax, which was conflicting with the tag surounding the whole masterpage. In using the tools on the side I found my solution.
Thank you.
Well open Visual Studio Click File -> New -> Website -> Empty Web Site.
Create a MasterPage.master and add it to you project.
Example:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<html>
//.......
</html>
Create a SearchPage.aspx and bind it with your master page. Example:
< Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="MasterPage.aspx.cs" Inherits="MasterPage" Title="Untitled Page" %>

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.

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.

Asp.net Masterpage not working

<%# 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?

Resources