0

マスターページの要素にasp:literalタグを配置しようとしています。マスターページのコードビハインドで値を設定し、このマスターページを使用しているページを表示すると、リテラルの値が<body>に出力されます。

私は次のマスターページを持っています:

 <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Test1.master.cs" Inherits="Corporate.Presentation.Web.Test1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:Literal runat="server" ID="ltlGoogleAnalytics"></asp:Literal>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

コードビハインド:

using System;

namespace Corporate.Presentation.Web
{
    public partial class Test1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ltlGoogleAnalytics.Text = "test";
        }
    }
}

ASPXページのテスト:

<%@ Page Title="" Language="C#" MasterPageFile="~/Test1.Master" AutoEventWireup="true" CodeBehind="TestForm1.aspx.cs" Inherits="Corporate.Presentation.Web.TestForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>

HTML出力:

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>

</title></head><body>test


    <form method="post" action="TestForm1.aspx" id="aspnetForm">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTY1NDU2MTA1Mg9kFgJmD2QWAgIBD2QWAgIBDxYCHgRUZXh0BQR0ZXN0ZGQVhLZ0Btj29J7NtToygADkXCPCuLkULTvV5jvIb8hFFw==">
</div>

    <div>


    </div>
    </form>


</body></html>
4

1 に答える 1

1

テキストを配置する代わりに、実際のスクリプト参照を配置してみてください。文字通りのテキストとして頭の領域で許可されている適切なコンテンツがある場合は、頭に配置されます。

ltlGoogleAnalytics.Text = "<script src='/scripts/analytics.js' type='text/javascript'></script>"
于 2012-06-08T19:01:24.563 に答える