【问题标题】:Insert literal control inside HEAD when <head runat="server"> is not specified?未指定 <head runat="server"> 时在 HEAD 中插入文字控件?
【发布时间】:2011-08-31 22:45:05
【问题描述】:

由于各种原因,我无法标记我的&lt;head&gt; 标签runat="server"。我还有其他方法可以访问它并在其下方附加文字吗?我需要添加一个脚本(作为文字)并让它成为&lt;head&gt; 标签内部引用的第一个脚本。

也许使用 FindControl?

【问题讨论】:

    标签: asp.net webforms page-lifecycle


    【解决方案1】:

    可以声明 Literal 服务器控件而不将 head 元素定义为服务器控件:

    <head>
        <asp:Literal ID="literal1" runat="server"></asp:Literal>
        <title></title>
    </head>
    

    使用它,您可以根据需要从后面的代码中添加内容:

    protected void Page_Load(object sender, EventArgs e)
    {
        literal1.Text = "<script type=\"text/javascript\" src=\"scripts.js\"></script>";
    }
    

    【讨论】:

      【解决方案2】:

      您只能使用FindControl 来查找控件,即作为服务器控件的元素。所有其他元素仅作为文本控件中的文本处理。

      你可以在head标签中放一个PlaceHolder控件,然后你可以在里面添加文字控件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-23
        • 1970-01-01
        • 2012-07-15
        • 2017-06-02
        • 2022-01-22
        相关资源
        最近更新 更多