Set background picture on webpage when using a masterpage - c#

Is there away to set a background image on a webpage when the webpage is based on a masterpage. Notice its not the background of the masterpage i want to change, but the background of the page which use the masterpage.

You can just add a style rule for body{} or whatever on the actual ASPX page. It will override any style set at the master page level.
edit: example:
<%# Page Title="Example" Language="C#" MasterPageFile="~/MasterPages/Main.master"
AutoEventWireup="true" CodeBehind="TroubleShootScanning.aspx.cs"
Inherits="SpectrumTechnologies.TroubleShootingScanning" %>
<asp:Content ID="Content1" ContentPlaceHolderID="mainContent" runat="server">
<style type='text/css'>
body { background-image: url(images/bgimage.jpg); }
</style>
<!-- the rest of the page here -->
</asp:Content>
This will override any values set in a stylesheet.

You could put a content placeholder on the master page that is within the header.
Then in your content page, put a content control where you could include the second modified CSS stylesheet or STYLE block directly.

I suggest adding an <asp:ContentPlaceHolder ID="ExtraStyles" runat="server" /> tag to the header of the Masterpage. That way you can add the following to your page:
<asp:Content ID="ExtraStylesContent" ContentPLaceHolderId="ExtraStyles" runat="server">
<style type="text/css">
body {background-image:url('someotherimage.jpg');
</style>
</asp:Content>
By adding an extra ContentPlaceHolder you won't get the scattered style tags, they will end up in the head tag of the rendered html.

There's an additional trick to add to this...(Second line below)
Also helps the background show while testing locally.

Related

ASP.Net- Items inside the Content Placeholder is not showing in .aspx pages

I have an error in a content placeholder. When I try to create a master page it shows me an error as below in the master page and I can't see any items such as buttons, labels, etc. inside the .aspx pages.
Also I can't add content placeholder from the VS Toolbox, it shows a cross mark on the Content placeholder button.
{ Error Rendering Control - ContentPlaceHolder1
An unhandled exception has occurred.
This control can only be used in a MaserPage.}
ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application.
In addition to static text and controls that will appear on all pages, the master page also includes one or more ContentPlaceHolder controls. These placeholder controls define regions where replaceable content will appear. In turn, the replaceable content is defined in content pages. Here is an example of a master page
<%# 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>
Here should be a content page looking like
<% # 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>
please read more here about ASP.NET Master Pages

verify content control's ContentPlaceHolderID attribute in the content page

I am a new ASP.NET developer. I am making development using C#. I removed some default contents from that I am getting these error notification as attached in screen shot can anybody look at and give me a solution also some one inform me from where .NET pages control header and footer so I can make modification there.
Here is the complete error heading text:
"Cannot find ContentPlaceHolder 'MainContent' in the master page '/Site.Master', verify content control's ContentPlaceHolderID attribute in the content page."
Remove asp:Content ContentPlaceHolderID="MainContent" from your child pages or Add <asp:ContentPlaceHolder ID="MainContent" runat="server"> into your Master page.
There is no ContentPlaceHolder named 'MainContent' in your master page,
A content place holder should be in your Master page like this:
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
And child pages like this:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
When you have a child page that inherits from a master page ContentPlaceHolder should match between them.
So if you masterpage declare that every child should fill its Box1 Box2 and Box3containers every
child page that is linked to it (MasterPageFile="~/MasterPages/Site.master") must contain this references <asp:Content runat="server" ContentPlaceHolderID="Box1"> even if empty.
In your case you have declared a box name that Master page doesn't has
update
looking at your code I can suspect that you have linked the master page in a wrong way.
Change the attribute of your page and be sure that the path is correct
MasterPageFile="~/MasterPages/Site.master"
if it doesn't work try this
If the page is empty delete it and recreate aspx page with master page, after you select the master page the child page will be created with the right contentplaceholders.

What's the difference between <head> and <asp:Content ID="HeaderContent" ...>?

This may be a newbie question, but i'm pretty new to asp.net & C# etc.
I'm working with an ASP.net website, and I'm curious about the structure of it (after automatically creating a web project), specifically the following:
I see that in Default.aspx , I have a tag like this:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>**strong text**
But in Site.master, I have this:
<head runat="server">
*etc*
</head>
So where would I put code if I wanted to include JavaScript code to run, on page load?
I believe you can put your code in any of them. The first one is for adding code or script used by all content pages(that using this master page file) while the second one is if you want to to add script or code from content pages(that should be used only for this specific page)
//in the Master page, the content here is used by all content pages
<head runat="server">
*etc*
</head>
and
//this is specific to the content page that use it. This section needs to be supplied in content pages
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
That section needs to be supplied in each content page and it will be exclusive to that page - no other page can use the script in that section
asp:Content ID="HeaderContent" is a content region. Anything within that tag will get embedded in the associated ContentPlaceHolder in the master page when it is generated.
head is a standard html markup, indicating the page head elements. Typically, the HeadContent placeholder is inside the head tag on the master page.
The head element, container for all the head elements, must use a title for the document. Some other elements it can include: style, base, link, meta, script, noscript.
The asp: Content ID = "HeaderContent" is a content element of the master page.
Have a look at the Plugging in Content part of the following link for detailed information on this: http://odetocode.com/articles/419.aspx
I think you asked when you want to use JavaScript where you put JS in your code.You can put anywhere you wish in asp side between script block such as:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
function Onclick(){
//some codes
}
</script>
</asp:Content>
or
<head runat="server">
<script type="text/javascript">
function Onclick(){
//some codes
}
</script>
</head>
Also you can put JS outside this tag. You only should use tag.

Referencing Page.Title after it has been set as part of a asp:contentplaceholder

I've got a master page setup with a contentplaceholder control inside the title tag as so:
<head id="head1" runat="server">
<style type="text/css">
body { font-family: Tahoma; font-size: 9pt; }
</style>
<title><asp:contentplaceholder id="title" runat="server" /></title>
</head>
That contentplaceholder is implemented inside a page that uses that masterpage as so:
<asp:content runat="server" contentplaceholderid="title">
Welcome: <%= this.BasketID %>
</asp:content>
I'm trying to then get a copy of the substituted title inside the masterpage body (also tried inside the page - and this doesnt work either) like:
<p>
<strong>Subject:</strong> <%# Page.Title %>
</p>
In all cases Page.Title and Page.Header.Title are "" (I've tried both databinding and using the <%= %> syntax to no avail.
Does anyone know what is going on here and how I can overcome this?
Thanks.
The problem you're getting is because you are 'tricking' the page cycle. You'd better use this in the codebehind of the page:
Master.Title = "Welcome: " + basketId
You could do it this way however; on the masterpage: create a HtmlTextWriter, configure it to write to a MemoryStream. Render the 'title' contentplaceholder to the HtmlTextWriter, attach a StreamReader to the stream to grab the content as a string, and output this to your page. Yet, this is not efficient, and way too much work :-)

Using nested Master Pages

I'm very new to ASP.NET, help me please understand MasterPages conception more.
I have Site.master with common header data (css, meta, etc), center form (blank) and footer (copyright info, contact us link, etc).
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="_SiteMaster" %>
<!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 id="tagHead" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<form id="frmMaster" runat="server">
<div>
<asp:ContentPlaceHolder ID="holderForm" runat="server"></asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="holderFooter" runat="server">Some footer here</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
and I want to use second master page for a project into sub directory, which would contains SQL query on Page_Load for logging (it isn't necessary for whole site).
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Project.master.cs" Inherits="_ProjectMaster" MasterPageFile="~/Site.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<asp:ContentPlaceHolder ID="holderForm" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="server">
<asp:ContentPlaceHolder ID="holderFooter" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
But I have a problem: footer isn't displayed.
Where is my mistake? Am I right to use second master page as super class for logging?
Project page looks like this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/Project.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<p>Hello World!</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="Server">
Some footer content
</asp:Content>
I've been working with nested master pages and have run in to something similar. From what I see where you have "Some footer here" in the Site.Master is where the problem lies and I've had similar problems with having content with-in a contentplaceholder tag. if you try this instead
<asp:ContentPlaceHolder ID="holderFooter" runat="server"/>Some footer here
Then you should be able to see the footer content.
I'm not sure I'd use master pages for this. If it's really just going to do logging, I'd implement IHttpModule, register it in web.config, and then check whether or not to log based on the path of the request. I think of master pages as being about content rather than other processing such as logging.
See the IHttpModule walkthrough on MSDN for an example - in your BeginRequest handler, you'd probably check the request path and log appropriately if it matched.
Apologies if I misunderstood what you're trying to do though.
You should leave your ContentPlaceHolder empty, for it gets substituted by the content of the Content in your actual Page...
When you move the "Some footer here" text to your Content, you will see your lines of text :)
HTH
This link gives a simple explanation on Master pages,
http://waxtadpole.wordpress.com/2009/01/16/master-page-content-not-visible-visual-studio-2008/
The question are you right to use child Master pages in this instance - I would say master pages should be helping you solve issues around building a consistent layout, not for whether or not logging should occur.
The problem is, when the text elements placed inside Default.aspx are put in their relative Content Placeholders, they are written on the placeholders of your Site.master page and not those of Project.master (which have the same names).
You should resolve the naming conflict, by assigning different ContentPlaceHolderIDs to the the placeholders in Project.master (this means you'll also have to change the references in Default.aspx).
This would be your Project.master file:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Project.master.cs" Inherits="_ProjectMaster" MasterPageFile="~/Site.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<!-- whatever... -->
<asp:ContentPlaceHolder ID="holderFormInternal" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
<!-- ... -->
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="server">
<asp:ContentPlaceHolder ID="holderFooterInternal" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
And thus, your .aspx pages that use the Project master page instead of the global Page.master must be changed to:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/Project.master" %>
<asp:Content ContentPlaceHolderID="holderFormInternal" runat="server">
<p>Hello World!</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooterInternal" runat="Server">
</asp:Content>
If the only reason is to implement loggin why would you mess around with masterpages?
If the logging isent supposed to display any text!?
You either do as Skeet proposed with an IHTTP handler.. Or lazier one would be do have a class that derives from webpage and implement logging in that class and make your pages that need logging dervice from that..
ex:
public class LoggingPage : : System.Web.UI.Page
{
public override void OnLoad()
{
// Do logging
}
}
partial class OneOfTheWebPages : LoggingPage
{
public void onLoad()
{
base.onLoad();
}
}
I may be misunderstanding your problem - but from the code you've posted, there isn't anything in the footer.
In your Project page, the <asp:Content> tag for the holderFooter content place holder doesn't have anything in it.
I have next inheritance tree:
Site.master <-- Page1.aspx
<-- Project.master <-- Page2.aspx
And I don't know why Page2 display only content of itself and it's master page - Project. But doesn't display a content of Site (as Page1 does) Why? What have I to write for doing that?

Categories

Resources