Help us translate this website and improve this translation and earn free licenses!
Anonymous user  |  Log in  |  Create Account

How to...

ASP.NET Postback

Alternately to the GMaps control native serverEvents, we can force them to execute an ASP.NET tradicional PostBack. To achieve this, we only have to show it with serverEventsType="AspNetPostBack" (As always, enableServerEvents must be on).

One of the most important differences is that the only way of interaction between the native serverEvent and the map is returning a string with Javascript. We can return the Javascript using the PostBack, but we can also interact directly with the map (see the example).

We can also access to the session variables, Viewstate, etc.

It also works well placed inside an UpdatePanel.

However, the native serverEvent are much more efficient and speedy, so it's recommended to use them whenever possible.




Code.aspx
<cc1:GMap ID="GMap1" runat="server" serverEventsType="AspNetPostBack" enableServerEvents="true" OnClick="GMap1_Click" OnMarkerClick="GMap2_MarkerClick" />
Code.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.setupMap();
    }

    if (GMap2.IsAjaxPostBack)
    {
        // GMaps Ajax PostBack
    }
}

private void setupMap()
{
    GLatLng latLng = new GLatLng(40,10);

    GMap2.GCenter = latLng;

    GMarker marker = new GMarker(latLng);
    GMap2.Add(marker);
}

protected string GMap2_Click(object s, GAjaxServerEventArgs e)
{
    GMap2.GZoom = 1;

    GInfoWindow window = new GInfoWindow(e.point, "Clicked");
    GMap2.Add(window);

    return string.Empty;
}

protected string GMap2_MarkerClick(object s, GAjaxServerEventArgs e)
{
    GLatLng latLng = new GLatLng(40, 10);

    GMap2.GCenter = latLng;
    GMap2.resetInfoWindows();

    GMap2.GZoom = 6;
    return string.Empty;
}
Powered by Subgurim.NET