简体   繁体   中英

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

Here is an example of what I want to do:

Content side (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):

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

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);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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