Click here to Skip to main content
Click here to Skip to main content

Accessing C# Variables in JavaScript

By , 13 Jul 2012
 

Introduction

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.

Solution

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:

<script>
    var myName;
    function GetMyName()
    {
        myName = <%=this.firstName%>

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:

<script>
    function GetMyName()
    {
	alert(myName);
    }
</script>

License

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

About the Author

Manas Bhardwaj
Technical Lead
United States United States
Member
Manas Bhardwaj's Stream : www.manasbhardwaj.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionJavaScript and edititemtemplatememberchaim_zvi9 Sep '12 - 2:57 
SuggestionJSONmemberstian.net14 Jul '12 - 11:46 
GeneralRe: JSONmemberManas Bhardwaj14 Jul '12 - 12:09 
GeneralRe: JSONmemberstian.net14 Jul '12 - 15:34 
QuestionHmmmmemberRyan Criddle13 Jul '12 - 18:36 
What if a client side coder looked at this page and had to work on it, without knowledge of the code behind, and tried to understand the javascript here?
 
I'm not sure it's such a good idea to hide javascript variables in server side code like this. The first example with inline server tags, whilst not being as nice to look at maybe, is much clearer as to how the code works.
AnswerRe: HmmmmemberGeorge Mamaladze13 Jul '12 - 20:17 
AnswerRe: HmmmmemberManas Bhardwaj14 Jul '12 - 0:10 

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

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