what im trying to do is to read the current master page's title, and append something to it from the codebehind of the child page.
I tried to use:
this.Master.Page.Title.ToString()
But it returned null. Any ideas?
In the master page, here is how i set the title:
<head runat="server">
<title>The Magic Finger - Web Design</title>
<link href="App_Themes/Style.css" rel="stylesheet" type="text/css" />
</head>
Try Page.Title instead, the title is set by the content page.
Set the page title as normal in your Master Page. In your child page, use
Me.Master.Page.Title &= " - This text will be appended"
Important: make sure you do not have "Title" as an attribute in the child page's .aspx. If you have this:
<%# Page Title="" Language=""
Make sure to remove the title attribute.
Related
Every page in my ASP.NET application is based on Main masterpage, which in its turn is based on another Base masterpage (nested masterpages). I try to set different title for every page. I tried two approaches, but it does not work without any obvious reason.
1st attempt: <%# Page Title="ABC" ....... %>. I do not know why, but it does nothing. No <title> tag is generated in resulting HTML.
2nd attempt: <%# Page meta:resourcekey="PageTitle" ....... %> and PageTitle.Title to .resx. Same here. No <title> tag at all.
What did I miss?
I discovered the reason: looong time ago I removed runat="server" from my <head> tag. When I add it back - the Title directive starts to work, but it breaks other functionality, like <link rel="stylesheet" type="text/css" href="/css/<%= www.Utils.LanguageName %>/language14.css" />.
I have a master page and two content pages that uses the master page for layout and design. I have 2 css files for my master page. Now what I want to do is that when I run first content page master page uses the first css file and when I run the second content page it uses the other one. Any suggestions about how should I do this .
First off, this should not be your normal approach unless you are doing something unusual. The whole point of using a common master page is so that you can easily have a common look and feel across your website.
But you can do it a few ways. One way would be to put a placeholder in your master pages <head> section. Then create content for that placeholder in each content page that includes the appropriate css file.
You can use a ContentPlaceHolder in the master page, and inside the head to change the css differently on every next page, or just ignore it to keep some default.
Here is an example:
<head runat="server">
<asp:ContentPlaceHolder ID="styleHolder" runat="server" >
<link rel="stylesheet" type="text/css" href="default.css">
</asp:ContentPlaceHolder>
</head>
<body>
and inside on the page with the different css, just include the the PlaceHolder and change it.
It seems straightforward enough, and testing it, it actually works:
<title>Default text</title>
In the Masterpage. And:
<%# Page Title="Specific name"...
in the specific page. Or in its codebehind:
Title = "Specific name";
So why am I asking? Because searching for it I've found all sorts of more complicated methods for doing that. (Yes. In Asp.net.)
So is there any drawback to the way I wrote above?
Not sure what you've found but that's how it is normally done.
Master page has the default, with overrides from specific pages.
An alternative (and I'm not specifying it's better) is to use a Content Placeholder.
On Master Page
<title>
<asp:ContentPlaceHolder id="PageTitle" runat="server">Default Title</asp:ContentPlaceHolder>
</title>
On specific page
<asp:Content ContentPlaceHolderID="PageTitle" runat="server">Specific Title</asp:Content>
But the drawback is that it's not as easy to set the title from code-behind.
What you have to know here that the <title></title> can not change from the code behind, or from the page declaration if is NOT inside the header with runat="server" So only if you have like that :
<head runat="server">
<title>Default Title</title>
</head>
you can have it as default, and then change it on pages. If the head is not runat="server" then the code behind can not find it to change it and the default title is shown.
All the rest stands as they are, I also use the same way, a default title on the master page, that I change it from the page if I can, or if not the default title is shown.
Normally when we are using Master/Content style pages, we apply the css to Master page so every page child of the master page can use the style but I don't want this,I want I wanna apply css to content page directly instead of master page. Where should I put
<link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
that reference code ?
Thanks in advance by the way.
Normally I put a content placeholder in the head section of the master page. That way any content page can add extra css/js/etc references to the head of the page.
In your master page put the following
<head>
... title, meta tags, js and css links ...
<asp:contentPlaceholder id="head" runat="server" />
</head>
Then in your pages you can include extra elements in the head using this
<asp:content contentplaceholderid="head" runat="server">
<link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
</asp:content>
Put a content placeholder in the head portion of your master page. Not all content pages will need to place anything in it but this particular content page can place the CSS file you want to link.
You can't apply a style sheet to a content page only, it applies to the whole web page.
I have a simple page inside a master page (well, in a master in a master).
In the top master I have the head tag with runat="server", with a number of bits such as scripts, stylesheets, etc. and also a contentplaceholder. There is no title tag here.
In the page that uses this master, the content for the placeholder contains the
<title>pagename</title> bit in it. I really have to set it in there.
Unfortunately when the page is rendered I get my title which is all good, but also get a second blank title tag - I presume dumped in there by .NET.
Is there any way of stopping this second title tag coming out?
From memory, by virtue of putting the runat="server" on the <head> .Net automagically adds a <title> if there isn't one already.
I think (haven't tested it) is if in your masterpage you do
<head runat="server">
Blah
<title runat="server" visible="false"></title>
</head>
setting the Title tag explicitly in the Head of the masterpage and setting visibility to false works. I think.
You don't have to manually insert <title> to the head.
Just set Page.Title = "title" by code, or <%# Page Title="My Title" .. %> by markup.
ASP.NET will figure out the rest, and put the right title.
I think using:
If you want to set title at page level
<%# Master ... %>
<html>
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="titleContent" runat="server" />
</title>
</head>
Or,
If you want to set dynamic title at Master Page level.
<%# Master ... %>
<html>
<head runat="server">
<title>
<asp:Literal ID="litPageTitle" runat="server"></asp:Literal>
</title>
</head>
is better way to make sure that empty second title tag is not generated.