Rodolfo Redolfi
Rodolfo Redolfi

Reputation: 85

asp.net literal control not rendered after postback

I'm trying to use Google Maps in order to show some locations extracted from a database. It's al list of vending point, grouped by a set of promoters that, upon promoter selection, have to be displayed on the map. After successfully using the Subgurim.net library, I decided to move to a Javascript implementation, with the help of https://www.codeproject.com/Articles/36151/Show-Your-Data-on-Google-Map-using-C-and-JavaScrip (the only difference is that I'm using GoogleMaps v3 calls). The javascript insertion works very well on page load, but when I need a postback on a promoter code in order to show his vending points, I can see in the codebehing the data loaded in the literal control, but no markers are rendered on the web page. If I check with Microsoft Edge's development tools the page being generated I always find the literal control substituted with the data loaded during the very first (no postback) page load. The aspx part of the page is very simple, and it's not contained in an UpdatePanel, although an UpdatePanel is used on the page, and I've tried also with EnableViewState true and false on the literal control, with no change.

This is the aspx relevant part :

<div id="mapContent" >
    <asp:Literal ID="mapJs" runat="server" EnableViewState="false" />
    <div id="mapDiv" style="width:100%; height:640px;" />
    <div id="infoDiv">
    ....
    </div>
</div>

This is the part of code that inserts the first part of the script:

StringBuilder map = new StringBuilder();
bool loadPVGiro = false;
double centerMapLat = 37.451669,
    centerMapLong = 15.05263;

map.Append(@"<script type='text/javascript'>" + System.Environment.NewLine);
map.Append("var map;" + System.Environment.NewLine);
map.Append("function infoCallback(infowindow, marker)" + System.Environment.NewLine);
map.Append("{" + System.Environment.NewLine);
map.Append("  return function()" + System.Environment.NewLine);
map.Append("  {" + System.Environment.NewLine);
map.Append("    infowindow.open(map, marker);" + System.Environment.NewLine);
map.Append("  };" + System.Environment.NewLine);
map.Append("}" + System.Environment.NewLine);
map.Append("function mapLoad()" + System.Environment.NewLine);
map.Append("{" + System.Environment.NewLine);
map.Append("var centralPoint = { lat: " + centerMapLat.ToString().Replace(",", ".") + ", lng: " + centerMapLong.ToString().Replace(",", ".") + "};" + System.Environment.NewLine);
map.Append("var map = new google.maps.Map(document.getElementById('mapDiv'), { zoom: 12, center: centralPoint });" + System.Environment.NewLine);
map.Append("}" + System.Environment.NewLine);   // mapLoad close
map.Append("</script>" + System.Environment.NewLine);
map.Append("<script async defer src=\"https://maps.googleapis.com/maps/api/js?key=MY_GOOGLE_MAPS_API_KEY&callback=mapLoad\"> " + System.Environment.NewLine);
map.Append("</script> " + System.Environment.NewLine);

mapJs.Text = map.ToString();

This is the portion of the rendered page (from Edge's development tools):

<div id="mapContent" >
<script type='text/javascript'>
var map;
function infoCallback(infowindow, marker)
{
  return function()
  {
    infowindow.open(map, marker);
  };
}
function mapLoad()
{
  var centralPoint = { lat: 37.451669, lng: 15.05263};
  map = new google.maps.Map(document.getElementById('mapDiv'), { zoom: 12, center: centralPoint });
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=MY_GOOGLE_MAPS_API_KEY&callback=mapLoad"> 
</script> 
<div id="mapDiv" style="width:100%; height:640px;" />
....

Whan a postback occurs, following the selection of a promoter code, the script is fully regenerated with the list of markers, and the literal control is filled with the new script (I can see it only through a breakpoint), but the control on the page remains the first one. Obviously if I force a code during the first page load all the markers are displayed correctly. The infoCallback function inside the script is used only to force the generation of a new InfoWindow for each marker placed on the map. Any help would be greatly appreciated.

Rodolfo.

PS: tried also with ViewStateMode="disabled" on the literal control, without changes.

PS2> tried ClientScript.RegisterClientScriptBlock and ClientScript.RegisterStartupScript, but the script being generated are exactly the same as the literal control. Forgot to mention that the actual page is inside a MasterPage.

Upvotes: 0

Views: 576

Answers (1)

Rodolfo Redolfi
Rodolfo Redolfi

Reputation: 85

The problem seems related to the user tree inside the update panel; clicking on a user generates a postback, but the script injected in the literal control is never updated on the page, only in the codebehind. Removing the updatepanel the script is correctly updated and executed. After I'll finish the modifications needed by the project, I'll try to further investigate on this behaviour.

Upvotes: 1

Related Questions