0

I've got a variable which is set in a final script tag on page load, as this is required due to accessing the DOM (using a 3rd party library). I want to access my variable, however I get "undefined" when accessing it via C# (ASP Web Application).

How can I access this?

If I run the following from C#:

ClientScript.RegisterStartupScript(GetType(), "hwa", "hello();", true);

I get the following error: https://i.stack.imgur.com/veiLB.png

My hello function is:

function hello() {
 alert(mapData);
}

This mapData variable is set in a script tag at the end of the body, my original code was to call a JS function AFTER the site loaded, however it comes up as undefined via C# yet shows up if I use the Web Console.

Full Script:

function createMap() {
mapData = L.map('map', {
    center: [20.0, 5.0],
    minZoom: 2,
    zoom: 5
});

L.mapData = mapData;

L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
    subdomains: ['a', 'b', 'c']
}).addTo(L.mapData);
}

function createMarker(long, lat, type) {
    alert(L.mapData);
    L.marker([long, lat], { icon: L.icon({ iconUrl: 'Content/img/' + type + '.png', iconSize: [50, 60], iconAnchor: [22, 94], popupAnchor: [-3, -76], }) }).addTo(L.mapData);
}

function hello() {
    alert(mapData);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Charlie J
  • 1
  • 2
  • check this out https://stackoverflow.com/questions/12233775/how-to-get-javascript-variable-value-in-c-sharp – Clint Jan 26 '20 at 16:39
  • Thanks @Clint, sorry I didn't clarify the variable is an object. – Charlie J Jan 26 '20 at 16:47
  • @Clint it does, but I need to store a object that's the only thing. – Charlie J Jan 26 '20 at 17:43
  • I do not understand what you meant by that, pls elaborate – Clint Jan 26 '20 at 17:44
  • @Clint I am using a JS variable object that has map related data (using leaflet js), I need to be able to access that via C#. C# shows it as undefined while the JS console does. – Charlie J Jan 26 '20 at 17:49
  • have not tried that, have you thought of other work arounds such as logging the variable to a log then reading it from c# etc ? – Clint Jan 26 '20 at 17:54
  • @Clint how would this work? – Charlie J Jan 26 '20 at 18:08
  • @CharlieJ: Welcome to StackOverflow. Keep in mind that all **ASP.NET Web Forms** is doing is generating the markup on the server, which includes any resources such as JavaScript, and returning it to the browser. The JavaScript itself, however, is executed _afterwards_ in the browser and has no awareness of the C# code. By the point the JavaScript starts, your C# code has already finished. As such, if you want C# to be aware of data assembled by JavaScript, you need to send it _back_ to the server, using either a web service call with AJAX or a post back with a form (as done in Clint's link). – Jeremy Caney Jan 26 '20 at 23:36
  • @JeremyCaney thank you for the warm welcome! How would I go ahead with this? – Charlie J Jan 27 '20 at 08:10
  • Does this answer your question? [how to get Javascript variable value in C#](https://stackoverflow.com/questions/12233775/how-to-get-javascript-variable-value-in-c-sharp) – Jeremy Caney Sep 22 '22 at 17:52

1 Answers1

0

It looks like your hello method is being called before mapData is being instantiated.

I do not see where it is being instantiated but you could change your register script as follows:

ClientScript.RegisterStartupScript(GetType(), "hwa", "createMap(); hello();", true);

Note that you may want to add a hello callback to your create function.

More info as to what you are trying to do here may also be helpful.