Thursday, March 02, 2006

Script Call back in Asp.net 2.0

Script Call back in Asp.net 2.0

Today I need to use script callback in my project. I search on net for that. I found few results. But still something is missing in that and code shown by them is not working.
Then I tried to solve the problem and luckily I succeed. Example will fill Address Drop down List on change event of Name drop down list.

The Client Callback feature consists of two things: ICallbackEventHandler
interface and Page.ClientScript.GetCallbackEventReference method.

Page.ClientScript.GetCallbackEventReference method has many overloaded version. I will explian which I used in code.

string strCallbackRef = Page.ClientScript.GetCallbackEventReference(this, "message", "showResult", "context", "showError", true);






Return type of GetCallbackEventReference is string. You need to place this string at client side in java script. This string will contain reference to XMLHTTP which asynchronously call the page referred in first argument of function.

You need to implement two methods of ICallbackEventHandler interface. First is public void RaiseCallbackEvent(string strArgFromClientSide) which is the method that execute on XMLHTTP request. Second is
public string GetCallbackResult() which wil return the result of execution of first method to javascript function mentioned in third argument of GetCallbackEventReference method.

I think we read to much. Now take a look at code I used.


Below is code for aspx page.




Explanation

Once you copy past the code and do necessary changes for connection string and Select query of Data Adapter everything is done. Just press F5 and you will see the result.

Now see the view source for this page. You will see following thing.


You can see this red line in your view source (not in red J) . This is the line which cause XMLHTTP request to the page and returns the result. Now see below and you will understand what happened at run time.


I hope this has explained you how script call back works. The only remaining thing is ”context”. This argument is used when you have used more then Async XMLHTTP request. It is not sure that all XMLHTTP request retursns the result in same order as they called. You can identify each request using different context.

Conclusion

To use scrip call back first you need to get reverence of function using GetCallbackEventReference method. Use that reference in java script function.
Then you have to implement two function of ICallbackEventHandler handler.
If your XMLHTTP request execute successfully, java script function mentioned in third argument of GetCallbackEventReference is called and returns you result. If your XMLHTTP request cause an error to occur java script function mentioned in fifth argument of GetCallbackEventReference is called.

Feel free to ask any questions ( but not hard :) ).

2 comments:

davidgibson3247047943 said...

Do you want free porn? Contact my AIM SN 'abunnyinpink' just say 'give me some pics now!'.

No age verification required, totally free! Just send an instant message to AIM screen name "abunnyinpink".

Any message you send is fine!

AIM abuse can be reported here.

mattbencon10540394 said...

Get any Desired College Degree, In less then 2 weeks.

Call this number now 24 hours a day 7 days a week (413) 208-3069

Get these Degrees NOW!!!

"BA", "BSc", "MA", "MSc", "MBA", "PHD",

Get everything within 2 weeks.
100% verifiable, this is a real deal

Act now you owe it to your future.

(413) 208-3069 call now 24 hours a day, 7 days a week.