简体   繁体   中英

Execute javascript function in code behind (c#) multiple times

I have an datatable and for each row i need execute a javascript function (same javascript function, but with diferent values).

For each row, it executed a javascript function and it is returned the result to the code behind (trought web method).

But, with my actual code the function it only execute one time (first row of table).

I have this: c#

 string strScript = "";
foreach (DataRow row in dtLodgings.Rows)
{
    strScript = strScript + "\n" + "GetInfoGMaps('" + row["Name"].ToString() + "'," + row["Latitude"].ToString().Replace(",", ".") + "," + row["Longitude"].ToString().Replace(",", ".")+ ");";
}
  strScript = strScript.Replace("'", "\'");
  ClientScript.RegisterStartupScript(GetType(), "GetInfoGMaps", strScript, true);

javascript

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>
var map, service;
function GetInfoGMaps(name, latitude, longitude) {
    var loc= null;
    if (latitude != "" && longitude != "") {
        loc  = new google.maps.LatLng(latitude, longitude);
    }

    map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: loc,
        zoom: 15
    });

    service = new google.maps.places.PlacesService(map);

    var request = {location: loc, radius: 50000, keyword: name};

    service.radarSearch(request, callback); // i got the data and i sent to c# trought ajax
}

c#

[WebMethod]
public static void GetInfoGMaps(InfoMaps lodg)
{
   if (lodg != null)
   {
      // the data is saved in database
   }
}

I tried to execute the strString in console (chrome web browser), but I getand undefined and I don't understand why. It seems that when I run radarSearch function gives undefined.

GetInfoGMaps('Mak Albania Resort Durres',41.2314605712891,19.5132293701172);
GetInfoGMaps('Mak Albania Resort Durres',41.2314605712891,19.5132293701172);
GetInfoGMaps('Mak Albania Resort Durres',41.2314605712891,19.5132293701172);

Anyone can help me?

Thanks

As per the documentation , each startup script is uniquely identified by its key and its type. Scripts with the same key and type are considered as duplicates. Add unique values to each key like this..

int i = 0;
foreach (DataRow row in dtTable.Rows) {
    ClientScript.RegisterStartupScript(GetType(), "GetInfoGMaps" + i, "GetInfoGMaps('" + row["Name"].ToString() + "'," + row["Latitude"].ToString().Replace(",", ".") + "," + row["Longitude"].ToString().Replace(",", ".")");", true);
    i++;
}
  • ClientScript should be outside foreach loop, since you register GetInfoGMaps N rows times.

  • foreach to create your script string first.

    • clean/escape ' char to avoid breaking your javascript
  • it will give another benefit : you can breakpoint your script string before sending client.

Example :

string strScript = "";

foreach (DataRow row in dtTable.Rows) {
   strScript = strScript + "\n" +  "GetInfoGMaps('" + row["Name"].ToString() + "'," + row["Latitude"].ToString().Replace(",", ".") + "," + row["Longitude"].ToString().Replace(",", ".")");";

}
//clean/escape ' char to avoid breaking your script
clientScript = clientScript.Replace("'", "\'")

// now it may be ok :
ClientScript.RegisterStartupScript(GetType(), "GetInfoGMaps", clientScript, true);

Can't check my code on visual studio (on my Mac right now), so it may contain errors but idea is here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM