Using Ajax Professional.NET Library
Library Creator: Michael Schwarz (Michael.schwarz@gmail.com)
      Author: Joseph Guad...
Using Ajax Professional Library


USING AJAX PROFESSIONAL.NET LIBRARY........................................................
Using Ajax Professional Library

RECOMMENDATIONS.............................................................................
Using Ajax Professional Library



Introduction
Ajax Professional.NET Library
The Ajax Professional .NET library allows yo...
Using Ajax Professional Library


Enabling the Page
Register the page for use with the AjaxPro library. This creates the J...
Using Ajax Professional Library


The params can be any number of parameters but should match the signature of the
ASP.NET...
Using Ajax Professional Library


JavaScriptObject
TODO: Document



Response Object
When a callback function is used, whi...
Using Ajax Professional Library


RegisterEnumForAjax(Type)
TODO: Document

RegisterEnumForAjax(Type, Page)
TODO: Document...
Using Ajax Professional Library


AjaxNamespace
This attribute can be used to specify a different namespace for the client...
Using Ajax Professional Library


                     the request is finished.
onError              A function that will ...
Using Ajax Professional Library

       var teams = response.value;
       //if the response wasn't what we expected
     ...
Using Ajax Professional Library

     drToAdd.LookId = document.getElementById("txtLookId").value;
     drToAdd.LeftImage ...
Microsoft Word version
Upcoming SlideShare
Loading in...5
×

Microsoft Word version

