<%= GetAddPinScript() %>
コード ビハインド ファイル内のメソッドを評価する式があります。このメソッドは JavaScript を生成し、それを返します。
AddPushpin('data', 'more data', numbers, numbers, 'no');
ここで重要なのは'no'
、コード ビハインド メソッドによって挿入される変数であり、残りの JavaScript が一部のコンテンツを表示するかどうかを判断するのに役立つことです (基本的にブール値)。
私が抱えている問題は、JavaScript で生成する正しい変数を決定するために、コード ビハインドのメソッドが、式が評価される前result
にajax 呼び出しを介して送信される変数 (以下のコード) を必要とすることです。ただし、式は常にページの読み込み時に評価されます。
では、ajax 呼び出しが行われる前に式が評価されないようにするにはどうすればよいでしょうか?
Javascript
function displayGeneralInfo(specifics)
{
var callback = AddQueryString(window.location.href, "action", "displayResults");
$.ajax({
url: callback,
type: "POST",
async: false,
data: {
specifics: specifics
}
});
<%= GetAddPinScript() %>
}
および AddPushpin 関数
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);
}
およびコード ビハインド スニペット
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;