无法在中使用 Response.Write aspx 页面的一部分?

发布于 2024-08-22 05:37:14 字数 838 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森林迷了鹿 2024-08-29 05:37:14

您不能在 runat="server" 标记内使用 <%= %>,您的 标记是。

您可以将其更改为 <%# %> 并在代码隐藏中对其进行 DataBind,也可以将链接标记设置为 runat="server",给它一个id并从后面的代码中分配属性。

请参阅此答案,其中包含详细信息。

You can't use <%= %> inside a runat="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 tag runat="server", give it an id and assign the attribute from the code behind.

See this answer, which goes into the details.

陪我终i 2024-08-29 05:37:14

使用这个:

this.myButton.Attributes.Add(attribute, value);

它对我有用:)

Use this:

this.myButton.Attributes.Add(attribute, value);

It worked for me :)

你不是我要的菜∠ 2024-08-29 05:37:14

解决此问题的最佳方法是使用 OnPreRender

示例:

首先,定义您的标签:

<link href="~/css/your_default.css" type="text/css" runat="server" id="myCSS" />

在 OnPreRender 上:

protected override void OnPreRender(EventArgs e){
     base.OnPreRender(e);
     myCSS.Attributes["href"] = "~/css/your_new.css";
}

the best way to resolve this problem is using OnPreRender

Example:

First, define your tag:

<link href="~/css/your_default.css" type="text/css" runat="server" id="myCSS" />

And on the OnPreRender:

protected override void OnPreRender(EventArgs e){
     base.OnPreRender(e);
     myCSS.Attributes["href"] = "~/css/your_new.css";
}
听,心雨的声音 2024-08-29 05:37:14

如果你写出整行应该没问题:

<%
Response.write("<link href=\"" + Corporation.PageStyleSheet + "\" rel=\"stylesheet\" />");
%>

PS 我的语法可能不完全正确,提前抱歉。

If you write out the full line you should be ok:

<%
Response.write("<link href=\"" + Corporation.PageStyleSheet + "\" rel=\"stylesheet\" />");
%>

P.S. My syntax may not be completely right, sorry in advance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文