Accessing variable declared in aspx.cs in aspx or variable declared in ascx.cs ascx page

user1768031 picture user1768031 · Dec 13, 2012 · Viewed 16.7k times · Source

I was asked a question in an interview, weather we can access a publically declared variable which is been declared in aspx.cs or ascx.cs page in aspx or ascx page respectively.

Answer

Habib picture Habib · Dec 13, 2012

Yes you can, In ASPX page you can do:

<%=yourVariable%>

If you have defined in your code behind file .cs file as:

public string yourVariable;

If you want to use it in a span in aspx page then:

<span> <%= yourVariable %> </span>

You may see: Accessing CodeBehind Variables or C# Variables, Methods in ASPX Page in ASP.Net