title: How to Embed C# code correctly inside JavaScript Block contained within a C#.net Page?
body: <p>I am basically merging two features of my Google map web app. but I need the co-ordinates which are accessible by backend so I have made a <code>Data Table</code> and it is then passed on to the javascript.</p>
  
  <p>But as I was merging the code I am facing some syntax problem (most likely).</p>
  
  <p><strong>Update:</strong> Error that I get is <code>Operator '+' cannot be applied to operands of type 'string' and 'method group'</code></p>
  
  <p><strong>Update #2 :</strong> See below for the Page_Load code (creation of the data table &amp; main code execution if fired from that code)</p>
  
  <p><strong>Update #3:</strong> Added Main Page's code. Error now I am getting: <code>Microsoft JScript runtime error: The value of the property 'initialize' is null or undefined, not a Function object</code></p>
  
  <p>At Line <code>var myLatLng = new google.maps.LatLng(" tblPoints.Rows[i][0].ToString @", " + tblPoints.Rows[i][1].ToString + @");</code> I am getting some strange error related to openeing/closing of @" &amp; ";</p>
  
  <p>Any answer/suggestion is appreciable..Thanks..</p>
  
  <p>Code for the Main Page (where the JS func is called):</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head id="Head1" runat="server"&gt;
      &lt;title&gt;maps integ&lt;/title&gt;
      &lt;script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;div&gt;
          &lt;/div&gt;
          &lt;div id="map_canvas" style="width: 100%; height: 100%;"&gt;&lt;/div&gt;
          &lt;script type="text/javascript"&gt;
              window.onload = function (e) {
                  initialize();
              }
          &lt;/script&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Code for the <code>Page_Load</code> in Main Page:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              DataTable dt = new DataTable();
              dt.Columns.Add(new DataColumn("Latitude"));
              dt.Columns.Add(new DataColumn("Longitude"));
              dt.Columns.Add(new DataColumn("Info"));
  
              DataRow row = dt.NewRow();
              row["Latitude"] = 28.483109;
              row["Longitude"] = 77.107756;
              row["Info"] = "INFO #1";
              dt.Rows.Add(row);
  
              row = dt.NewRow();
              row["Latitude"] = 28.483243;
              row["Longitude"] = 77.107624;
              row["Info"] = "INFO #1";
              dt.Rows.Add(row);
  
              row = dt.NewRow();
              row["Latitude"] = 28.483293;
              row["Longitude"] = 77.107579;
              row["Info"] = "INFO #1";
              dt.Rows.Add(row);
  
              row = dt.NewRow();
              row["Latitude"] = 28.483359;
              row["Longitude"] = 77.107536;
              row["Info"] = "INFO #1";
              dt.Rows.Add(row);
  
              row = dt.NewRow();
              row["Latitude"] = 28.483559;
              row["Longitude"] = 77.107273;
              row["Info"] = "INFO #1";
              dt.Rows.Add(row);
  
              //row = dt.NewRow();
              //row["Latitude"] = 28.4804;
              //row["Longitude"] = 77.1251;
              //dt.Rows.Add(row);
  
              js.Text = GPSLib.PlotGPSPoints(dt);
          }
      }
  </code></pre>
  
  <p>Code for the class file:</p>
  
  <pre><code>public static class GPSLib
  {
      public static String PlotGPSPoints(DataTable tblPoints)
      {
          try
          {
              String Locations = "";
              String sJScript = "";
              int i = 0;
  
  
              foreach (DataRow r in tblPoints.Rows)
              {
                  // bypass empty rows 
                  if (r["latitude"].ToString().Trim().Length == 0)
                      continue;
  
                  string Latitude = r["latitude"].ToString();
                  string Longitude = r["longitude"].ToString();
  
                  // create a line of JavaScript for marker on map for this record 
                  Locations += Environment.NewLine + @"
                  path.push(new google.maps.LatLng(" + Latitude + ", " + Longitude + @"));
  
                  var marker" + i.ToString() + @" = new google.maps.Marker({
                      position: new google.maps.LatLng(" + Latitude + ", " + Longitude + @"),
                      title: '#' + path.getLength(),
                      map: map
                  });";
  
                  i++;
  
              }
  
              // construct the final script
              // var cmloc = new google.maps.LatLng(33.779005, -118.178985);
  
              // map.panTo(curmarker.position); //zooming on current marker's posn.
              // map.panTo(myOptions.getPosition());
              sJScript = @"&lt;script type='text/javascript'&gt;
  
              var poly;
              var map;
  
              function initialize() {
                  var cmloc = new google.maps.LatLng(28.483243, 77.107624);
                  var myOptions = {
                      zoom: 19,
                      center: cmloc,
                      mapTypeId: google.maps.MapTypeId.ROADMAP
                  };
  
                  map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
  
  
                  var polyOptions = {
                      strokeColor: 'blue',
                      strokeOpacity: 0.5,
                      strokeWeight: 3
                  }
                  poly = new google.maps.Polyline(polyOptions);
                  poly.setMap(map);
  
                  var path = poly.getPath();
  
                 " + Locations + @"
  
                      }
  
              final initPoints(){
           //  var map;
              var infowindow;
  
              var mapOptions = {
                  zoom: 19,
                  mapTypeId: google.maps.MapTypeId.ROADMAP,
                  center: centr
               };
  
              map = new google.maps.Map(document.getElementById('map_canvas'),
              mapOptions);
              infowindow = new google.maps.InfoWindow();
              drop();
  
  
              }
  
          function drop() {
              for (var i = 0; i &lt; "+ tblPoints.Rows.Count + @"; i++) {
          var myLatLng = new google.maps.LatLng("+ tblPoints.Rows[i][0].ToString+ @", "+ tblPoints.Rows[i][1].ToString + @");
          var mark = new google.maps.Marker({
              position: myLatLng,
              map: map,
          });
      iWindow(mark, "+ tblPoints.Rows[i][2].ToString +@");
     }
  }
  
  function iWindow(marker, title) {
      google.maps.event.addListener(marker, 'click', function () {
  
          infowindow.setContent(title);
          infowindow.open(map, marker);
      });
  }
                  &lt;/script&gt;";
              return sJScript;
          }
          catch (Exception ex)
          {
              throw ex;
          }
      }
  }
  </code></pre>