8

I have the following two pages:

  1. Default.aspx
  2. Default.aspx.cs

How do I access variables in the code-behind file (Default.aspx.cs) from my embedded code in (Default.aspx) with the <% %> syntax?

1
  • Fixed! I forgot to mark my variable as "public". Commented Dec 13, 2011 at 15:15

3 Answers 3

16

Any public or protected (but not private, the "page" itself inherits from the code-behind Page class) class-level member can be accessed in this way. For example, if your code-behind class has a property:

protected string SomeValue { get; set; }

Then in your aspx code you can refer to it:

<% =SomeValue %>
Sign up to request clarification or add additional context in comments.

Comments

0

Simply reference them as if they are part of the current class.

<%= this.Foo %>

Comments

0

If you don't specify the access modifier for the variable the default is private and hence you cannot access it inside your page. It works for public, protected and friend. I prefer to use protected variables than public ones.

Comments

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.