在多个位置显示asp:内容?

时间:2014-05-22 03:06:28

标签: c# asp.net master-pages umbraco

我正在使用ASP.NET主页,我希望它能让模板指定页面标题ONCE,然后在<head><title>Title Element</title></head><h1>Page Header</p1>中使用它

我发现了一段看起来像我想要的代码,但我相信它是用VB而不是C#所以我稍微调整了一下:

<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>

此代码给出了以下错误:

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?)

我是ASP.NET和Umbraco的新手,所以请放轻松我。

谢谢,

YM

1 个答案:

答案 0 :(得分:1)

C#使用方括号作为索引器。

更改

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

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

错误是说编译器找不到名为Item的方法,它在ControlCollection中不存在。

这值得一读,它适用于旧版本的C#,但仍然是一个有用的基础 - http://msdn.microsoft.com/en-us/library/aa288462(v=vs.71).aspx