Using Ajax Professional.
NET Library
Library Creator: Michael Schwarz ([Link]@[Link])
Author: Joseph Guadagno (jguadagno@[Link])
Using Ajax Professional Library
USING AJAX [Link] LIBRARY.....................................................................................1
INTRODUCTION..........................................................................................................................................4
AJAX [Link] LIBRARY...........................................................................................................4
WHAT IS AJAX.............................................................................................................................................4
XMLHTTPRequest..................................................................................................................................4
QUICK START..............................................................................................................................................4
ENABLING THE WEBSITE..............................................................................................................................4
[Link].............................................................................................................................................4
Solution...................................................................................................................................................4
ENABLING THE PAGE...................................................................................................................................5
C# Sample...............................................................................................................................................5
Visual Basic Sample...............................................................................................................................5
ENABLING A METHOD..................................................................................................................................5
CS File....................................................................................................................................................5
VB File....................................................................................................................................................5
ASPX File...............................................................................................................................................5
JS File.....................................................................................................................................................5
THE LIBRARY..............................................................................................................................................6
DATATYPES..................................................................................................................................................6
Class.......................................................................................................................................................6
DataType................................................................................................................................................6
DataSets..................................................................................................................................................6
HtmlControls..........................................................................................................................................6
Collections..............................................................................................................................................6
Special....................................................................................................................................................6
OnLoading..............................................................................................................................................6
KeyPress.................................................................................................................................................6
UserControls..........................................................................................................................................6
JavaScriptObject....................................................................................................................................6
Response Object.....................................................................................................................................6
Error Object...........................................................................................................................................6
[Link].....................................................................................................................................7
GetClientMethod....................................................................................................................................7
GetScripts(Page)....................................................................................................................................7
GetScripts(Page, bool)...........................................................................................................................7
GetSessionUri.........................................................................................................................................7
RegisterClientScriptBlock......................................................................................................................7
RegisterCommonAjax.............................................................................................................................7
RegisterCommonAjax(Page)..................................................................................................................7
RegisterConverterForAjax.....................................................................................................................7
RegisterEnumForAjax(Type)..................................................................................................................7
RegisterEnumForAjax(Type, Page).......................................................................................................7
RegisterTypeForAjax(Type)...................................................................................................................7
RegisterTypeForAjax(Type, Page).........................................................................................................7
ATTRIBUTES.................................................................................................................................................7
__AjaxClass............................................................................................................................................7
__AjaxEnum...........................................................................................................................................7
AjaxCache...............................................................................................................................................7
AjaxMethod.............................................................................................................................................8
AjaxNamespace......................................................................................................................................8
AjaxNonSerializable...............................................................................................................................8
AjaxProperty...........................................................................................................................................8
Using Ajax Professional Library
HttpSessionState.....................................................................................................................................8
JavaScriptConverter...............................................................................................................................8
CALLING [Link] METHOD......................................................................................................................9
RECOMMENDATIONS...............................................................................................................................9
SAMPLES.....................................................................................................................................................10
POPULATING A DROP DOWN LIST, SELECT ELEMENT.............................................................................10
Sending a DataSet to the Server................................................................................................................10
Using Ajax Professional Library
Introduction
Ajax [Link] 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 [Link]
Ajax Professional Examples [Link]
Sample Code Site (Joseph Guadagno) [Link]
Support for the library [Link]
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
[Link]
Add into the web configuration file ([Link]) the required httpHandlers within the
<[Link]> section.
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="[Link], AjaxPro"/>
</httpHandlers>
Solution
Add a reference to the AjaxPro library within your solution.
Joseph J Guadagno Page 1 12/8/2021
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, [Link] e)
{
// Put user code to initialize the page here
[Link](typeof(AjaxVB.WebForm1));
}
Visual Basic Sample.
Private Sub Page_Load(ByVal sender As [Link], ByVal e As [Link]) Handles
[Link]
'Put user code to initialize the page here
[Link](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
[[Link]()]
public string GetServerTime()
{
return [Link]();
}
VB File
<[Link]()> _
Public Function GetServerTime() As String
Return [Link]()
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="[Link]"></script>
JS File
The JavaScript file contains the calls to the server. In order to call the [Link] method,
you follow the following syntax:
[Link] (params, callback_function)
Joseph J Guadagno Page 2 12/8/2021
Using Ajax Professional Library
The params can be any number of parameters but should match the signature of the
[Link] method. Please note the current version of [Link] library does not
support overloaded methods.
The callback_function is optional but stronger recommended.
function GetServerTime()
{
[Link](GetServerTime_Callback)
}
function GetServerTime_Callback(response)
{
[Link] = [Link];
}
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 12/8/2021
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 [Link] 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, [Link]()
Type The type of Exception
Stack A full stack trace
Source The page source that generated the error.
[Link]
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 12/8/2021
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 [Link] Javascript function with the ability to
access the SessionState.)
Bool Marks the method to be exported as an [Link]
Javascript function with the ability to be processed as an
async request on the server.)
HttpSessionStateRequirement, Marks the method to be exported as an [Link]
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 12/8/2021
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 [Link]
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 [Link] 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:
[Link](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 [Link]
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 12/8/2021
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
[Link] 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 ([Link] != null)
{
// Display the error
return;
}
var saveResults = [Link];
if (saveResults != "")
{
// Nothing was return
}
// Work with the response
[Link] = saveResults;
}
Samples
Populating a Drop Down List, SELECT element
function GetTeamList(response)
{
var teamsList = [Link]("ctrlContent__ctl0_ucProfile_dropTeam");
//if the server side code threw an exception
if ([Link] != null)
{
alert("A problem occurred in Profile:LoadTeams\n" + [Link]); //we
should probably do better than this
return;
}
Joseph J Guadagno Page 7 12/8/2021
Using Ajax Professional Library
var teams = [Link];
//if the response wasn't what we expected
if (teams == null || typeof(teams) != "object")
{
alert('A problem occurred in Profile:LoadTeams');
return;
}
[Link] = 0; //reset the teams dropdown
//note that this is JavaScript casing and the L in length is lowercase for arrays
[Link][[Link]] = new Option("", "");
for (var i = 0; i < [Link]; ++i)
{
[Link][[Link]] = new Option([Link][i].team_name,
[Link][i].team);
//[Link][[Link]] = new Option(teams[i].team_name,
teams[i].team);
}
}
The example above assumes that the [Link] 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 [Link]();
// Create a DataTable
Var dt = new [Link]();
// Add the columns
[Link]("NodeId", "[Link]");
[Link]("ParentId", "[Link]");
[Link]("MenuText", "[Link]");
[Link]("StatusText", "[Link]");
[Link]("NavigateUrl", "[Link]");
[Link]("LookId", "[Link]");
[Link]("LeftImage", "[Link]");
[Link]("LeftHoverImage", "[Link]");
[Link]("RightImage", "[Link]");
[Link]("RightHoverImage", "[Link]");
// Create an populate the row
var drToAdd = new Object();
[Link] = [Link]("txtNodeId").value;
[Link] = [Link]("txtParentId").value;
[Link] = [Link]("txtMenuText").value;
[Link] = [Link]("txtStatusText").value;
[Link] = [Link]("txtNavigateUrl").value;
[Link] = [Link]("txtLookId").value;
[Link] = [Link]("txtLeftImage").value;
[Link] = [Link]("txtLeftHoverImage").value;
Joseph J Guadagno Page 8 12/8/2021
Using Ajax Professional Library
[Link] = [Link]("txtRightImage").value;
[Link] = [Link]("txtRightHoverImage").value;
// Add the rows
[Link](drToAdd);
// Add the table to the DataTable
[Link](dt);
Joseph J Guadagno Page 9 12/8/2021