Sunday, January 21, 2007

GMap asp.net control , geocoding, and asynchronous page.

I have been playing the GMap asp.net control recently, and loved it. I didn't have much experience in asp.net server control, and this gave me a good start to get into some details.

I wanted to use the geocoder functionalities of the GMap, and did some simple research on that. I firstly tried to use some call back functions. In the class "GServerGeoCoder" I created, i have a match function which sent a Http request over to the server , like this:

public void Match()
{
IAsyncResult result = (IAsyncResult)geoCoderHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback), geoCoderRequestState);
}

And in the RespCallBack, I would raise an event GeoCoderResponeReceived which the aspx page registered. I thought it was a good approach, but the problem is that the server control won't render itself in the callback.

The thread which processed the geocoder request would go ahead to finish the rendering, and it won't wait for the call back result to finish the rending process. And the call back to the aspx page won't cause the server control render itself again. I assume there is a way to do it, but I didn't find it out so far.

Then, I searched some other ways and found out that asp.net actually has a way to do it called "Asynchronous Pages" ,and this is exactly what I looked for. By registering some PageAsyncTask, [Start quoting from Jeff Prosise ]"The page undergoes its normal processing lifecycle until shortly after the PreRender event fires. Then ASP.NET calls the Begin method that you registered using AddOnPreRenderCompleteAsync, Furthermore, the Begin method returns an IAsyncResult that lets ASP.NET determine when the asynchronous operation has completed, at which point ASP.NET extracts a thread from the thread pool and calls your End method. After End returns, ASP.NET executes the remaining portion of the page's lifecycle, which includes the rendering phase." [End quoting]

The whole idea is that we need to postpone the rending process until we get the result back.

No comments: