DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: assigning a value to JavaScript variable in codebehind

  1. #1
    Join Date
    Oct 2007

    assigning a value to JavaScript variable in codebehind

    Can anybody answer that how can i use assgin the value to the Javascript Variable in my codebehind in .Net.
    Actually I am writing a function in javascript in which i want to access the value of one of the property defined in the code behind. So i need to assign the value of the property to javascript variable and once i will get the value i will be further processing that in my javascript code. Plz answer asap.

    Thanks in advance.

  2. #2
    Join Date
    Aug 2006
    Create a <asp:HiddenField/> control on the web page and set it's value to the value of the variable in the code behind. Assuming you have named the control "myval", access it using the following specifier in JavaScript.

  3. #3
    Join Date
    Jul 2009

    assigning a value to javascript variable from label control

    I hope you can help me too, since I've tried so many ways available on the net and I still can't get it right.

    My problem is I can't pass the value of a Label control to the javascript variable. The Label control dynamically gets value from DB, and i managed to display the value in this label called "lblmap".

    I would like to pass the value of "lblmap" to a javascript variable called "address". I've tried the code in <script> as below:

    1. var address = document.getElementById('<%=lblmap%>').value;
    2. var address = document.getElementById('<%=lblmap%>').innerHTML;
    3. var address = document.getElementById('lblmap').value;
    4. var address = document.getElementById('lblmap').innerHTML;
    5. var mapaddress = '<%=lblmap%>';
    var address = document.getElementById(mapaddress).value;

    none of the above works, when i run the project, IE complaint the "object doesn't support this property or method". I'll need to assign an address (e.g. 123 Main Street, Sydney NSW 2000, Australia) to the address variable, the google map i'm working on will automatically point and mark based on this address variable.

    Currently if i assign a static (hard coded) value to this address variable, the map will display correctly (e.g. var address = "123 main street, sydney NSW 2000, Australia).

    I hope you can understand my question and wish you could help.

  4. #4
    Join Date
    Aug 2006
    I believe the only thing your code is missing is a call to the ClientID property. Try changing the line:
    var address = document.getElementById('<%=lblmap%>').innerHTML;
    var address = document.getElementById('<%=lblmap.ClientID%>').innerHTML;

    See the attached project for a working example.

    Arley Schrock
    Attached Files Attached Files
    Arley Schrock
    Email Me

  5. #5
    Join Date
    Aug 2006
    One more thing - if you didn't put the code that is failing inside of a javascript function, the statements will be executed while the page is loading and before the elements are accessible. If that is the case, move them into a function and handle the onload event of your body element.
    Last edited by arleyschrock; 07-24-2009 at 03:45 PM.
    Arley Schrock
    Email Me

  6. #6
    Join Date
    Jul 2009
    Thank you for you advice, please see the code I attached below.

    the currently situation is if I hard coded the value to var address, it will work whether it's outside the function or inside the function.

    when I use the document.getElement line INSIDE the function and test with an alert, the alert actually popup with the correct address. however the line "place = response.Placemark[0];" in the next function, it return an error in IE "Placemark.0" is null or not an object. Please note that this message doesn't appear when I hard coded the address value.

    I'm not sure what to do, but i have a feeling that I need to convert the document.getElement value into a string or something, can you advice please?

    		<script type="text/javascript" language=javascript>
    		var geocoder;
    		var map;
    		//var address="new york, usa";
                              //var address = document.getElementById("<%=lblmap.ClientID%>").innerHTML;
    		function load()
    			//var address = document.getElementById("<%=lblmap.ClientID%>").innerHTML;
    			var address="new york, usa";
    			map = new GMap2(document.getElementById("map"));
    			geocoder = new GClientGeocoder();
    			geocoder.getLocations(address, addToMap);
    		function addToMap(response)
    			place = response.Placemark[0];
    			point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    			marker = new GMarker(point);
    	<body onload="getTime();load();" onunload="GUnload()">

Similar Threads

  1. Assigning XSL value to Javascript
    By teamsite in forum XML
    Replies: 1
    Last Post: 08-25-2005, 02:06 PM
  2. Replies: 0
    Last Post: 11-14-2001, 08:02 AM
  3. Sample Sites.
    By Murray Foxcroft in forum Web
    Replies: 5
    Last Post: 11-02-2000, 03:42 AM
  4. Pass asp variable to javascript
    By suresh in forum ASP.NET
    Replies: 9
    Last Post: 06-16-2000, 04:44 PM
  5. Replies: 1
    Last Post: 04-26-2000, 11:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center