ASP.NET AJAX Server-Side Validation - ServerSideValidationExtender in Validation Guidance Bundle

ASP.NET AJAX Server-Side Validation - ServerSideValidationExtender in Validation Guidance Bundle

by David Hayden ( Florida .NET Developer ), Filed: ASP.NET 2.0, Web Client Software Factory

 

My brain is working overtime on the simple concept of doing server-side validation using ASP.NET AJAX in webforms just like the ServerSideValidationExtender in the Validation Guidance Bundle. I really dig the ServerSideValidationExtender Control, but am trying to better understand alternative solutions and the pros and cons of each.

The code I am working from today is essentially a strip down version of the QuickStart Tutorial that ships with the Validation Guidance Bundle. Basically I have a page where a user types in a URL and I want to validate that the URL exists by doing a simple WebRequest to the URL from the server. In its simplest form it includes a page with a CustomValidator, ServerSideValidationExtender, and of course the CustomValidator Server Code that checks for the existence of the URL:

 

ASP.NET AJAX Server Side Validation

 

Here is a snippet of the page where you can see the ServerSideValidationExtender targeting the CustomValidator Control:

 

<asp:TextBox
    ID="txtUrl"
    runat="server">
</asp:TextBox>&nbsp;
<asp:CustomValidator
    ID="UrlExistsValidator"
    ControlToValidate="txtUrl"
    ErrorMessage="Invalid Url."
    OnServerValidate="CustomValidator1_ServerValidate"
    runat="server">
</asp:CustomValidator><br />
<cc1:ServerSideValidationExtender
    ID="ServerSideValidationExtender1"
    TargetControlID="UrlExistsValidator"
    runat="server">
</cc1:ServerSideValidationExtender>

 

For more information on the ServerSideValidationExtender, you can see the following:

 

The user experience is pretty slick, because the ServerSideValidationExtender does a partial-page postback after typing in the URL, letting you know if the URL is valid or not:

 

AJAX Server-Side Validation

 

The CustomValidator's Validation Code ( which I think I literally copied and pasted from the quickstart tutorial ) is as follows:

 

protected void CustomValidator1_ServerValidate

    (object source, ServerValidateEventArgs args)

{

    string url = args.Value;

    Uri uri;

    if (Uri.TryCreate(url, UriKind.Absolute, out uri))

    {

        args.IsValid = UriExists(uri);

    }

    else

    {

        args.IsValid = false;

    }

}

 

private bool UriExists(Uri uri)

{

    try

    {

        HttpWebRequest request =

            (HttpWebRequest)WebRequest.Create(uri);

        request.MaximumAutomaticRedirections = 4;

        HttpWebResponse response =

            (HttpWebResponse)request.GetResponse();

        response.Close();

        return response.StatusCode == HttpStatusCode.OK;

    }

    catch

    {

        return false;

    }

}

 

This is all very, very slick, and quite frankly perfect for my needs even though I realize there are some gotchas with Partial-Page Postbacks. Still, I wondered how I could get this immediate feedback without a partial-page postback, which got me into Client-Side Network Callbacks. I will save that post for next time.

 

Source: David Hayden ( Florida .NET Developer )

Filed: ASP.NET 2.0, Web Client Software Factory

 

posted on Monday, November 05, 2007 4:38 PM

My Links

Post Categories

Article Categories

Archives

Loose-Leaf Tea