【问题标题】:Response.write to <head>Response.write 到 <head>
【发布时间】:2012-08-10 16:00:47
【问题描述】:

希望这对某人来说不会是一个难以回答的问题,但我在网上找到解决方案时遇到了很多麻烦。我正在尝试从后面的代码(它是 VB.net)向我的 asp.net 页面添加一些 HTML。我想将 HTML 添加到页面的头部,但目前只能添加到正文。

【问题讨论】:

    标签: asp.net html vb.net


    【解决方案1】:

    您可以将代码放在头部,就像在正文中一样。例如:

    <%= CallAMethodThatReturnsAStringOfHtml() %>
    

    【讨论】:

      【解决方案2】:

      您可以尝试在后面的代码中创建一个属性,然后在 Page_Load 方法中添加您的 html:

      Public MyHtml As String
      

      然后在你的 HTML 的 head 部分中使用文字符号:

      <%= MyHtml %>
      

      【讨论】:

        【解决方案3】:

        在你的 head 元素上有runat 属性,你就可以访问它

        <head id="someHead" runat="server">
        
        </head>
        

        现在在你的代码隐藏中,你可以像这样设置它

         someHead.InnerHtml="<script src='somelibrary.js' ></script>";
        

        【讨论】:

          【解决方案4】:

          我是这样做的,并且成功了:

          .aspx 文件:

          ... ...

          在 aspx.cs 文件上:

              protected string GetDisclosureText()
              {
                  string disclosure = ""; 
                  // ...Apply  custom logic ...
                  if (!string.IsNullOrEmpty(disclosure))
                  {
                      return disclosure;
                  }
                  return "Error getting Disclosure Text";
              }
          

          请注意,唯一的区别是我调用的是 Response.Write,而不仅仅是函数。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-02-06
            • 1970-01-01
            • 2012-01-22
            • 2013-05-09
            • 2010-10-29
            • 1970-01-01
            • 2013-02-20
            相关资源
            最近更新 更多