AJAX


Whiel using the AJAXToolkit i came up with the situation where i needed to run some javascript after a server-side event was called. After poking around for a bit i found the command “ScriptManager.RegisterClientScriptBlock”. This command sends back asynchronously javascript that will be executed by the clients browser on update. Here is an example:-

string strscript = "var cbut=$get('programmaticPopup');alert(cbut.id+' '+cbut.style.position);";
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ToggleScript", strscript, true);

this.Page is your pages current Page object, “ToggleScript” is the name you are giving the script block, strscript is a string containg the javascript and following that is true which is there to tell the command to suround the inputed javascript with “

p.s. you will notice the javascript command $get(elementname), this returns the object with the ID you gave it at design time just like document.getElementById();. If you look at the source of a running script all the ID tags have strange strings appended to the start of the ID name you gave it. $get finds the element without all the crap. Its part of the AJAX Toolkit.

We wanted to upgrade the usability of the new site we are developing, i have briefly seen info about the microsoft ajax extentions so i thought i would check it out. The main site is http://ajax.asp.net which has tutorials, articles and downloads. I downloaded the extensions for asp.net 2.0 and i also downloaded the toolkit. Now i had the code i thought i would upgrade one of my pages, i added the script manager and an update pane to one of my pages, in the script manager i set the property “Enable Partial Rendering” to true and then pressed play. The page loaded and when i clicked the submit button it submitted the form and updated the message label without reloading the page. Cool i thought this is going to be straight forward so i thought now i will check out the AJAX Toolkit. I unzipped the solution, loaded it into visual studio and pressed play. Well the build failed because the System.EnterpriseServices assembly wouldn’t load because it didn’t have the correct permissions. So off to google i went, after many unhelpful sites i figured it has something to do with the security levels in the web.config. So i updated the directive “trust” and set it to

<trust level=”Full”/>

I am not too experienced with windows permissions but i don’t think this is too unsafe. Anyway this got it working and i loaded the sample website. There seemed to be many cool controls and i worked out how i could use some of them in the current project. I added the tabs control without much drama but the autocomplete control is giving me trouble. What is cool about it is that it uses web services which i hadn’t done is asp.net before. They seem pretty straight forward which is great as we are going to be using them in the future. One thing i noticed about them is they don’t share the session from the ajax page accessing them.