title: Calling ajax function as a variable declared in asp.net master page
body: <p>I am trying to set some session variables using Ajax function call. The function is declared as a variable in ASP.Net master page since it will have to be called from different pages.</p>
  <p>In response to a drop down selection change I call this function and since I need to wait until it has done its work before using some f the values I used $.when. But the function is hit after drop down selection change event is completed. I guess this is how asp.net orders function executions (main page before master page). Any idea how to resolve this? I need to be able to call this from any other page, hence adding it to master page.</p>
  <p>I tried taking it out of master page and put it in a separate js file and include the file, but that didn't help either, same thing happens: page first, js is run after which is too late.</p>
  <p>This is in master page:</p>
  <pre><code>setISCSession = function(isc) {
      $.ajax({
          type: &quot;POST&quot;,
          dataType: &quot;json&quot;,
          contentType: &quot;application/json; charset=utf-8&quot;,
          url: &quot;/WebService/idc.asmx/SetISCSessionVars&quot;,
          cache: false,
          data: JSON.stringify({ ISC: isc }),
      }).done(function (result) {debugger
          if (!result || result.d === &quot;&quot; || result.d.length == 0) {
          }
          else {
              var iscData = JSON.parse(result.d)[0];
              siteTZ = iscData.SITE_TZ;
              utcOffset = iscData.UTC_OFFSET;
              ...
              // this is just window.sessionStorage.setItem(key, value)
              $.session.set('Site_TZ', siteTZ);
              $.session.set('UTC_Offset', utcOffset);
              ...
          }
      }).fail(function (jqXHR, textStatus, errorThrown) {debugger
          alert(textStatus + ' - ' + errorThrown + '\n' + jqXHR.responseText);
      });
  }
  </code></pre>
  <p>In some page:</p>
  <pre><code>$(document).on('change', '#ddlSelISC', function () {debugger
      isc = $(this).val();
      $.when(setISCSession(isc))
          .done(function (r1) {debugger
              var tz = $.session.get('Site_TZ');
              $('#hfSiteTZ').val(tz);
              $('#lblOffset').text($.session.get('UTC_Offset'));
              ...
          });
  });
  </code></pre>