C# ASP.NET MasterPage,不显示我的 <%=CodeBehindVar %>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16055335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MasterPage, doesn't display my <%=CodeBehindVar %>
提问by Boblepointu
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 Versionvar into the header, as a cache control. When I execute the code above, the result on the line is:
如图所示,我想将Versionvar显示到标题中,作为缓存控件。当我执行上面的代码时,行上的结果是:
<link href="../App_Themes/Global/Site.css?v=<%=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?
就像代码没有被解释。我无法找到解释。为什么会这样?
采纳答案by Marcianin
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!
希望这能帮助到此页面的其他人!
回答by Roar
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" />
回答by Hagar Abd El Aal
public static string Version
{
get
{
Assembly asm = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
return String.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart);
}
}

