Calling Web Services using Client-Side ASP.NET AJAX for Server-Side Validation

Calling Web Services using Client-Side ASP.NET AJAX for Server-Side Validation

by David Hayden ( Florida .NET C# SQL Server Developer ), Filed: ASP.NET 2.0

 

This is really a part 2 on ASP.NET AJAX Server-Side Validation based on the ServerSideValidationExtender Control in the Validation Guidance Bundle that is a part of WCSF v2.0. You can read the first part here so this makes more sense:

I can avoid the whole partial-page postback needs of the ServerSideValidationExtender to clue the user in on whether the URL they typed in is valid by making a client-side network call to a web service that checks to see if the URL exists using WebRequest and WebResponse as mentioned in the previous post. Here is another screenshot of providing a similar experience to what the ServerSideValidationExtender with just a textbox and label control and a bit of javascript on the client. I removed the CustomValidatorControl to focus on the client-side web service call, but you would/could keep it there for actual page validation.

 

Calling Web Services using ASP.NET AJAX

 

I need a web service to call that will actually check the existence of the URL. The code is basically the same as in the previous post:

 

/// <summary>

/// Summary description for ValidatorService

/// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo

    = WsiProfiles.BasicProfile1_1)]

[ScriptService]

public class ValidatorService : WebService

{

 

    [WebMethod]

    public bool ValidateUrl(string url)

    {

        try

        {

            HttpWebRequest request =

                (HttpWebRequest)WebRequest.Create(url);

            request.MaximumAutomaticRedirections = 4;

            HttpWebResponse response =

                (HttpWebResponse)request.GetResponse();

            response.Close();

            return response.StatusCode

                == HttpStatusCode.OK;

        }

        catch

        {

            return false;

        }

    }

}

 

The addition of the [ScriptService] Attribute opens up this web service for use with AJAX.

 

I need to register the web service with ScriptManager on the page so it will create a proxy for me:

 

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
    <asp:ServiceReference Path="ValidatorService.asmx" />
</Services>
</asp:ScriptManager>

 

I need to create a bit of Javascript on the page that calls the webservice and returns the results:

 

<script language="javascript" type="text/javascript">

function CheckUrl () {
    ret = ValidatorService.ValidateUrl
            (document.getElementById("Text1").value,
            OnComplete);
    return(true);
}

function OnComplete (arg) {
    if (!arg)
    {
        document.getElementById("UrlExists")
            .innerText = 'Invalid Url.';
    }
}

</script>

 

I then hook into the TextBox's onBlur Event to call the Javascript Code:

 

<div>
    Url:<br />
    <input
        id="Text1"
        onblur="javascript:CheckUrl();"
        style="width: 319px"
        type="text" />&nbsp;
    <asp:Label ID="UrlExists"
        ForeColor="Red"
        runat="server" />&nbsp;
    <br />
    <br />
    <asp:Button
        ID="btnAdd"
        runat="server"
        OnClick="btnAdd_Click"
        Text="Add" />
</div>

 

The end result is basically the same in terms of appearance and functionality:

 

Call Web Service Using AJAX

 

 

However, rather than a partial-page postback, one gets a more performant request and response. Fiddler tells the story:

 

Call Web Service Using AJAX

 

And that is an alternative :) I think you have to look at UI complexity, maintainability, and other factors to decide what is right for the application. I have another option I am working on as well and will post it in the near future.

 

Author: David Hayden ( Florida .NET C# SQL Server Developer )

Filed: ASP.NET 2.0

 

posted on Wednesday, November 07, 2007 12:19 PM

My Links

Post Categories

Article Categories

Archives

Loose-Leaf Tea