ASP.NET MasterPage,不显示我的<%= CodeBehindVar%>

时间:2013-04-17 08:36:38

标签: c# master-pages webforms

以下是我想要做的一个例子:

内容方面(Structure.master):

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Structure.master.cs" Inherits="Structure" %>

<!DOCTYPE HTML>
<html>
<head runat="server">
    <title>Bienvenue sur</title>
    <meta charset="utf-8" content="" />
    <link runat="server" href="App_Themes/Global/Metro.css?v=22" rel="stylesheet" type="text/css" />
    <link runat="server" href="App_Themes/Global/Site.css?v=<%=Version %>" rel="stylesheet" type="text/css" />
    <link runat="server" href="App_Themes/Global/Structure.css?v=22" rel="stylesheet" type="text/css" />

和codebehind(Structure.master.cs):

using System;
using System.Linq;
using BaseInstanceEntity = Library.Common.Entities.BaseEntities.BaseInstanceEntity;
using BaseInstanceManager = Library.Manager.BaseInstanceManager;

public partial class Structure : System.Web.UI.MasterPage
{
    public string Version { get; set;}

    protected void Page_Load(object sender, EventArgs e)
    {
        Version = System.Configuration.ConfigurationManager.AppSettings["Version"].Replace(".", "");

如图所示,我想将Version var显示在标题中,作为缓存控件。当我执行上面的代码时,该行的结果是:

<link href="../App_Themes/Global/Site.css?v=&lt;%=Version %>" rel="stylesheet" type="text/css" />

就像代码没有被解释一样。我无法找到解释。为什么会这样?

3 个答案:

答案 0 :(得分:1)

嗯,我迟到了这个问题,但是当我需要动态创建CSS链接时,我只需在后面的代码上生成整个标记,然后就像这样在标记上植入它:

public partial class WebForm1 : System.Web.UI.Page
{
    public string link;
    protected void Page_Load(object sender, EventArgs e)
    {
        string ver = "1.0.0";
        link = "<link rel=\"stylesheet\"  href=\"../App_Themes/Global/Site.css?v=" + ver + "\"/>";
    }
}

然后标记

 <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<%= link %>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

希望这会有助于访问此页面的其他人!

答案 1 :(得分:0)

也许它应该是这样的:

<link runat="server" 
      href='<%# string.Format("{0}?v={1}", Page.ResolveUrl("~/App_Themes/Global/Site.css"), Version)%>' 
      type="text/css" />

答案 2 :(得分:0)

    public static string Version
    {
        get
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            return String.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart);
        }
    }