无法在中使用 Response.Write aspx 页面的一部分?
我正在尝试使用 Response.Write() 方法在 << 中动态插入内容头> aspx 页面的一部分。我需要从代码隐藏对象的属性中注入一个字符串值,该对象是我的 CSS 文件的链接。但是,它在运行时没有得到正确处理。该对象在类中是公共的,并在 Page_Load() 事件中进行水化。在页面正文中,我可以成功地从 Corpoartion 对象注入其他属性,完全没有问题。
为什么这在 << 中不起作用?头>部分?
这是未正确扩展的部分:
<link href="<%= Corporation.PageStyleSheet %>" rel="stylesheet" type="text/css" />
这是整个 <<头>部分:
<head runat="server">
<title></title>
<link href="<%= Corporation.PageStyleSheet %>" rel="stylesheet" type="text/css" />
<script language="JavaScript" type="text/JavaScript" src="cntv_menu.js"></script>
<script language="JavaScript" type="text/JavaScript" src="cntv_category.js"></script>
</head>
这无法正常扩展的原因是什么?
I'm trying to use the Response.Write() method to dynamically insert content in the < head > section of an aspx page. I need to inject a string value from a property on a code-behind object which is a link to my CSS file. However, it is not being processed properly at run time. The object is public on the class and is hydrated in the Page_Load() event. Down in the body of the page I can successfully inject other properties from the Corpoartion object with no problem at all.
Why does this not work in the < head > section?
This is the part that does not expand correctly:
<link href="<%= Corporation.PageStyleSheet %>" rel="stylesheet" type="text/css" />
Here is the entire < head > section:
<head runat="server">
<title></title>
<link href="<%= Corporation.PageStyleSheet %>" rel="stylesheet" type="text/css" />
<script language="JavaScript" type="text/JavaScript" src="cntv_menu.js"></script>
<script language="JavaScript" type="text/JavaScript" src="cntv_category.js"></script>
</head>
What is the reason that this will not expand properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能在
runat="server"
标记内使用<%= %>
,您的标记是。
您可以将其更改为
<%# %>
并在代码隐藏中对其进行 DataBind,也可以将链接标记设置为runat="server"
,给它一个id
并从后面的代码中分配属性。请参阅此答案,其中包含详细信息。
You can't use
<%= %>
inside arunat="server"
tag, which your<head>
tag is.You can either change it to
<%# %>
and DataBind to it in the code-behind, or you can make the link tagrunat="server"
, give it anid
and assign the attribute from the code behind.See this answer, which goes into the details.
使用这个:
它对我有用:)
Use this:
It worked for me :)
解决此问题的最佳方法是使用 OnPreRender
示例:
首先,定义您的标签:
在 OnPreRender 上:
the best way to resolve this problem is using OnPreRender
Example:
First, define your tag:
And on the OnPreRender:
如果你写出整行应该没问题:
PS 我的语法可能不完全正确,提前抱歉。
If you write out the full line you should be ok:
P.S. My syntax may not be completely right, sorry in advance.