简体   繁体   English

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

[英]ASP.NET MasterPage, doesn't display my <%=CodeBehindVar %>

Here is an example of what I want to do: 这是我想做的一个例子:

Content side (Structure.master): 内容方(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" />

And codebehind (Structure.master.cs): 和代码隐藏(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(".", "");

As shown, I want to display the Version var into the header, as a cache control. 如图所示,我想将Version var显示在标题中,作为缓存控件。 When I execute the code above, the result on the line is: 当我执行上面的代码时,该行的结果是:

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

It's like the code isn't interpreted. 就像代码没有被解释。 I couldn't manage to find explanation. 我找不到解释。 Why is this happening? 为什么会这样呢?

well I got to this question late but when I need to create CSS links dynamically I just generate the entire tag on code behind and the just plant it on mark-up like this: 好吧,我迟到了这个问题,但是当我需要动态创建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 + "\"/>";
    }
}

then mark up 然后标记

 <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>

Hopefully this will help someone else who comes to this page! 希望这对其他访问此页面的人有所帮助!

Maybe it should be like this: 也许应该是这样的:

<link runat="server" 
      href='<%# string.Format("{0}?v={1}", Page.ResolveUrl("~/App_Themes/Global/Site.css"), Version)%>' 
      type="text/css" />
    public static string Version
    {
        get
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            return String.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM