希望这对某人来说不是一个很难回答的问题,但我在网上找到解决方案时遇到了很多麻烦。我正在尝试从后面的代码向我的 asp.net 页面添加一些 HTML(这是 VB.net)。我想将 HTML 添加到页面的标题部分,但目前只能添加到正文中。
你可以把代码放在脑袋里,就像身体一样。例如:
<%= CallAMethodThatReturnsAStringOfHtml() %>
您可以尝试在代码隐藏中创建属性,并在 Page_Load 方法中添加 html:
Public MyHtml As String
然后在 HTML 的 head 部分中,只需使用文字表示法:
<%= MyHtml %>
在你的头元素上有runat
属性,你将能够访问它
<head id="someHead" runat="server">
</head>
现在,在您的代码隐藏中,您可以将其设置为
someHead.InnerHtml="<script src='somelibrary.js' ></script>";
我这样做了,它奏效了:
在.aspx文件上:
。 <% Response.Write(GetDisclosureText()); %> ...
在 ASPX.cs 文件上:
protected string GetDisclosureText()
{
string disclosure = "";
// ...Apply custom logic ...
if (!string.IsNullOrEmpty(disclosure))
{
return disclosure;
}
return "Error getting Disclosure Text";
}
请注意,唯一的区别是我调用 Response.Write,而不仅仅是函数。