asp:呈现的文字标记

时间:2012-06-08 18:38:38

标签: asp.net

我正在尝试在母版页的元素中放置一个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>

1 个答案:

答案 0 :(得分:1)

尝试使用实际的脚本引用而不是放置文本。如果您在头部区域中允许使用适当的内容作为文字文本,它将被置于头部。

ltlGoogleAnalytics.Text = "<script src='/scripts/analytics.js' type='text/javascript'></script>"