Static Page Methods Instead of Web Services in ASP.NET AJAX Control Toolkit

As you may already know, it is required to create a web service to make use of some ASP.NET AJAX Control Toolkit controls. However, you can use static methods of your pages instead, you just have to mark them with the [System.Web.Services.WebMethod] attribute and not set the ServicePath parameter in the ACT controls.

In the example from my article on CascadingDropDown we used a web service for populating the drop-down lists, now we'll use static methods instead, we can just copy-paste the methods from the web service to the code-behind file of the page and remove the ServicePath parameters from the controls.

Extract from Default.aspx.cs

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Services.WebMethod]
    public static CascadingDropDownNameValue[] GetContinents(string knownCategoryValues, string category)
    {
        CascadingDropDownNameValue[] continents = new CascadingDropDownNameValue[2];
        continents[0] = new CascadingDropDownNameValue("North America", "North America");
        continents[1] = new CascadingDropDownNameValue("Europe", "Europe");
        return continents;
    }

Extract from Default.aspx

<%--No ServicePath attribute here--%>
<cc1:CascadingDropDown
  ID="cddContinent"
  runat="server"
  TargetControlID="ddlContinent"
  Category="continent"
  ServiceMethod="GetContinents"
/>

If you don't need to expose the methods consumed by the ACT controls, then you can use static methods instead of web services.

Mike Borozdin (Twitter)
17 August 2008

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. My personal thoughts tend to change, hence the articles in this blog might not provide an accurate reflection of my present standpoint.

© Mike Borozdin