Click here to Skip to main content
Click here to Skip to main content
Go to top

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?

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)

Share

About the Author

Manas Bhardwaj
Architect
Netherlands Netherlands
Read my personal blog at www.manasbhardwaj.net.
Follow on   Twitter

Comments and Discussions

 
QuestionThanks, I Know This But forgoten it :D PinmemberMember 1030153414-Jan-14 20:59 
AnswerRe: Thanks, I Know This But forgoten it :D PinprofessionalManas Bhardwaj14-Jan-14 21:49 
QuestionJavaScript and edititemtemplate Pinmemberchaim_zvi9-Sep-12 2:57 
SuggestionJSON Pinmemberstian.net14-Jul-12 11:46 
GeneralRe: JSON PinmemberManas Bhardwaj14-Jul-12 12:09 
GeneralRe: JSON Pinmemberstian.net14-Jul-12 15:34 
QuestionHmmm PinmemberRyan Criddle13-Jul-12 18:36 
AnswerRe: Hmmm PinmemberGeorge Mamaladze13-Jul-12 20:17 
AnswerRe: Hmmm PinmemberManas Bhardwaj14-Jul-12 0:10 

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

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

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