Search code examples
c#asp.netuser-controlsvs-web-site-projectvs-web-application-project

How do I access a public variable in the codebehind object for an ascx control? (Inline Expression)


This used to work (on a website project). Now, we're converting to a web application project, and I can't seem to get it to work.

Very simple ascx with nothing on it and creating a public class variable in the cs file.

This is part of the code in the cs file

public partial class Test: System.Web.UI.UserControl
{
    public string strThisOne = "This is the string";

    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

Now code in the ascx file (this used to work, what am I missing going forward?):

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="test.ascx.cs" Inherits="test" %>
<%= strThisOne %>

I now get an error saying that strThisOne does not exist in the current context. What changed? And how do I access the variable going forward (we used this technique a lot in the past to get values into JavaScript on the page)?


Solution

  • OK. So, Visual Studio shows this in the editor window as an error (with red wavy underlines), but when I compile there are no errors and the page works! Seems like an issue with Intellisense...