Isaac Kleinman
Isaac Kleinman

Reputation: 4422

Accomplishing Client-side Work During Page Initialization

In order to complete the initialization of an entry-point page, I need some data which can only be obtained through a third-party via client-side JavaScript. Hence, I need some sort of temporary page to be served which will run the JavaScript code before the target page gets rendered. How can I accomplish this?

My Server-side code, MyPage.aspx.vb may look something like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
    ...
    ...
    // Read data from DB which gets written via client-side 
    // JavaScript interacting with web service
End Sub

Upvotes: 0

Views: 96

Answers (3)

addy2601
addy2601

Reputation: 387

I would recommend using jQuery AJAX instead of Temp.aspx,

Step 1. Get the data from third-party via client-side JavaScript.

Step 2. Using jQuery AJAX, get the data from from your DB which gets written via client-side & render it.

Upvotes: 0

Mehran Hatami
Mehran Hatami

Reputation: 12961

Create two different pages your target page which here is MyPage.aspx, and as you yourself have mentioned, a temp page like Temp.aspx. Use a flag to check if your client-side task is done, in the session scope. like:

Session["AjaxCallIsDone"]

in your your MyPage Page_Load method, check if it is true:

If(Session["AjaxCallIsDone"] == null ||
   !(Boolean)Session["AjaxCallIsDone"])
    Response.Redirect ("Temp.aspx");

Ok then, now it goes to the temp page if your ajax call hasn't been done yet.

using jQuery or raw javascript, create your ajax call in the temp page:

request = $.ajax({
    url: "third-party-server-url",
    type: "POST",//or may be GET
    data: yourdata
});
request.done(function (response, textStatus, jqXHR){
    //your client-side task is done
});

create a Handler or Generic Handler to let client inform the server about the ajax call and to set the AjaxCallIsDone flag, do this in your handler method;

Session["AjaxCallIsDone"] = true;

then in your request callback create another ajax call to call that Handler, then in handlerRequest callback function do your redirect like this:

request = $.ajax({
    url: "third-party-server-url",
    type: "POST",//or may be GET
    data: yourdata
});
request.done(function (response, textStatus, jqXHR){
    //your client-side task is done
    var handlerRequest = $.ajax({
        url: "your-handler-url",
        type: "GET"
    });
    handlerRequest.done(function (response, textStatus, jqXHR){
        document.location="MyPage.aspx";
    });
});

and now all is done.

Upvotes: 1

gfrobenius
gfrobenius

Reputation: 4067

Some more specifics / example code of what you got so far would be nice.

If you are using jQuery then you could use the getScript method...

$.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {
  console.log( data ); // Data returned
  console.log( textStatus ); // Success
  console.log( jqxhr.status ); // 200
  console.log( "Load was performed." );
  //now do what you need to do
});

http://api.jquery.com/jquery.getscript/

Hope that helps.

Upvotes: 0

Related Questions