how to call a variable in code behind to aspx page

i know i have seen this but cant recall the correct way of doing it… basically i have a string variable called “string clients” in my .cs file.. but i wasn’t to be able to pass it to my aspx page something like


please correct me, i do not recall or not sure how to do this. (new to c#) and when i googled it.. it was not clear.. or not many of these out there.. searched as

“ c# <%= %> not consistent results.. maybe because i do not know how to call these..


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The field must be declared public for proper visibility from the ASPX markup. In any case, you could declare a property:

private string clients;
public string Clients { get { return clients; } }

UPDATE: It can also be declared as protected, as stated in the comments below.

Then, to call it on the ASPX side:


Note that this won’t work if you place it on a server tag attribute. For example:

<asp:Label runat=”server” Text=”<%=Clients%>” />

This isn’t valid. This is:


Method 2

In your code behind file, have a public variable

public partial class _Default : System.Web.UI.Page
    public string clients;

    protected void Page_Load(object sender, EventArgs e)
        // your code that at one points sets the variable
        this.clients = "abc";

now in your design code, just assign that to something, like:

    <p><%= clients %></p>

or even a javascript variable

<script type="text/javascript">

    var clients = '<%= clients %>';


Method 3



to work you need to have a public or protected variable clients in the code-behind.

Here is an article that explains it:

Method 4

Make sure that you have compiled your *.cs file before browsing the ASPX page.

Method 5

First you have to make sure the access level of the variable is protected or public. If the variable or property is private the page won’t have access to it.

Code Behind

protected String Clients { get; set; }


<span><%=Clients %> </span>

Method 6

You need to declare your clients variable as public, e.g.

public string clients;

but you should probably do it as a Property, e.g.

private string clients;
public string Clients{ get{ return clients; } set {clients = value;} }

And then you can call it in your .aspx page like this:


Variables in C# are private by default. Read more on access modifiers in C# on MSDN and properties in C# on MSDN

Method 7

I would create a property to access the variable, like this:

protected string Test
    get; set;

And in your markup:

<%= this.Test %>

Method 8

The HelloFromCsharp.aspx look like this

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HelloFromCsharp.aspx.cs" Inherits="Test.HelloFromCsharp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
       <%= clients%>

And the HelloFromCsharp.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Test
    public partial class HelloFromCsharp : System.Web.UI.Page
        public string clients;
        protected void Page_Load(object sender, EventArgs e)
            clients = "Hello From C#";

Method 9

You can access a public/protected property using the data binding expression <%# myproperty %> as given below:

    <asp:Label ID="Label1" runat="server" Text="<%#CodeBehindVarPublic %>"></asp:Label>

you should call DataBind method, otherwise it can’t be evaluated.

    public partial class WebForm1 : System.Web.UI.Page
     public string CodeBehindVarPublic { get; set; }
      protected void Page_Load(object sender, EventArgs e)
          CodeBehindVarPublic ="xyz";
       //you should call the next line  in case of using <%#CodeBehindVarPublic %>



All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x