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

Server events 2

Although the predefined server events are most common, the case can occur that we want to do something different. For example, to handle a non-predefined event, or to redefine a predefined event from which we want different data.

For this, we have the event ServerEvent. In order to make it work, we will have to go through the following steps:

1.- Javascript
We must add Javascript and use the Javascript class 'serverEvent' to send the information to the server. As we see in the example, an easy way to create the Javascript is by using addListener.
In the examples we see that to use the class 'serverEvent' is very simple, because we just initialize it with the name we want to give to the event (eventName) and carry on adding as many arguments as we wish. Finally it is necessary to call the function ' send() ' and the information is sent to server.

2.- Server At the server, we work with the event ServerEvent, as we do with any other events.
There we will have the GAjaxServerEventOtherArgs in which we can find 2 properties (in addition to common "who" and "point"):
  • eventName: string that contains the name of the event that we did have defined. It is is very useful to, like in the example, distinguish between several different events
  • eventArgs: it is an array of string that contains all the arguments that we have added in Javascript.
in the example we see as we defined two events: "Click" and "InfoWindowClose".
The first handles the event click of the map; we have already seen that this event was one of the predefined ones, but here we have added an argument we did have before: the size of the map.
And as to "InfoWindowClose", it handles a non-predefined event. We then see we can handle any event the map produces, from markers or whatever it is.



Code.aspx
<cc1:GMap ID="GMap1" runat="server" enableServerEvents="true"
    OnServerEvent="GMap1_ServerEvent" />

Code.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
      GMap1.addListener(new GListener(GMap1.GMap_Id, GListener.Event.click,
       string.Format(@"
       function(overlay, point)
       {{
          if (!point) return;
          var ev = new serverEvent('Click', {0});
          ev.addArg({0}.getSize().width);
          ev.addArg({0}.getSize().height);
          ev.addArg(point.lat());
          ev.addArg(point.lng());
          ev.send();
       }}
       ", GMap1.GMap_Id)));
      GMap1.addListener(new GListener(GMap1.GMap_Id, GListener.Event.infowindowclose,
       string.Format(@"
       function()
       {{
          var ev = new serverEvent('InfoWindowClose', {0});
          ev.addArg('My Argument');
           ev.send();
       }}
       ", GMap1.GMap_Id)));
   }
}

protected string GMap1_ServerEvent(object s, GAjaxServerEventOtherArgs e)
{
    string js = string.Empty;
    switch (e.eventName)
    {
    case "Click":
       GLatLng latlng = new GLatLng(
       Convert.ToDouble(e.eventArgs[2], new System.Globalization.CultureInfo("en-US", false)),
       Convert.ToDouble(e.eventArgs[3], new System.Globalization.CultureInfo("en-US", false)));
       GInfoWindow window = new GInfoWindow(latlng, string.Format("Window Size (px): ({0},{1}). Close Me.", e.eventArgs[0], e.eventArgs[1]));
       js = window.ToString(e.who);
       break;
    case "InfoWindowClose":
       js = string.Format
       ("alert('{0}: {1} - {2} - {3}')", e.eventName, e.point, e.eventArgs[0], DateTime.Now);
       break;
    }
    return js;
}
Powered by Subgurim.NET