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