10

How do I register a Javascript variable on the server side (backend) and access it on the client side (Javascript file), without a hidden field, Literal, etc.?

4 Answers 4

8

You can use the RegisterClientScriptBlock-Function from the Page's ClientScriptManager.

Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "initMyClientVariable", "var myClientVariable=null;", True)

EDIT: according to your new informations, that you want to register a client array, use ClientScriptManager's RegisterArrayDeclaration Method.

VB.Net example:

Dim myArrayValue As String = """1"", ""2"", ""text"""
Page.ClientScript.RegisterArrayDeclaration("myClientArray", myArrayValue)

According to the new information in my comments that you need access to that variable from an external js-file: you should pass the js-array as argument to the function in the js-file. For example:

callFunctionInJsFile(checkBoxes);
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for the Information. I did the same way , but i stuct up with one more issue . That variable registered appears in <script type="text/javascript"> //<![CDATA[ var myClientVariable=System.String[];//]]> </script> . (1) when i try to acceess that variable(myClientVariable) its throws message Object expected.How to access that variable to proceed further. (2) For myClientVariable i want to assign some variable in code behind , how can assign value . Pls assist me
Ya i tried that Page.ClientScript.RegisterArrayDeclaration("myClientArray", myArrayValue). But how to access that array in the Javascript file so that i can work on that array. Kindly help me out
You can access that array from client-side directly. View page-source in browser to see that the variable is available and intialized correctly.
Hi , i tried following Method : In Page_load() of Asp.net (C# Code) String myArrayValue = "abc"; Page.ClientScript.RegisterArrayDeclaration("myClientArray", myArrayValue); But in the JavaScript Side : when it comes to CDATA Block like :::: <script type="text/javascript"> //<![CDATA[ var myClientArray = new Array(abc); //]]> < it throws Error : myClientArray is undefined. kindly help me to solve this issue . If possible quote me an example
Also suggest how to handle in Masterpages , AjaxUpdate panels (i m using both ). pls suggest me
|
4

You can put the following code in .aspx file ...

<script type="text/javascript" >
    var date1 = "<%: DateTime.Now %>";
    var date2 = "<%= DateTime.Now %>";
</script>

<%:  %> works under ASP.NET 4

Comments

3

You can put a literal in the xml portion of the code and assign that literal some text:

myLiteral.Text = "<script language=\"javascript\">var myVar = 24;</script>";

This makes myVar globally available on the client side once it's rendered. You can also use the ClientScriptManager object to use Asp.Net to inject scripts and variables.

Comments

1

First place an <asp:Literal ID="Literal1" runat="server"></asp:Literal> tag in the <head> of your .aspx file.

Then in the server side code in your .aspx.cs file, do something like Literal1.Text = "<script type=\"text/javascript\">var timer = 3600</script>" and you've got yout javascript variable called timer.

That's it. Have fun!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.