Before I explain my situation, please have a look at the very important notice!!
1.My javascript is not embedded in .aspx file, so something like
var strMessage = '<%= str%>';
StartGeocoding(strMessage);
is not working (I tried a lot, but if you can improve it please let me know)
2.Also, I already used
Page.ClientScript.RegisterStartupScript( , , , )
function, so I think I am not allowed to use twice.
================================================================
So, here
In “Location.js” (seperated from .aspx)
function LoadMap(count) {
var asdf = (something variable from code-behind);
var counts = count;
myMap = new VEMap("mapDiv");
myMap.LoadMap();
StartGeocoding(asdf); // it will show the map with location info of "asdf"
}
In code behind, there is something
public string blahblah = "1 Yonge Street"
Basically, I will get the address from code behind, and I will show it using javascript.
If you (my LORD!) can teach me how to get the variable in C# from javascript, that would be very appreciated!!!
If you guys wanna challenge, here is bonus(?) question
actually, I will show the multiple location in a map. Therefore, instead of having one string “blahblah”, I might have a list of string
<list>Locationlist //not array
So, ‘count’ in LoadMap() function will recognize how many entries I have. How to get each location information from javascript? is this possible? Any idea?
Here’s what I had in mind.
On code-behind, let’s say the Page_Load method, you can have the following code:
At this point you can acccess the registered array in Location.js:
Some remarks:
To use the StringBuilder class you need to add a “using System.Text” at the top of the file;
the System.Web.HttpUtility.JavaScriptStringEncode is needed to ensure that the serverside strings are correctly encoded (taken from Caveats Encoding a C# string to a Javascript string). It is available only in .Net 4, from what I understand.
if you have a ScriptManager on the page it is best to use the RegisterStartupScript on the ScriptManager rather then the method in the Page.ClientScript
I cannot test the above code right now but you should get the basic ideea.