AJAX has become very very popular and the resurgence of javascript has been unprecendented, spurred on by libraries like jQuery and Mootools, we can now do partial-page postbacks, get data and fire events asynchronously easier than ever thought possible.
In ASP.NET we have the luxury of having an AJAX framework built in from v3.5 onwards that includes some pretty powerful tools, the best of which is the UpdatePanel. The UpdatePanel allows us to trigger partial-page postbacks and hit methods and classes in the code-behind with little effort to wire-up the event.
This can be built upon by using an UpdatePanel in conjunction with jQuery through the ScriptManager object. ScriptManager will inject javascript into a page asynchronously and then let it do its stuff.
What this means, in laymans terms, is that you can hit a button, return some data and run some javascript all without a postback.
The simplest example of ScriptManager usage is this;
C#
ScriptManager.RegisterClientScriptBlock(Page, GetType(), “Load”, “Load();”, true);VB.NET
ScriptManager.RegisterClientScriptBlock(Page, GetType, “Load”, “Load();”, true
What this does is, on a partial page postback, adds this to the page;
<script type=”text/javascript”>
Load();
</script>
So executing a javascript function called that has already been included in the page but been laying dormant until now, and this can do anything you want; run some jQuery to pop-up a modal window, change the colour of some text or whatever, or perform some validation, the possibilities are endless.
Want to inject javascript into a webform from code-behind that hasn’t been fired asynchronously? Use the ClientScriptManager object instead of ScriptManager. ClientScriptManager runs at the beginning of a a full-page postback.
-
aeonra liked this
-
rankandfile liked this
-
howardtharp liked this
-
cardiffcoder posted this