Click here to Skip to main content
13,196,946 members (57,152 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 13 Jul 2012

Accessing C# Variables in JavaScript

, 13 Jul 2012
Rate this:
Please Sign up or sign in to vote.
How to access the variables/properties from C# in JavaScript?


So often I come across this question on various forums - 'How to access the variables/properties from C# in JavaScript?' And this is one of the scenarios which you are (most) likely to come across if you are writing an ASP.NET application.

For most beginners, it might get confusing as they start wondering how to pass on information from a server side variable to client side.


The one shortcut we used during the (good old) ASP days, and which still works in ASP.NET is to declare a public property in codebehind. Let us say we declare a string variable firstName in codebehind.

public string firstName = "Manas";
public string lastName = "Bhardwaj";

Now, we can access this in aspx page/JavaScript like this:

    var myName;
    function GetMyName()
        myName = <%=this.firstName%> + ' ' + <%=this.lastName%>;


To do it nicely, you can use RegisterClientScriptBlock. RegisterClientScriptBlock accepts the following parameters:

  • Type: The type of the client script to register
  • key: The key of the client script to register
  • script: The client script literal to register
  • addScriptTags: A boolean value indicating whether to add script tags
string script = string.Format("var myName = '{0} {1}';", firstName, lastName);
if (!ClientScript.IsClientScriptBlockRegistered("myScript"))
    ClientScript.RegisterClientScriptBlock(typeof(_Default), "myScript", script, true);

Once done, the variable 'myName' is available on the client side (JavaScript in aspx page) and can be accessed like:

    function GetMyName()


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Manas Bhardwaj
Netherlands Netherlands

Read my personal blog at

You may also be interested in...

Comments and Discussions

QuestionThanks, I Know This But forgoten it :D Pin
Member 1030153414-Jan-14 20:59
memberMember 1030153414-Jan-14 20:59 
AnswerRe: Thanks, I Know This But forgoten it :D Pin
Manas Bhardwaj14-Jan-14 21:49
professionalManas Bhardwaj14-Jan-14 21:49 
QuestionJavaScript and edititemtemplate Pin
chaim_zvi9-Sep-12 2:57
memberchaim_zvi9-Sep-12 2:57 
SuggestionJSON Pin
stian.net14-Jul-12 11:46
memberstian.net14-Jul-12 11:46 
I prefer using a mvc controller or generic to create a json-response with variables to use in javascript. This keeps my javascript code clean and easy to read.

GeneralRe: JSON Pin
Manas Bhardwaj14-Jul-12 12:09
memberManas Bhardwaj14-Jul-12 12:09 
GeneralRe: JSON Pin
stian.net14-Jul-12 15:34
memberstian.net14-Jul-12 15:34 
QuestionHmmm Pin
Ryan Criddle13-Jul-12 18:36
memberRyan Criddle13-Jul-12 18:36 
AnswerRe: Hmmm Pin
George Mamaladze13-Jul-12 20:17
memberGeorge Mamaladze13-Jul-12 20:17 
AnswerRe: Hmmm Pin
Manas Bhardwaj14-Jul-12 0:10
memberManas Bhardwaj14-Jul-12 0:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171019.1 | Last Updated 14 Jul 2012
Article Copyright 2012 by Manas Bhardwaj
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid