NickyLarson
NickyLarson

Reputation: 137

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

Upvotes: 0

Views: 1263

Answers (2)

NickyLarson
NickyLarson

Reputation: 137

the solution to my problem was to write the namespace as well as the class name in the Inherit part of the aspx page, like such:

Inherits="Solution_History.Default"

Upvotes: 2

KH S
KH S

Reputation: 444

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.

Upvotes: 0

Related Questions