Programmatically Setting a Web Part Page’s Title - Perficient Blogs
Blog
  • Topics
  • Industries
  • Partners

Explore

Topics

Industries

Partners

Programmatically Setting a Web Part Page’s Title

Nothing new or exciting here, but I had to do this recently, and I couldn’t find out how to do it quickly…

I needed to set the text in the browser’s title bar based on some data that is being pulled from a database and displayed in a web part. This would be completely trivial in a plain old ASP.NET page. It was a little more complicated in SharePoint.

SharePoint wants to control the title itself so that it can be set based on the value you supply when you edit the properties for the page in the Document Library where it lives. For this reason, there’s a Content Placeholder dropped into the title tag in the Master Page. You need to update the value of this Content Placeholder in order to update the title tag. You can’t just set Page.Title.

So the sequence goes:

  1. Get a reference to the appropriate Content Placeholder
  2. Clear out anything that SharePoint might have put in it already
  3. Put your content in.

Or, in C#:

ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder) Page.Master.FindControl("PlaceHolderPageTitle");

contentPlaceHolder.Controls.Clear();

LiteralControl literalControl = new LiteralControl();

literalControl.Text = "Your text goes here";

contentPlaceHolder.Controls.Add(literalControl);

So, obviously not rocket science… But it took me longer than it should have none the less…

MB

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up