Click here to Skip to main content
13,191,400 members (74,588 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 4 Mar 2013

Pass Variables From Server to JavaScript after JavaScript is Loaded.

, 5 Mar 2013
Rate this:
Please Sign up or sign in to vote.
This sample demonstrates how to pass data (variables) from code behind to JavaScript.


The sample demonstrates how to pass variables from a code behind to JavaScript after the page is loaded.

Also, it shows how to create hierarchical objects in JavaScript so you can start organizing the variables you get in the way that is more structured and that become more readable.

Using the code

Code is ready to use and only requires Visual Studio.

The code behind:

In the string format below, five variables are being passed in the signature of the JavaScript function Obj_SubObjInit. In this example, the variables are the value from the TextBox1.Text  and 4 integers such as 2,3,4,5. (it could be any code behind data per say)

The only fact on this call is that, the page is already loaded and the JavaScript of the page already loaded as well.

when the user clicks on the button1, then the variables are passed. 

It will be useful when you want JavaScript to work with some data from the code behind after the page is loaded. 

// Pass the variables to javascript
protected void Button1_Click(object sender, EventArgs e)
  string script = string.Format("setTimeout('Obj_SubObjInit(\"{0}\",\"{1}\",
    \"{2}\",\"{3}\",\"{4}\");',5);", TextBox1.Text, 2, 3, 4, 5);         
  ScriptManager.RegisterStartupScript(this, this.GetType(), "uniqueKey", script, true);


Here is the JavaScript area.

Here, I am showing how to created JavaScript variables within JavaScript variables just to get a sense of organization and structure. It is not part of the main reason for this tip. However, since the tip to pass variables is so straight forward,  when I got the variables in the JavaScript function, I am adding them in structured JavaScript variables, in a organized way (just for demonstration purpose).

<%@ Page Language="C#" AutoEventWireup="true" 

  CodeBehind="Javascript.aspx.cs" Inherits="ImageResize.Javascript" %>

<%@ Register Assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

    Namespace="System.Web.UI" TagPrefix="asp" %>

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


<html xmlns="">
<head runat="server">
    <script type="text/javascript">
        //this function will be initialized from code behind and in this
        // examples 5 variables will be available in JavaScript from the code behind
        function Obj_SubObjInit(vA, vB, vC, vD, vE) {

            var jsContainer;
            var VariablesX;
            var VariablesY;

            //code below gets the two above variables and make them part of the first variable.
            jsContainer =
            {"VariablesX" : VariablesX
            ,"VariablesY": VariablesY
            //now fill in the second variable
            jsContainer.VariablesX =
		    { "Var1": vA
		    , "Var2": vB

            //now fill in the third variable
            jsContainer.VariablesY =
		    { "Var3": vC
            , "Var4": vD
            , "Var5": vE

            //now read it. *** MAIN PURPOSE OF THIS IS TO ORGANIZE AS YOU 
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <asp:Button ID="Button1" runat="server" 

          Text="Initialize My JavaScript Variables" onclick="Button1_Click" />
        <br />
        <asp:TextBox ID="TextBox1" runat="server" Text="This is in code Behind"></asp:TextBox>


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


About the Author

Technical Lead Financial Institution
United States United States
Technology Developer with 15 years of experience in the IT industry, mostly spent with financial applications and financial business models. Played various roles such as leadership, management, system architect, database modeling and development, middle tier code and development, GUI, Java Script, C#, VB, SQL Server, Sybase Power Design, Red Gate, Scribe, Metastorm, Telerik, Infragistic, Aspose, Silverlight, Visual studio 2010 and others.

You may also be interested in...


Comments and Discussions

GeneralMissing file Pin
Smitha Vijayan5-Mar-13 3:22
staffSmitha Vijayan5-Mar-13 3:22 
GeneralRe: Missing file Pin
Marcio_Coelho5-Mar-13 3:30
memberMarcio_Coelho5-Mar-13 3:30 
GeneralRe: Missing file Pin
Marcio_Coelho5-Mar-13 3:38
memberMarcio_Coelho5-Mar-13 3:38 
GeneralRe: Missing file Pin
Marcio_Coelho5-Mar-13 3:44
memberMarcio_Coelho5-Mar-13 3:44 
GeneralRe: Missing file Pin
Smitha Vijayan5-Mar-13 3:58
staffSmitha Vijayan5-Mar-13 3:58 

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
Web03 | 2.8.171017.1 | Last Updated 5 Mar 2013
Article Copyright 2013 by Marcio_Coelho
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid