How can I reference a constant from constants.cs in page.aspx, I'm trying the following without any success
<%@ Import Namespace="MyConstants" %>
<uc:MyControl ID="id1" runat="server" ConstantValue=" <%= Constants.TheValue %>" />
<uc:MyControl ID="id2" runat="server" ConstantValue=" <%# Constants.TheValue %>" />
<uc:MyControl ID="id3" runat="server" ConstantValue=" <%= MyConstants.Constants.TheValue %>" />
<uc:MyControl ID="id4" runat="server" ConstantValue=" <%# MyConstants.Constants.TheValue %>" />
And in Constants.cs
namespace MyConstants
public class Constants
public const string TheValue = "Hello, World";
You need to import your namespace. You do this differently depending on your view engine.
If you're using WebForms:
<%@ Import Namespace="Your.Namespace" %>
If you're using Razor with C#
@using Your.Namespace
If you're using Razor with VB.NET
@Imports Your.Namespace
Have you tried using the fully qualified class-name?
<%= MyNamespace.MySubNamespace.Constants.TheValue %>
If that works, you can add this namespace to namespaces list in the web.config.
<pages>
<namespaces>
<add namespace="MyNamespace.MySubNamespace" />
</namespaces>
</pages>
And then you won't have to fully-qualify the class name in any page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With