Skip to main content
 
Go Search
Home
Categories
Bloggers
Programmatically Setting a Web Part Page’s Title
By: Michael Becker | Posted: August 16, 2007 at 2:23 PM

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


  Comments   Add Comment   Share It  
  Your Name:
  Your Email: **will not be displayed
  Comment Title:
* Comments:
  If you cannot read the code, please
click here to get a new one. You won't
lose your comments by doing so.
* Security Code:
   
  
  
* Your Name:
* Your Email: **will not be displayed
* Recipient's Email:
* Subject:
  If you cannot read the code, please
click here to get a new one. You won't
lose your comments by doing so.
* Security Code:
  
  
  
Programmatically Setting a Web Part Page’s Title
By: nilesh | Posted: May 5, 2010 at 4:46 AM
Thank you. This help me to set my page title. otherwise i used javascript page load to set page title. Thanks
Thanks
By: Igor | Posted: May 14, 2009 at 1:34 AM
Thanks for the solution!
Page title
By: XristosK | Posted: January 12, 2009 at 6:33 AM
Hello, and thank you! I have troubles taking the page title - here is my code... Label listTitle = new Label(); ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)Page.Master.FindControl("PlaceHolderPageTitle"); LiteralControl LiteralControl = (LiteralControl)contentPlaceHolder.Controls[0]; listTitle.Text = LiteralControl.Text; Is this a correct approach; Thank you!
How to show the page title...
By: XristosK | Posted: January 12, 2009 at 6:32 AM
Hello, and thank you! I have troubles taking the page title - here is my code... Label listTitle = new Label(); ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)Page.Master.FindControl("PlaceHolderPageTitle"); LiteralControl LiteralControl = (LiteralControl)contentPlaceHolder.Controls[0]; listTitle.Text = LiteralControl.Text; Is this a correct approach; Thank you!
Thanks!!!
Posted: September 13, 2007 at 5:36 PM
Michael - thanks for posting that tip. It saved me a lot of digging.
Re: How to run this in aspx pages that can't run inline code
By: Michael Becker | Posted: September 13, 2007 at 12:54 AM
David, If you can't run server-side code, then I suppose you have two options. You can either set the title using the SharePoint GUI -- so basically just edit the properties of the page. Or you could write some javascript that executes OnLoad that uses the browser DOM to set the title. If the title is static, use the first option, if it's in any way dynamic (ie, not known at design time) then you'll have to go with the 2nd approach...
How to run this in aspx pages that can't run inline code
Posted: September 8, 2007 at 5:23 AM
How do you do this in SharePoint aspx pages where you can't run server side inline code?
 

 About Michael Becker

ArchitectMichael Becker is an architect for PointBridge. He has over 10 years of experience in the IT industry ranging from “Big 5” consulting to corporate IT for the insurance and investment industries. He is... [more]

 Tag Cloud

 External Links

 ‭(Hidden)‬ Admin Links