I’ve spent some time today trying to figure out how to set the title of a SharePoint page from my own code. As blogger Michael Becker rightly points out, you can’t simply set Page.Title.
The correct solution, as provided by Michael, is illustrated in this example C# code:
// Get a reference to the appropriate Content Placeholder
ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)
Page.Master.FindControl("PlaceHolderPageTitle");
// Clear out anything that SharePoint might have put in it already
contentPlaceHolder.Controls.Clear();
// Put your content in
LiteralControl literalControl = new LiteralControl();
literalControl.Text = "Your text goes here";
contentPlaceHolder.Controls.Add(literalControl);
Happily this even works when you “cheat” by hosting an ASP.NET user control within a SmartPart, as opposed to creating a bona fide Web Part.
Thank you!!!!
This does not work for me,in sharepoint sandbox solution on
ontentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)
Page.Master.FindControl(“PlaceHolderPageTitle)
gives me error null exception, why??
Thanks,
This is a great post.
I got such a headache looking for this solution until I found your article.
Thank you! This works!
Thanks! Really useful post!!!
Thank’s for that,
in SP 2013 this worked for me on a none publishing site with placeholder “PlaceHolderPageTitleInTitleArea”.
This code will fail in SharePoint 2013 publishing sites if someone set the page title in “Edit SEO Properties”.
In this case this code should be used:
ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)page.Master.FindControl("PlaceHolderPageTitle");
contentPlaceHolder.SetRenderMethodDelegate(delegate(System.Web.UI.HtmlTextWriter writer,
System.Web.UI.Control Container)
{
writer.Write(title);
});