0

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

8
  • Can you show the code around the public string VersionNumber? Commented Sep 16, 2019 at 9:41
  • Please show the first 8 lines of the aspx file. Commented Sep 16, 2019 at 9:51
  • Updated post with both requests Commented Sep 16, 2019 at 10:18
  • I would first add a value to the public string VersionNumber = like "2.0" just to make sure it shows. If it shows the problem is mostlukely here: System.Reflection.... Commented Sep 16, 2019 at 10:30
  • Tried hardcoding a value but still the same issue, aspx page shows "VersionNumber doesn't exist in the current context" Commented Sep 16, 2019 at 10:47

2 Answers 2

2

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"
Sign up to request clarification or add additional context in comments.

Comments

1

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.

5 Comments

Thanks for your help. That code appears to be exactly like the code I posted though? I couldn't spot any difference.
I haven't added it above, but I do have a namespace too
I think you need to add it in order to work, the namespace I mean. I can't see how the aspx page could access the code behind otherwise. Try copying my examples. They work on my test page like that adn should work.
Yes, what I am saying is that it was already added, I just hadn't included it above in my question. I just copy-pasted your code and I still get the same error. How strange.
Understand. I would create a new page for testing.Just the code no name spaces, etc. and see it if makes a difference. Something must be different in your pages than in mine.I do get 0.0.0.0 as the version as I just a website.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.