323

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
323
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Microsoft Word version"

  1. 1. Using Ajax Professional.NET Library Library Creator: Michael Schwarz (Michael.schwarz@gmail.com) Author: Joseph Guadagno (jguadagno@hotmail.com)
  2. 2. Using Ajax Professional Library USING AJAX PROFESSIONAL.NET LIBRARY.....................................................................................1 INTRODUCTION..........................................................................................................................................1 AJAX PROFESSIONAL.NET LIBRARY..................................................................................................................1 WHAT IS AJAX................................................................................................................................................1 XMLHTTPRequest..................................................................................................................................1 QUICK START..............................................................................................................................................1 ENABLING THE WEBSITE...................................................................................................................................1 Web.Config.............................................................................................................................................1 Solution...................................................................................................................................................1 ENABLING THE PAGE........................................................................................................................................2 C# Sample...............................................................................................................................................2 Visual Basic Sample...............................................................................................................................2 ENABLING A METHOD.......................................................................................................................................2 CS File....................................................................................................................................................2 VB File....................................................................................................................................................2 ASPX File................................................................................................................................................2 JS File.....................................................................................................................................................2 THE LIBRARY..............................................................................................................................................3 DATATYPES....................................................................................................................................................3 Class.......................................................................................................................................................3 DataType.................................................................................................................................................3 DataSets..................................................................................................................................................3 HtmlControls..........................................................................................................................................3 Collections..............................................................................................................................................3 Special.....................................................................................................................................................3 OnLoading..............................................................................................................................................3 KeyPress.................................................................................................................................................3 UserControls...........................................................................................................................................3 JavaScriptObject.....................................................................................................................................4 Response Object......................................................................................................................................4 Error Object............................................................................................................................................4 AJAXPRO.UTILITIES.........................................................................................................................................4 GetClientMethod.....................................................................................................................................4 GetScripts(Page).....................................................................................................................................4 GetScripts(Page, bool)...........................................................................................................................4 GetSessionUri.........................................................................................................................................4 RegisterClientScriptBlock.......................................................................................................................4 RegisterCommonAjax.............................................................................................................................4 RegisterCommonAjax(Page)..................................................................................................................4 RegisterConverterForAjax.....................................................................................................................4 RegisterEnumForAjax(Type)..................................................................................................................5 RegisterEnumForAjax(Type, Page)........................................................................................................5 RegisterTypeForAjax(Type)...................................................................................................................5 RegisterTypeForAjax(Type, Page).........................................................................................................5 ATTRIBUTES....................................................................................................................................................5 AjaxMethod.............................................................................................................................................5 AjaxNamespace.......................................................................................................................................6 AjaxNonSerializable...............................................................................................................................6 AjaxProperty...........................................................................................................................................6 HttpSessionState.....................................................................................................................................6 JavaScriptConverter...............................................................................................................................6 CALLING ASP.NET METHOD..........................................................................................................................6
  3. 3. Using Ajax Professional Library RECOMMENDATIONS...............................................................................................................................7 SAMPLES.......................................................................................................................................................7 POPULATING A DROP DOWN LIST, SELECT ELEMENT.........................................................................................7 SENDING A DATASET TO THE SERVER.................................................................................................................8
  4. 4. Using Ajax Professional Library Introduction Ajax Professional.NET Library The Ajax Professional .NET library allows you the developer to create more interactive web pages, commonly referred to as Web 2.0, without the need for post backs. URLs Ajax Professional .NET library http://www.ajaxpro.info Ajax Professional Examples http://www.codeplex.com Sample Code Site (Joseph Guadagno) http://josephguadagno.net/ajax.aspx Support for the library http://groups.google.com/group/ajaxpro What is Ajax Asynchronous JavaScript and XML (AJAX) is not a technology in itself, but is a term that describes a "new" approach to using a number of existing technologies together, including: HTML or XHTML, Cascading Style Sheets, JavaScript, The Document Object Model, XML, XSLT, and the XMLHTTPRequest object. When these technologies are combined in the AJAX model, web applications are able to make quick, incremental updates to the user interface without reloading the entire browser page. This makes the application faster and more responsive to user actions. XMLHTTPRequest XMLHTTPRequest is a HTTP Response that returns an XML document. Quick Start Enabling the Website Web.Config Add into the web configuration file (web.config) the required httpHandlers within the <system.web> section. <httpHandlers> <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro"/> </httpHandlers> Solution Add a reference to the AjaxPro library within your solution. Joseph J Guadagno Page 1 4/29/2010
  5. 5. Using Ajax Professional Library Enabling the Page Register the page for use with the AjaxPro library. This creates the JavaScript files on the server for the class and methods. C# Sample private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxVB.WebForm1)); } Visual Basic Sample. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here AjaxPro.Utility.RegisterTypeForAjax(GetType(AjaxVB.WebForm1)) End Sub Enabling a Method Once you have Ajax-enabled the page and website, Ajax-enabling a method is quite simple. The only item that is required is to add the AjaxMethod attribute to the class. CS File [AjaxPro.AjaxMethod()] public string GetServerTime() { return DateTime.Now.ToString(); } VB File <AjaxPro.AjaxMethod()> _ Public Function GetServerTime() As String Return DateTime.Now.ToString() End Function ASPX File Within the HTML designer, add the following line to include the JavaScript library to the page. It is recommended to name the JavaScript file, the same as the web page name. <script language="javascript" src="WebForm1.js"></script> JS File The JavaScript file contains the calls to the server. In order to call the ASP.NET method, you follow the following syntax: namespace.classname.methodname (params, callback_function) Joseph J Guadagno Page 2 4/29/2010
  6. 6. Using Ajax Professional Library The params can be any number of parameters but should match the signature of the ASP.NET method. Please note the current version of AjaxPro.Net library does not support overloaded methods. The callback_function is optional but stronger recommended. function GetServerTime() { AjaxVB.WebForm1.GetServerTime(GetServerTime_Callback) } function GetServerTime_Callback(response) { Label1.innerHTML = response.value; } The Library DataTypes Class TODO: Document DataType TODO: Document DataSets TODO: Document HtmlControls TODO: Document Collections TODO: Document Special TODO: Document OnLoading TODO: Document KeyPress TODO: Document UserControls TODO: Document Joseph J Guadagno Page 3 4/29/2010
  7. 7. Using Ajax Professional Library JavaScriptObject TODO: Document Response Object When a callback function is used, which is strongly recommended, a response object will be passed as the only parameter. The response object contained two (2) objects Value The returned value from the ASP.NET method Error If not null, an object containing error information Error Object The error object contains four properties that describe the error. Message The message text, Err.ToString() Type The type of Exception Stack A full stack trace Source The page source that generated the error. AjaxPro.Utilities GetClientMethod TODO: Document GetScripts(Page) TODO: Document GetScripts(Page, bool) TODO: Document GetSessionUri TODO: Document RegisterClientScriptBlock TODO: Document RegisterCommonAjax TODO: Document RegisterCommonAjax(Page) TODO: Document RegisterConverterForAjax TODO: Document Joseph J Guadagno Page 4 4/29/2010
  8. 8. Using Ajax Professional Library RegisterEnumForAjax(Type) TODO: Document RegisterEnumForAjax(Type, Page) TODO: Document RegisterTypeForAjax(Type) TODO: Document RegisterTypeForAjax(Type, Page) TODO: Document Attributes Attributes are applied to your page methods that you want to make available to the browser. __AjaxClass (Obsolete) __AjaxEnum (Obsolete) AjaxCache This attribute instructs the Ajax library to cache the response on the server for the duration specified in the second’s parameter. The library will cache results for the same parameters. Attribute Seconds (int) The number of seconds to cache the response AjaxMethod This attribute instructs the Ajax library to make this method available to the browser. Attribute Overloads HttpSessionStateRequirement see HttpSessionState, Marks the method to be exported as an Ajax.NET Javascript function with the ability to access the SessionState.) Bool Marks the method to be exported as an Ajax.NET Javascript function with the ability to be processed as an async request on the server.) HttpSessionStateRequirement, Marks the method to be exported as an Ajax.NET Bool Javascript function with the ability to be processed as an async request on the server and to access the SessionState. Joseph J Guadagno Page 5 4/29/2010
  9. 9. Using Ajax Professional Library AjaxNamespace This attribute can be used to specify a different namespace for the client-side representation Attribute String The namespace to use AjaxNonSerializable TODO: Document AjaxProperty TODO: Document HttpSessionState An enumeration which indicates how the method can interact with the ASP.NET SessionState (used by the AjaxMethod attribute) Enumeration ReadWrite Enabled read/write access to the SessionState Read Enables read access to the SessionState None No SessionState available. JavaScriptConverter This attribute marks a class to be converted by a specified IJavaScriptConverter Type The type (class) Calling ASP.NET Method The Ajax Professional .NET library creates a JavaScript method for each method that is attributed with the AjaxMethod attribute. The signature of the method is as follows: Namespace.Classname.Method(arg1, arg2, arg3, ... callback, context, onLoading, onError, onTimeout, onStateChanged); Parameter Used For Arg1, arg2, arg3 The arguments used in the .NET method Callback The function that will be called when the request is finished Context Every JavaScript value that can be accessed in the callback with the response.context onLoading A function that will be called twice, once with a true argument once the request has been started, once with a false argument when Joseph J Guadagno Page 6 4/29/2010
  10. 10. Using Ajax Professional Library the request is finished. onError A function that will be called in the event of an error like http status of 404 or 500. onTimeout A function that will be called in the event that the request timeouts (default of 10 seconds, which can be changed by calling AjaxPro.timeoutPeriod within JavaScript) onStateChanged A function that is called if the internal XMLHTTPRequest is switching status. Recommendations Use a JavaScript file. This will enable you to debug any problems within the Visual Studio IDE. Use the callback function function GetServerTime_Callback(response) { if (response.error != null) { // Display the error return; } var saveResults = response.value; if (saveResults != "") { // Nothing was return } // Work with the response Label1.innerHTML = saveResults; } Samples Populating a Drop Down List, SELECT element function GetTeamList(response) { var teamsList = document.getElementById("ctrlContent__ctl0_ucProfile_dropTeam"); //if the server side code threw an exception if (response.error != null) { alert("A problem occurred in Profile:LoadTeamsn" + response.error.Message); //we should probably do better than this return; } Joseph J Guadagno Page 7 4/29/2010
  11. 11. Using Ajax Professional Library var teams = response.value; //if the response wasn't what we expected if (teams == null || typeof(teams) != "object") { alert('A problem occurred in Profile:LoadTeams'); return; } teamsList.options.length = 0; //reset the teams dropdown //note that this is JavaScript casing and the L in length is lowercase for arrays teamsList.options[teamsList.options.length] = new Option("", ""); for (var i = 0; i < teams.Rows.length; ++i) { teamsList.options[teamsList.options.length] = new Option(teams.Rows[i].team_name, teams.Rows[i].team); //teamsList.options[teamsList.options.length] = new Option(teams[i].team_name, teams[i].team); } } The example above assumes that the ASP.NET method returns a DataTable. If a DataSet is returned, preface the .Rows with .Tables[0]. Sending a DataSet to the Server // Create the DataSet var ds = new Ajax.Web.DataSet(); // Create a DataTable Var dt = new Ajax.Web.DataTable(); // Add the columns dt.addColumn("NodeId", "System.Int"); dt.addColumn("ParentId", "System.Int"); dt.addColumn("MenuText", "System.String"); dt.addColumn("StatusText", "System.String"); dt.addColumn("NavigateUrl", "System.String"); dt.addColumn("LookId", "System.String"); dt.addColumn("LeftImage", "System.String"); dt.addColumn("LeftHoverImage", "System.String"); dt.addColumn("RightImage", "System.String"); dt.addColumn("RightHoverImage", "System.String"); // Create an populate the row var drToAdd = new Object(); drToAdd.NodeId = document.getElementById("txtNodeId").value; drToAdd.ParentId = document.getElementById("txtParentId").value; drToAdd.MenuText = document.getElementById("txtMenuText").value; drToAdd.StatusText = document.getElementById("txtStatusText").value; drToAdd.NavigateUrl = document.getElementById("txtNavigateUrl").value; Joseph J Guadagno Page 8 4/29/2010
  12. 12. Using Ajax Professional Library drToAdd.LookId = document.getElementById("txtLookId").value; drToAdd.LeftImage = document.getElementById("txtLeftImage").value; drToAdd.LeftHoverImage = document.getElementById("txtLeftHoverImage").value; drToAdd.RightImage = document.getElementById("txtRightImage").value; drToAdd.RightHoverImage = document.getElementById("txtRightHoverImage").value; // Add the rows dt.addRow(drToAdd); // Add the table to the DataTable ds.addTable(dt); Joseph J Guadagno Page 9 4/29/2010

×