To manage the page title on my pages, I have a master page with a ContentPlaceHolder in the header.
<head runat="server">
<asp:ContentPlaceHolder runat="server" ID="headContent">
</asp:ContentPlaceHolder>
</head>
On each of my pages, I add the meta tags and page title as follow:
<asp:content id="Header" contentplaceholderid="headContent" runat="server">
<meta name="keywords" content="keyword1, keyword2" />
<meta name="description" content="page description" />
<%Page.Title = "My page title";%>
</asp:content>
I cannot modify the code on the pages by placing the Page.Title in the OnInit method of the page.
I need access to the page title in the codebehind of the master page, but I always get an empty title when I use Page.Title.
By using <%Page.Title = "My page title";%> you implicitly tell the ASP.NET to execute this embedded code block during the page's render phase.
What does it mean? You won't be able to get this value before the page's render phase. Assuming you're trying to get this value a little bit earlier than during the render. That's why you get the empty string.
The workaround could be in setting your Title property of the <%# Page directive in the beginning of your page e.g.:
<%# Page Title="My Title Goes Here" Language="C#" ... %>
By setting this you'll be able to access the Page.Title property from your master page a little bit earlier than the page's rendering occurs.
Just use
<title>My page title</title>
Related
I have created a master page, which has the following css:
body { background-image:url(images/back.jpg); }
This image is the one I want to appear on my Index page. When a user clicks the "Next" button, I want to change the image to be a different one.
How can I do this?
you can over-ride the css of master page with new css on other page. The back-ground image for body loaded first will come on back ground the second one will be ignored.
You can use masterpage placeholders to the header
<head>
<link rel="stylesheet" type="text/css" href="master.css">
<asp:contentplaceholder id="Header" runat="server" />
</head>
Then add this code to your NewPage.aspx
<% # Page Language="VB" MasterPageFile="~/Master.master" Title="Content Page 1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Header" Runat="Server">
<link rel="stylesheet" type="text/css" href="newpage.css" />
</asp:Content>
Now you can override the "CSS body" inside newpage.css
If you are loading Another page without page refresh then this will work:
HTML:
<button class="next">Next</button>
jQuery:
$(function() {
$('.next').click(function() {
$(body).css('background-image', 'url(images/back2.jpg)');
});
}):
And if navigating from one page to 2nd gets the page refreshed then
you can simply specify internal css.
Ok this an odd way of doing this, I know I should have done it on Page_Load in every pages when using Masterpage, but is there a way around this issue? I don't want to rebuild the pages if I can help it,if I can only insert a title on <asp:content></asp:content> it would be easier.
If anyone has run into this or maybe have a suggestion of a good way to do it , I know jquery can do it document.title ='' but I heard it's not SEO friendly.
Thanks!
You can still set the title in each page that's using a MasterPage. In markup: -
<%# Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... etc
Or in code: -
protected override void OnLoad(EventArgs e)
{
Page.Title = "Your Title";
base.OnLoad(e);
}
you can place a contentplaceholer in your master page as..
<asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder>
After that In content page add its reference as..
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<title> Your title goes here. </title>
</asp:Content>
more simple solution in Master page
<%: Page.Title %> - the main title goes here
in content page first line of it
<%# Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... etc
I know this is an older question but what definitely works for me is to set a Literal control in the title tag of the MasterPage like this:
<title><asp:Literal runat="server" id="pagetitle" Text="MyTitle"></asp:Literal></title>
Then, in the content page, put this in the Page_Load method:
Literal myTitleLiteral = (Literal)Master.FindControl("pagetitle");
if (myTitleLiteral != null)
{
myTitleLiteral.Text = "Test Title";
}
My ASP.NET page was not showing the controls. It just shows that master page error
The page has one or more <asp:content> controls that do not correspond
with <asp:ContentPlaceHolder controls in the Master Page
Try setting
<title></title>
instead of
<title />
Remove comment (<%-- --%>) in the Master file, this worked for me.
All your Content controls have to reference existing ContentPlaceHolders on master page
For instance, if you have on your page
<asp:Content ContentPlaceHolderID="Header" ID="Title" runat="server">
You must have something like this on your master page
<asp:ContentPlaceHolder ID="Header" runat="server" />
ContentPlaceHolderID is the property that must match against any ContentPlaceHolder ID on master page.
The problem is that you must have a syntax error in the master.page's markup :
Ex) %²> in stead of %>
If you validate your markup, I'm sure thar this will solve your problem
PI. I has the same error & it works for me ;)
I have a master page which containing few CSS links and java scripts.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script src="Scripts/slides.min.jquery.js" type="text/javascript"></script>
<asp:ContentPlaceHolder id="head" runat="server">
<script language="javascript" type="text/javascript">
$(document).ready(
function () {
$("#pikame").PikaChoose();
});
</script>
</asp:ContentPlaceHolder>
`
I want to use this JavaScript and css style files ,in child ASP form page which has inherited from above master page.
<%# Page Title="" Language="C#" asterPageFile="~/MasterPage.master"AutoEventWireup="true" CodeFile="LeadInformation.aspx.cs" Inherits="LeadInformation" %>
<%# MasterType TypeName="MasterPage" %>
Seems this way not working. Please anyone help me to use masterpage CSS and JavaScript in child-page to me. Tx in advanced.
Put your script outside the ContentPlaceHolder tag. Just put it in the head section normally.
The ContentPlaceHolder tag should be empty in the Master Page - its contents will be replaced by the asp:Content tags in the Pages.
You can check if it's being rendered properly by browsing to the page and right-click -> View Source.
<asp:ContentPlaceHolder id="head" runat="server">
is used to provide script,css that are specific to only that child page
so if you want the scripts/css to be applied to the child pages just place it normally in the master page head section and outside head ContentPlaceHolder in master page..
Hope this helps....
First, child pages are not inherited from master pages. It's the same page.
Second, you don't need MasterType directive to access javascript.
So just consider masterpage and child page to be one as it will be one page when it renders to Browser and javascript runs on browser. So you can place your javascript/CSS anywhere in the head section of master page and access it from any of the child page in a same manner as they are on the same page.
I know this is a very common problem. I was trying to get a "enter key" event to invoke a click on a certain button on the page through javascript. However, I can't for the life of me fetch a single element by its id or name from my asp.net page.
It is my understanding that this issue is related to where the javascript is located and whether the elements have been rendered by the time the javascript is loaded or something of that nature?
Every time I attempt to use var x = document.getElementById('btn_AddAdmin') I end up with a null value.
My asp.net page has SiteMaster page that it inherits from. I have included a trimmed down version of my asp.net page and the SiteMaster page.
ASP.NET Page:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="HERMES.Admin" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<input type="text" id="txt_AddAdmin" runat="server" onkeypress="Populate()" />
Master Page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs"
Inherits="HERMES.SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="./Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
</form>
</body>
Sorry for the formatting, I seem to be doing something wrong...
I have attempted placing the javascript in many different locations -- anywhere from the sitemaster page to the top and bottom of my asp.net page. No luck either way.
var x = document.getElementById('<%= btn_AddAdmin.ClientID %>');
If your button is a Server Control it prepends a naming scheme to the input button so it can be bound back to the appropriate server control during postback. Thus the need to use the ClientID property to retrieve the "rendered id".
To learn more visit:
http://www.asp.net/web-forms/overview/client-script,-jquery,-and-ajax
http://www.asp.net/
Google it with Bing:
clientid
aspnet webforms client side programming