I have an expression <%= GetAddPinScript() %> that evaluates a method in my code-behind file. The method generates javascript which it then returns:
AddPushpin('data', 'more data', numbers, numbers, 'no');
The key here is that 'no' is a variable inserted by the code-behind method and helps the rest of my javascript determine if some content should display or not.(basically a bool)
The problem I’m having is that the method in my code-behind needs a variable(result in the below code) that is sent through an ajax call before the expression is evaluated in order to determine the correct variable to generate in the javascript — but the expression always evaluates on the page load.
So how can I prevent the expression from being evaluated before the ajax call is made?
Javascript
function displayGeneralInfo(specifics)
{
var callback = AddQueryString(window.location.href, "action", "displayResults");
$.ajax({
url: callback,
type: "POST",
async: false,
data: {
specifics: specifics
}
});
<%= GetAddPinScript() %>
}
and the AddPushpin function
function AddPushpin(name, description, latitude, longitude, selected) {
// Add a pin to the map
var center = new Microsoft.Maps.Location(latitude, longitude);
var pin = new Microsoft.Maps.Pushpin(center, null);
if(selected !== null || selected!="")
{
if(selected == "yes")
{
infoboxOptions = new Microsoft.Maps.Infobox(center,
{ width: 285,
height: 170,
visible:true,
actions:[{label: 'Associate', eventHandler: associate}]
});
map.entities.push(infoboxOptions);
}
}
map.entities.push(pin);
}
and the code-behind snippet
public string GetAddPinScript()
{
foreach (Location location in foo(x => !string.IsNullOrWhiteSpace(x.Longitude) && !string.IsNullOrWhiteSpace(x.Latitude)))
{
selected = "no";
if (!result.IsNullOrEmpty())
{
if (location.MapPinDescription.IndexOf(result) > 0)
selected = "yes";
}
pins.Add(string.Format("AddPushpin('{0}', '{1}', {2}, {3}, '{4}');",
location.etc1("'", @"\'"), location.etc2("'", @"\'"), location.etc3, location.etc4, selected));
string retVal = string.Join("\n", pins.ToArray());
return retVal;
You cannot make it wait. The javascript is executed on the client and this function runs on the server. Asp.net does all the binding before it sends data to the client so there is no way you can say wait till some code runs on client and then run this function. I suggest you call this function when the ajax call is made to the server. Return some html from this function and then include this in your html in the success function.