简体   繁体   English

无法从 .aspx 页面访问代码隐藏变量

[英]Code Behind variable is not accessible from .aspx page

I have a variable in an aspx.cs page called VersionNumber:我在 aspx.cs 页面中有一个名为 VersionNumber 的变量:

namespace App_name
{
  public partial class Default : System.Web.UI.Page
  {
    public string VersionNumber = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
  }
}

This variable should be accessible in the .aspx page like such, but instead there is a red squiggly line telling me it doesn't exist in this context:这个变量应该可以像这样在 .aspx 页面中访问,但是有一条红色的波浪线告诉我它在这种情况下不存在:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="App_name.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>App Name <%=VersionNumber %></title>
<script type="text/javascript" src="scripts/jquery-1.12.4.js"></script>
<script type="text/javascript" src="bs/js//bootstrap-typehead.js?<%=VersionNumber %>"></script>
<script type="text/javascript" src="scripts/Default.js?<%=VersionNumber %>"></script>
<script type="text/javascript" src="scripts/Helper.js?<%=VersionNumber %>"></script>

etc.等等。

How can I make VersionNumber accessible in the .aspx page?如何在 .aspx 页面中访问 VersionNumber? thanks谢谢

我的问题的解决方案是在 aspx 页面的 Inherit 部分编写命名空间以及类名,如下所示:

Inherits="Solution_History.Default"

I have tested your code and come to the following solutions:我已经测试了您的代码并得出以下解决方案:

The cs file should be like this without a change to the aspx page: cs 文件应该是这样的,不需要更改 aspx 页面:

namespace Solution_History
{
    public partial class Default : System.Web.UI.Page
    {
        public string VersionNumber = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

Not adding the namespace to the cs file then the first line in the aspx should be:不将命名空间添加到 cs 文件,那么 aspx 中的第一行应该是:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" %

I've tried both possibilities and both showed version numbers.我已经尝试了两种可能性并且都显示了版本号。

Hope this helps.希望这可以帮助。

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

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