home

Softsteel Solutions

About Us Contact Us Newsletter Training
Tutorials
 

ASP.NET Ajax Tutorial Lesson 4: Page Methods

printer friendly version

We will shortly discuss ASP.NET Ajax’s support for Web Service calls. However, we also get inbuilt support for calling into the public, static methods of standard ASP.NET pages (this kind of functionality will be familiar to anyone who has used the Ajax.Net libraries produced by Michael Schwarz).

To take an (obviously silly) example, suppose that we want to make an Ajax call to concatenate two strings. That is, we want the client to be able to call into the following method defined on our page:

1.

public static string ConcatenateStrings(string s1, string s2)

2.

{

3.

    return s1 + s2;

4.

}


The first thing we need to do is to turn on the page’s support for Page Methods, which we do by setting the ‘EnablePageMethods’ attribute of the ScriptManager to ‘true’, eg.

<asp:ScriptManager enablepagemethods=true ID="ScriptManager1" runat="server" />

We then decorate the ConcatenateStrings method with the ‘WebMethodAttribute’, viz:

[WebMethod]
public static string ConcatenateStrings(string s1, string s2) {…}

Because we’ve turned on Page Methods, the client page exposes the PageMethods collection, which contains our ConcatenateStrings method. Since Ajax calls are asynchronous, however, the clientside method uses the standard pattern for asynchronous calls. That is, instead of taking just two parameters, it takes two plus an extra three. The first of the extra parameters specifies a callback function for a successful return from the server. The second specifies a callback function for when an exception has been thrown on the server. The third provides a ‘context’ object to be passed back and forth (for instance to disambiguate multiple calls to the same function).

So, for instance, we might have client-side functionality like this, which on a successful postback to the server will result in the display of the message ‘hello world’:

1.

<script type="text/javascript">

2.

function echoResult(strResults, context)

3.

{

4.

    alert(strResults);

5.

}

6.

7.

function showError(objError, context)

8.

{

9.

    alert(objError.get_exceptionType());

10.

}

11.

</script>

12.

13.

<a href="javascript:PageMethods.ConcatenateStrings('hello ', 'world ', echoResult, showError, 'test')">Hello World</a>


It is useful to note that within the server-side method one can verify current user authentication and session states.

As with ASP.NET Ajax’s support for Web Services, the messages passing back and forth between the client and the server use JSON, a lightweight protocol for encrypting Javascript objects. As with XML Serialization, the only data that get passed back and forth are public properties and fields, and there is no support for ‘cyclic references’ (where A holds a reference to B and B holds a reference back to A). Beyond this, however, there is no fixed set of which objects can be trafficked between the client and the server.

Unfortunately, the 1.0 release of the ASP.NET Ajax Extensions is unable to cope with JSONifying standard ADO.NET objects such as DataSet, DataTable and DataRow, since it finds in them circular references to Globalization objects. This means that Ajax calls fail to Page Methods trying to return such objects. There is a workaround, however, since various Community Technology Previews contain appropriate JSON converters for these objects. But note that the licencing for CTPs generally stops you employing them in production websites.

In order to set up support for ADO.NET objects you need to do the following things:

1. Download and install the ASP.NET 2.0 AJAX Futures January CTP from http://www.microsoft.com/downloads/details.aspx?FamilyID=4CB52EA3-9548-4064-8137-09B96AF97617&displaylang=en

2. Make sure that there’s a reference in your web project to the Microsoft.Web.Preview dll that gets installed (under C:\Program Files\Microsoft ASP.NET\ ASP.NET 2.0 AJAX Futures January CTP\v1.0.61025)

3. Update the project’s Web.config file so that it contains the following entries

1.

<system.web.extensions>

2.

    <scripting>

3.

        <webServices>

4.

            <jsonSerialization>

5.

                <converters>

6.

                    <add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview"/>

7.

                    <add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview"/>

8.

                    <add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview"/>

9.

                </converters>

10.

            </jsonSerialization>

11.

        </webServices>

12.

    </scripting>

13.

</system.web.extensions>


 

ASP.NET Ajax Tutorial