Yoshi Walsh
Yoshi Walsh

Reputation: 2077

Display asp:content in multiple locations?

I'm working with ASP.NET masterpages, and I'd like it so that a template can specify a page title ONCE and this is then used in both the <head><title>Title Element</title></head> and also the <h1>Page Header</p1>.

I found a piece of code that appeared to do what I wanted, but I believe it was in VB instead of C# so I adapted it a little:

<asp:content ContentPlaceHolderId="Head" runat="server">
  <title><asp:ContentPlaceHolder Id="Title" runat="server">
    </asp:ContentPlaceHolder></title>
</asp:content>

<asp:content ContentPlaceHolderId="PageHeading1" runat="server">
  <%
    LiteralControl title = Title.Controls.Item(0).Text;
    Response.Write(title.Text);
  %>
</asp:content>

This code gives me the following error:

CS1061: 'System.Web.UI.ControlCollection' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'System.Web.UI.ControlCollection' could be found (are you missing a using directive or an assembly reference?)

I'm very new to ASP.NET and Umbraco, so please go easy on me.

Thanks,

YM

Upvotes: 0

Views: 45

Answers (1)

sh1rts
sh1rts

Reputation: 1874

C# uses square brackets for indexers.

Change

LiteralControl title = Title.Controls.Item(0).Text;

to

LiteralControl title = Title.Controls[0].Text;

The error is saying the compiler cannot find a method called Item, which does not exist on ControlCollection.

This is worth a read, it's for older versions of C# but still a useful grounding - http://msdn.microsoft.com/en-us/library/aa288462(v=vs.71).aspx

Upvotes: 1

Related Questions