本文介绍了内嵌code头标记 - ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能做一个头标记这样的事情,母版页,其中有runatserver的:

 <链接rel =stylesheet属性类型=文/ CSS的href ='<%= Config.ResourcesDomain%GT; /images/style.css/>

这是行不通的,因为它产生这样的html:

 <链接rel =stylesheet属性类型=文/ CSS的href =&放大器; LT;%= Config.ResourcesDomain%GT; /images/style.css/ >


解决方案

输出正在呈现像这样的原因:

  HREF =&放大器; LT;%= Config.ResourcesDomain%GT; /images/style.css

是因为ASP.NET是治疗链接作为 HtmlLink 控制和渲染的href的内容属性为文字。

这是标头部分作为服务器控件,其中某些元素被视为服务器控件很怪,(即使没有使用 =服务器属性)。

删除周围的href属性报价解决问题:

  HREF =<%= Config.ResourcesDomain%GT; /images/style.css

这样做停止链接元素被视为一台服务器控制,从而执行code座和渲染正确的URL。

但是,上面写出来的的href 值不包括引号。使用下面,将引号添加到链接标记:

  HREF =<%=的String.Format('{0}',Config.ResourcesDomain)%GT; /images/style.css

希望这有助于。

修改

奇怪的是,如果使用双引号为的href 属性,包括:code座内的双引号,这也解决了问题:

  HREF =<%=+ Config.ResourcesDomain%GT; /images/style.css


不过,以上都不是特别优雅的解决方案,并从code背后设置的URL可能是要走的路。

Is it possible to do something like this in a head tag, of master page, which has runatserver:

 <link rel="Stylesheet" type="text/css" href='<%=Config.ResourcesDomain %>/images/style.css' />

This is not working, as it produces this kind of html:

<link rel="Stylesheet" type="text/css" href="&lt;%=Config.ResourcesDomain %>/images/style.css" />
解决方案

The reason the output is being rendered like so:

href="&lt;%=Config.ResourcesDomain %>/images/style.css"

Is because ASP.NET is treating the link as an HtmlLink control, and rendering the contents of the href attribute as a literal.

This is a strange quirk of marking the head section as a server control, where certain elements are treated as server controls (even without being marked explicitly with the runat="server" attribute).

Removing the quotations around the href attribute resolves the issue:

href=<%= Config.ResourcesDomain %>/images/style.css

Doing so stops the link element being treated as a server control, thus executing the code block and rendering the correct URL.

However, the above writes the href value out without quotes. Using the following, will add the quotes to the link tag:

href=<%= String.Format("'{0}'", Config.ResourcesDomain) %>/images/style.css

Hope this helps.

Edit

Strangely, if you use double quotes for the href attribute, and include double quotes within the code block this also resolves the issue:

href="<%= "" + Config.ResourcesDomain %>/images/style.css"


However, none of the above are particularly elegant solutions, and setting the URL from the code behind is probably the way to go.

这篇关于内嵌code头标记 - ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 04:30
查看更多