Setting title of a page - c#

I have an ASP.NET application that uses a master page configuration. What I'd like to do is two things.
How can I programmically set the title in the child page (as in the text in the <title></title> tags? And,
If the child page does not set the title, I'd like the master page to automatically detect this and set a default title.
Any help would be appreciated.

you can have your pages inherit from a custom page
public abstract class CustomPage : Page
{
public virtual string PageTitle {get{return String.Empty;}}
}
Then, in your MasterPage's Page_Load, do ( can't remember if MasterPage.Title exists or if you'll have to do Page.Title, which will work since both objects are Page objects):
if(Page is CustomPage) {
this.Page.Title = ((CustomPage)Page).PageTitle;
} else {
this.Page.Title = "Default Title";
}
Then, when you create a Page, for instance a CustomerManager page:
public partial class CustomerManager : CustomPage
{
public override string PageTitle { get{return "Customer Manager"; }}
}
This way, your MasterPage isn't 100% tied to using CustomPage (creating normal Pages won't throw an error). And, if you use CustomPage objects, you're all set!

What I do is basically the same as Jim Schubert's. I do make one small change though, in the MasterPage's PageLoad, i would do a conditional check, something like the following:
if(Page is CustomPage) {
var cp = (CustomPage)Page;
this.Title = (String.IsNullOrEmpty(cp.PageTitle)) ?
"Master's Default Title" :
cp.PageTitle;
}
This then addresses point 2 of you question, so that your custom pages need not specify a title, but can just return String.Empty.
(Forgive if the syntax isn't exactly right, VB is my native language.)

For your first question,This is the one you are looking for http://www.devasp.net/net/articles/display/852.html and
For your second question, http://delphi.about.com/cs/adptips2004/a/bltip0304_2.htm

In the Master page markup, do this:
<head runat="server" id="hd">
<title></title>
</head>
Then, in code behind (assuming AutoEventWireup="false"):
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (String.IsNullOrEmpty(this.mhd.Title))
this.hd.Title = "Master Title";
}
Then in your page you can either set the title declaratively:
<%# Page Title="Page Title" . . . %>
or programmatically:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Title = "Page Title";
}

for your 1st question shouldnt:
Protected void Page_Load(object sender, EventArgs e)
{
Page.Title = "Title";
}
for each child page ,Do the trick?

you can add a contenttemplate at the header of the masterpage then add a contentplaceholder at the aspx pages inheriting from the masterpage, then in the pageload of the aspx you can set the title :
page.title = "my title"
to answer the second part, you can simply put the default title in the tag at the master page, so that if you did not set it programmatically then it will stay the same.

Related

Get Page Title In Master Page Code Behind

I want to get the page title in c# master page code behind.
I tried using Page.Header.Title; property but it return null.
Please help me to get the title.
Thanks in advance.
Shibin V.M
In your page head include runat="server" then you can easily get the Page title by
string Title = Page.Title;
EDIT:
Using the Title property of Page requires a header control on the page. (e.g. <head runat="server" />).
You can set the page title on Page_Load Event like this
protected void Page_Load(object sender, EventArgs e)
{ Page.Title = "Page Title"; }
and get the page title using
string title = Page.Title;
Set Title attribute of content pages <%# Page Title="Contact us" ../> and handle the Master page's Load or Init event to get Page.Title or Page.Header.Title.
try this in your master page
string Title = "Your Site: " + this.Page.Title;
Use this in your html tag, it has worked for me <%: Page.Title %>.
For eg: <li class="active"><%: Page.Title %></li>.

ASP.net get content page to change master page control

Master page:
<form runat="server">
<Scirra:MainMenu runat="server" ID="MainMenu" TopTabSelected="home" SubTabSelected="link2" />
<asp:ContentPlaceHolder id="MainContent" runat="server">
snip
Content page:
Master.MainMenu.TopTabSelected = "forum";
I know I'm probably doing this wrong, but is this possible? I want to change a parameter of that control. It says 'inaccessible due to protection level'.
You should provide a public property f.e MenuTabSelected in your MasterPage that Gets/Sets this property of your Menu.
public string MenuTabSelected {
get { return MainMenu.TopTabSelected; }
set { MainMenu.TopTabSelected = value; }
}
Then you can access it in this way:
((YourMasterPage)Master).MenuTabSelected = "forum";
where YourMasterPage is the type of your MasterPage.
The compiler error is thrown because you want to access a private or protected control from outside of your MasterPage-Class. This would only be allowed if it would be public, what is not recommended. You have more control if you do it the way i suggested :)
find menu items in content page and change its value
protected void Page_Load(object sender, EventArgs e)
{
Menu mainMenu = (Menu)Page.Master.FindControl("NavigationMenu");
MenuItem menuMaterials = mainMenu.FindItem("Materials");
if (menuMaterials.Value == "Materials")
{
menuMaterials.Value = "NO materials";
menuMaterials.Text = "No materials";
}
}

Change panel "visible" property on MasterPage from Child pages

