master page implementation in asp.net - 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

Related

Control directive is not allowed in .aspx file

I am trying to follow the Microsoft instructions on how to create a user control
my aim is to convert the .aspx file
<%# Page Language="C#" Title="Customize the queue UI" %>
<%# Register TagPrefix="CuteWebUI" Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="Form1" runat="server">
<CuteWebUI:Uploader ID="uploader1" runat="server">
</CuteWebUI:Uploader>
</form>
</body>
</html>
I tried the following
<%# Control CodeBehind="kgfileUploader.aspx.cs" Inherits="SampleUserControl" Language="C#" %>
<%# Register TagPrefix="CuteWebUI" Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader, Version=3.0.0.0, Culture=neutral, PublicKeyToken=bc00d4b0e43ec38d" %>
<h3> <u>User Control</u> </h3>
<CuteWebUI:Uploader ID="uploader1" runat="server">
</CuteWebUI:Uploader>
But I get an error "Control directive is not allowed in .aspx file"
Where have I gone wrong?

Do not show image when use User Control In Asp.net

I have a user control in my project (asp.net) that have an image. It has below code:
<%# Control Language="C#" ClassName="Header" %>
<asp:Panel ID="Panel1" runat="server">
<img alt="The Night Owl"
src="../Images/bookshelf.jpg"
width="800" height="110"/>
</asp:Panel>
<asp:Panel id="menuPanel" runat="server">
Home |
Titles |
Authors |
Publishers
</asp:Panel>
I can see image in user control but when i use it in my default.aspx page, it do not show image. my default.aspx code is:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register src="~/controls/Header.ascx" tagname="Header" tagprefix="uc1" %>
<!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>The Night Owl</title>
<link href="styles.css" rel="Stylesheet" type="text/css"/>
</head>
<body>
<form id="form1" runat="server">
<uc1:Header ID="Header1" runat="server" />
<h1><span lang="en-us">Home Page</span></h1>
<asp:Panel runat="server">
Welcome to The Night Owl, where all of
your technical needs are met!
</asp:Panel>
</form>
</body>
</html>
Because of src="../Images/bookshelf.jpg" in the default.aspx page it can not be load, user controls dont reference images well unless you specify the whole path or the page and user control are in the same folder,
Your UC is in a sub folder but Default page not,
Change the src something like ~/subf/subf1/img.jpg.
If that page and UC are in a same folder your src will work.
But if they are not in the same folder you should change src as I mentioned.
There is something about Path.

Creating a sub-title with a Master Page and ContentPlaceHolder

I have a site with a few pages, and I'd like the titles of the pages to be:
Foo - 1st Page
Foo - 2nd Page
Foo - 3rd Page
I've created a Master Page with the following code:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Foo.master.cs" Inherits="Foo" %>
<!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>Foo - <asp:ContentPlaceHolder ID="SubTitle" runat="server"></asp:ContentPlaceHolder></title>
</head>
<body>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
And then each page looks like this:
<%# Page Language="C#" MasterPageFile="~/Foo.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="SubTitle" runat="server">1st Page</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
Page 2
</asp:Content>
When I load the page in the browser I expect the title to be Foo - 1st Page, but it's just 1st Page.
The html source is :
<!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><title>
1st Page
</title></head>
<body>
Page 2
</body>
</html>
What am I doing wrong?
You do not want to remove runat="server" from head; it'll create other problems.
Use the following method which is a default method in ASP.Net Web Forms Application in Visual Studio 2012.
<!-- Master Page -->
<head runat="server">
<title>Foo - <%: Page.Title %></title>
</head>
<!-- Content Page/xxx.aspx -->
<%# Page Title="Home Page" ...>
Turns out this question had already been asked: ASP.NET: Having common Page title in master page with each page adding page-specific title?
After reading the answers there I found that removing runat="server" from the head element in the Master Page did the trick.

how to setup masterpage programmatically?

the function
private void SetUpMasterPage(){
this.MasterPageFile = "~/MasterPages/NestedMasterPageTest2.Master";
}
is called on the OnPreInit... This works when the masterpagefile is the base masterpage... But how are we going to make it work for the nested masterpage?
we actually tried
this.Master.MasterPageFile = "~/MasterPages/Base.Master";
but it throws an exception... =(
So, to make things clearer, on the aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test._Default"%>
<asp:Content ID="testContent" ContentPlaceHolderID="body" runat="server">
This is a test!
</asp:Content>
on the base.Master
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Base.master.cs" Inherits="Test.Base" %>
<!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>The title</title>
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="body" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
and on the default.aspx.cs
protected override void OnPreInit(EventArgs e){
this.MasterPageFile = "~/MasterPages/Base.Master";
}
it is working...
but when I use a nested master page
<%# Master Language="C#" MasterPageFile="~/MasterPages/Base.Master" AutoEventWireup="true" CodeBehind="NestedMasterPageTest2.master.cs" Inherits="Test.MasterPages.NestedMasterPageTest2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="body" runat="server">
</asp:Content>
then changed the default.aspx.cs
protected override void OnPreInit(EventArgs e){
this.MasterPageFile ="~/MasterPages/NestedMasterPageTest2.Master";
}
and I changed the default.aspx
<asp:Content ID="testContent" ContentPlaceHolderID="Content2" runat="server">
This is a test which uses a nested master page!
</asp:Content>
it returns an error that says Cannot find ContentPlaceHolder 'Content2' in the master page '/MasterPages/NestedMasterPageTest2.Master', verify content control's ContentPlaceHolderID attribute in the content page. But Content2 is on NestedMasterPageTest2. What really must have happened here?
Your nested master page has Content control but no ContentPlaceHolder control. Try adding it:
<asp:ContentPlaceHolder ID="cplh" runat="server">
</asp:ContentPlaceHolder>
Then, in Default.aspx:
<asp:Content ID="cnt1" ContentPlaceHolderID="cplh" runat="server">
This is a test which uses a nested master page!
</asp:Content>
You need to add an <asp:ContentPlaceholder> tag to your nested master page:
<asp:Content ID="basebody" ContentPlaceHolderID="body" runat="server">
<asp:ContentPlaceHolder ID="Content2" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>

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