views:

49

answers:

1

I am writing a webpart and was trying to update the browser title... so, I went into mywebpart.ascx added the following:

<asp:Content ID="contentPageTitle" ContentPlaceholderID="PlaceHolderPageTitle" runat="server">
    <%= SPContext.Current.Site.OpenWeb().Title %>
</asp:Content>

I then got this error: Content controls have to be top-level controls in a content page or a nested master page that references a master page.

So, I am trying to do it programatically in mywebpart.cs by doing:

Content content = new Content();
content.ContentPlaceHolderID = "PlaceHolderPageTitle";

I now need to input this piece: SPContext.Current.Site.OpenWeb().Title

What property in the Content control allows me to do that? If there is a better way to do this, I am open as well. Thanks.

A: 

Unfortunately, you cannot place a content control within a user control. As the error message indicates, content controls must be top level controls in a page or master page, and cannot belong in any other kind of control.

An alternate approach might be to customize your page layout or master page to contain the logic you want to provide.

If you have some assurance of the ID of the content control (not the ContentPlaceholderID, mind you), then you can interact with the content control like so:

var content = Page.FindControl("contentPageTitle");
content.Controls.Add(new LiteralControl("Hello, World!"));

--

On an aside, do ensure that any SPWeb opened with OpenWeb() gets disposed properly, or you may face memory management problems down the road.

kbrimington
Thanks for the tidbit. On the side note, should I dispose of all SPWeb objects? I usually use "Using", but since we are on the topic, can you advise me on which objects I should dispose of?
Josh
Here are some guidelines for SPWeb disposal. I recommend using SPDisposeCheck on your code as well. http://blogs.technet.com/b/stefan_gossner/archive/2008/12/05/disposing-spweb-and-spsite-objects.aspx
kbrimington