ASP.NET内联编码:变量名不会被值替换

时间:2009-07-29 07:05:52

标签: asp.net inline

我有一个ASP.NET页面。在页面加载中我设置了一个公共变量的值。在内联编码部分,我正在加载一个CSS,它是一个名称在公共变量中可用的文件夹我的HTML标记如下

<%@ Page Language="C#"  EnableEventValidation="false" AutoEventWireup="true" CodeFile="MyPage.aspx.cs"  Theme="GridView" Inherits="GUI.MyPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MyPage</title>
<link href="../Vendors/<%=vendorName%>/css/style.css" rel="stylesheet" type="text/css" />

</head>
 <body>
<%=vendorName %> <!-- here value is printed correctly -->
 ...
 </body>

并在我的代码后面

 public partial class MyPage: MyCommonClass
 {
    public string vendorName = "";
     protected void Page_Load(object sender, EventArgs e)
     {
        vendorName = "ACLL";
     }

 }

但是当我运行页面时,&lt;%= VEndorId%&gt;没有替换它中的值。但是在Body中,它正确打印。但是在头部它没有来。我检查了ViewSource并找到源HTML,如下所示

<link href="../Vendors/&lt;%=vendorName%>/Lib/css/tradein.css" rel="stylesheet" type="text/css" />

3 个答案:

答案 0 :(得分:7)

这两个选项是:

<link href="<%= string.Format("../Vendors/{0}/css/style.css", vendorName) %>" type="text/css" rel="Stylesheet" /> // as Greco stated

<style>
  @import url("../Vendors/<%=vendorName%>/css/style.css");
</style>

答案 1 :(得分:3)

将runat =“server”标记添加到link元素。

答案 2 :(得分:0)

类似的问题和良好的答案here

解决方案是将内联代码的引号移动到代码

<link href=<%="'../Vendors/" + vendorName + "/css/style.css'"%> rel="stylesheet"...
               ^                                           ^
相关问题