简体   繁体   中英

Set Page Title from Custom System.Web.UI.Page class

For some reason i am created my own version of System.Web.UI.Page class and inherited the deafult Page class. Now i want to set Title of every page that using my custom Page class on Page_Init event. Can you please suggest me the best way without creating a new version of Page_Init method on drrived classes

public class MyPage : Page{
    protected void Page_Init(object sender, EventArgs e){ 
          //do some basic opeation
    }
}

public partial class Login : MyPage{
         //want to set title from Here
}

Thanks Ravi Mittal

What's wrong with doing this.Title ?

I'm not too sure if you're setting the AutoEventWireup property but I'd suggest you to override the OnInit method rather than using Page_Init

You can set it just via Title property

protected void Page_Init(object sender, EventArgs e){ 
      //do some basic opeation
    this.Title = "Some title";
}

If you want to make it different for each sub page, you can make abstract property

public abstract class MyPage : Page
{
    public abstract string PageTitle {get;}

    protected override void OnInit(EventArgs e){ 
          //do some basic opeation
        this.Title = PageTitle;
        base.OnInit(e);
    }

}

and sample page

public class SubPage : MyPage
{
    public override string PageTitle
    {
        get { return "Some sub title";}
    }
}

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