简体   繁体   中英

Setting title of a page

I have an ASP.NET application that uses a master page configuration. What I'd like to do is two things.

  1. How can I programmically set the title in the child page (as in the text in the <title></title> tags? And,

  2. 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.)

  1. For your first question,This is the one you are looking for http://www.devasp.net/net/articles/display/852.html and
  2. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM