简体   繁体   中英

Code Behind variable is not accessible from .aspx page

I have a variable in an aspx.cs page called 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:

<%@ 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? 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:

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:

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

I've tried both possibilities and both showed version numbers.

Hope this helps.

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