So there are a few libraries out there to "connect" .NET and AJAX, one of the most popular being Ajax.NET. :p Still, the concept of putting code on the server-side to link up client-side script doesn't really do it for me, especially because I want to replace the traditional synchronous form postback with an asynchronous call, without touching my existing code. An interesting challenge...
Before I go into details about how to go about doing this, let me just spell out some of the problems to solve in this approach...
First, for the pages to run as-is, everything must appear cool to ASP.NET. You see, everytime you play around with something in an ASP.NET page, the most likely thing to happen is a postback... by default almost everything in a ASP.NET page provokes a post of the form, because that way it's easier to alter the page - showing or hiding stuff, loading combos, etc - in the code-behind. To plug-in ajax into this, we'll have to make sure the code-behind doesn't notice we're actually posting stuff programatically through ajax instead of allowing the form to auto-post itself.
Second, how do we deal with the return html? Because your page will not know that ajax did the posting, it will not return xml or any data format pretty enough to be manipulated in javascript. Oh no, it will return exactly what it would do if you allowed the form to post itself: HTML.
Now, you're thinking, "that's not so bad, we just take the html and plug it back to the user by doing a document.innerHTML = response or something, right?" Riiiight... that works... unless, of course, your nice page has javascript it wants the browser to run. Big oopsie.
Ya see, the page you're getting back is the same the browser would get directly, and of course, your nice web app might be outputting some nice script to load an array dinamically as you cycle through a repeater or something. Who knows! And that nice little script will come out in the middle of all that html that you will get in response to you ajax request, only when you show it to the user by writing it to the whateverHTML property of your document, it won't be executed. No siree. Isn't it fun? :)
So 2 nice little problems for us... making sure the ASP.NET engine doesn't notice we're doing the calling, and mimicking the behaviour of a browser by executing any scripts that are returned in the html.
This will be interesting... :)
No comments:
Post a Comment