0

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

2
  • you want one map or multiple maps? Commented Apr 24, 2015 at 10:33
  • one map only. the objective is to access the details of some places. Commented Apr 26, 2015 at 22:23

2 Answers 2

2

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++;
}
Sign up to request clarification or add additional context in comments.

3 Comments

It is the same. Only the last line is executed.
check if there is any problem with your GetInfoGMaps method.. May be each iteration is overwriting the previous one.
I updated my code. I used console.log and I think that my error is in radarSearch function, but i dont understand why.
0
  • 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.

4 Comments

It is the same. The strString appears with the correct number of iterations, but performs only the last row. Example of my strString: GetInfoGMaps ('Lido Hotel Durres' 41.322681427002,19.4581604003906,25); GetInfoGMaps ('Hotel Apollonia Durres' 41.3135414123535,19.4765796661377,33); GetInfoGMaps ('Mak Albania Resort Durres' 41.2314605712891,19.5132293701172,56);
Hard to see so. I would console.log my javascript to try to debut why it does not work like wanted.
I had a try with your script but I encounter an error : can't find lodging_name. I don't see yours but I used this service :<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
Sorry, my mistakes. I updated my code, but yes, it is the Google Places script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.