I want to show some panel with a label, both located on a MasterPage, from inside it's child pages.. I already did the coding on the MasterPage:
public class MyMaster : MasterPage
{
public void ShowPanel(string pMessage)
{
labelInside.Text = pMessage;
myPanel.visible = true;
}
}
Then I make the calls from child pages:
public void ShowPanel(string pMessage)
{
MyMaster masterPage = this.Master as MyMaster;
masterPage.ShowPanel(pMessage);
}
This "works" ok, but it won't show nothing, since I need the page to be "refreshed" in an "ajax-way" like an UpdatePanel, which I can't use because the Trigger is in another page, right?
I really need this to work.. even if you have another completely different way to do this, I would appreciate.
You must place your panel inside an UpdatePanel(UpdateMode conditional) and in ShowPanel call its Update method.
Have you considered having the masterpage just have a placeholder for the label, but having each child page put its own content label inside that placeholder, which it would then have full control over?
you can subClass your page, and expose a property say.. MyPage.FooVisible
than in your masterPage, you can:
myPage = this.Page as MyPage
if (myPage != null) myPage.FooVisble = false;
in your page you can handle that any way you like,
FooVisible {
set { SomeElement.Visible = value; }
}
pseudo code of course :)

Accessing top master page properties in a nested master page code behind

I have a nested master page that has its own master page. The parent master page has a property defined in its code behind.
Public ReadOnly Property SelectedPage() As String
Get
Return _selectedPage
End Get
End Property
How can I reference the parent master page's property from within either the child master page's code behind Page_Load or aspx template page?
VB.Net:
DirectCast(Master, MyMastPageType).SelectedPage
C#:
((MyMastPageType)Master).SelectedPage
http://msdn.microsoft.com/en-us/library/system.web.ui.masterpage.master.aspx
protected void Page_Load(object sender, EventArgs e)
{
MyDemoMaster m = Master as MyDemoMaster;
m.MyProperty = "My button text";
}
See:
How to access controls inside a
nested master page?
The right way of accessing Master
page properties from a child page
Like this:
DirectCast(MyMastPageType, Master).SelectedPage
Here is how I use
MasterPage tmp = this.Master;
while (tmp.Master != null)
{
tmp = tmp.Master;
}

How to select current menu in master pages?

In my web app (asp.net C#) I have menus as "All", "Education", "Fun", "Comedy". I want when I select All then it should be displayed as current menu, for which I have a CSS class current.
In case of WebUserControls I can do it easily by passing parameter of current page to select as below:
mywebpage.aspx
<uc:header ID="header1" runat="server" selectedMenu="comedy" />
header.ascx (code (c#))
public string selectedMenu
{
get { return strSelected; }
set { strSelected = value; }
}
header.ascx (html)
<ul>
<li><a href="/all/" title="All Videos" <%if (strSelected == "all"){%>class="current"<%} %>><span>All</span></a></li>
<li><a href="/fun/" title="Fun Videos" <%if (strSelected == "fun"){%>class="current"<%} %>><span>Fun</span></a></li>
<li><a href="/comedy/" title="Comedy Videos" <%if (strSelected == "comedy"){%>class="current"<%} %>><span>Comedy</span></a></li>
</ul>
When I'll pass comedy from my webpage to usercontrol then it will select comedy menu. I want to implement the same kind of functionality in case of master pages, could anyone tell me how to achieve this type of facility for master pages.
One way what I feel is to pass a query string parameter http://example.com/all/?select=all, I'll check on master page_load function if select parameter is "all" or fun or comedy then select corresponding menu. But this is not good, because I don't want to add an extra query string parameter to my URLs.
Please tell me how to solve this issue.
Thanks
You can access master page properties from your content page after casting the master to the correct type:
public class MyMasterPage : MasterPage
{
public string MyMenuProperty { get; set; }
protected void Page_PreRender(object sender, EventArgs e)
{
if (MyMenuProperty == "comedy")
{
/* do your menu stuff */
}
}
}
public class MyContentPage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var myMaster = Page.Master as MyMasterPage;
if (myMaster != null)
{
myMaster.MyMenuProperty = "comedy";
}
}
One way i have done simular in the past is add this to the page derivatives of any content pages:
<%# MasterType VirtualPath="~/YourMaster.master" %>
Then in the master i exposed this:
private PageTypes currentPageType;
public PageTypes CurrentPageType
{
get { return currentPageType; }
set { currentPageType = value; }
}
On this job this was used so the master knew what type of page it was on and therefore changed a few things, colours, controls etc. So from a contents pageload i did
Master.CurrentPageType = PageTypes.System;
One thing to note however, VS tends to moan about the MasterType derivative until you do a rebuild all.
I'm thinking that maybe a completely different approach might be easier to implement and maintain.
How about you just parse the URL in your header.ascx?
For example:
<li><a href="/all/" title="All Videos" runat="server" class='<%= Request.Url.ToString().Contains("/all/") ? "current" : "" %>' ><span>All</span></a></li>
That way, you won't have to worry about setting properties, accessing/casting master pages etc...
Hai prashant,
I had the same issue a month back and i posted in stack overflow check this one it may help you Find a UnorderedList <UL> control inside a master page from a content page in asp.net

Categories

Resources