Usability, MVC, ASP.NET

9. July 2007 23:03
by Henrik Stenbæk
9 Comments

ASP.NET AJAX username availability check without UpdatePanel

9. July 2007 23:03 by Henrik Stenbæk | 9 Comments

 

I have improved this code and turned it into a custom control – please se this post

 

Inspired by the RememberTheMilk signup usability I decided to write an ASP.NET AJAX function that works the same way. I also wanted my function to fire on every keystroke so that the user will get an instant response about whether the username is available.

After reading about the overhead that UpdatePanel produce on the server load I decided to try to write the AJAX username availability check without using the UpdatePanel.

For this example, I'm going to use a standard ASP.NET web form along with some of the functionalities from the Microsoft ASP.NET Futures (May 2007) Release.

I create one simple Web Form containing only a text box and a label

Username: 
<asp:TextBox ID="Username" runat="server" Width="93px">
</asp:TextBox> 
<asp:Label ID="lblAvailability" runat="server" Text="Type a username" 
ForeColor="DimGray">
</asp:Label> 

For the username availability check I create a webservice.

[ScriptService] public class UsernameService : System.Web.Services.WebService { .. } 


Remember to prefix the Service with the [ScriptService] attribute otherwise it will not work when called from JavaScript.

I add one function to the WebService called IsAvailable. The purpose for this function is to check for the availability of the current typed username. Inspired by Dave Ward I decided to check availability through a call to Membership.GetUser().

[WebMethod] public string[] IsAvailable(string TocheckFor) 
{ 
string[] myString = new string[2]; 
if (Membership.GetUser(TocheckFor) != null) 
{ 
myString[0] = string.Format("{0} is taken", TocheckFor); 
myString[1] = "false"; 
} 
else 
{ 
myString[0] = string.Format("{0} is available", TocheckFor); 
myString[1] = "true"; 
} 
return myString; 
} 

The IsAvailable function return a string array with the status text and a 'true'/'false' string depending on if the username is available or not.

I add a ScriptManager to the Web Form that will take care of the communication with the WebService.

<asp:ScriptManager ID="ScriptManager1" runat="server"> 
<Services> 
<asp:ServiceReference Path="UsernameService.asmx" /> 
</Services> 
<Scripts> 
<asp:ScriptReference Name="PreviewScript.js" 
Assembly="Microsoft.Web.Preview" /> 
</Scripts> 
</asp:ScriptManager> 

In the script manager I configure the webService to be used with the client side javaScript. So I write a little bid of clientside javaScript

<script type="text/javascript" > 
function checkName() {    var tb = new Sys.Preview.UI.TextBox($get('Username')); 
var name = tb.get_text(); 
if(name.lenght != 0) 
UsernameService.IsAvailable(name, onIsAvailableCompleted) 
} 
function onIsAvailableCompleted(result) 
{ 
if(result != null) 
{ 
var lbl = new Sys.Preview.UI.Label($get('lblAvailability')); 
lbl.set_text(result[0]); 
if(result[1]=='true') 
document.getElementById('lblAvailability').style.color="green"; 
else 
document.getElementById('lblAvailability').style.color="red"; 
} 
} 
</script> 

The first function checkName calls the WebService if there is any text in Username, setting the second function (onIsAvailableCompleted) as the call back function to receive the answer from the webservice.

Finally in code behind I add the clienside function checkName to the Username textbox's onKeyUp

Username.Attributes.Add("onkeyup", "checkName()");

Thats all. Now the username is checked on every keystroke and with a minimum of server traffic.

Download the code: UsernameFutures.zip (708,33 kb)

Comments

  1. RapzaDudey

    Nice work dude! Smile

  2. alidad

    I have tried in php and didn't works, any idea if you can help us in php!

  3. dav


    It says ..UserNameservice is undefined in javascript

  4. Simone Busoli

    Why do you need the ASP.NET futures? It's nothing that can't be accomplished without the Sys.Preview.UI.TextBox or Sys.Preview.UI.Label classes!

  5. farshad

    thanks.. i wish it was about php ajax username checker

  6. Henrik Stenb&#230;k

    Hi Simone

    Your right! There is no need for the ASP.NET futures. Please look at my next post where I turned this code into a usercontrol with 'no future' Wink

    onesoft.dk/.../...ailability-with-suggestions.aspx

  7. matt

    I am trying to implement with an aspx page and a master page.
    <%@ Page Language="C#" MasterPageFile="~/Template.master" AutoEventWireup="true" CodeFile="CheckUserName.aspx.cs" Inherits="CheckUsernameWithAJAX.CheckUserName_aspx" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

        
            
           <div>
                Username: <asp:TextBox ID="Username" runat="server" Width="93px"></asp:TextBox>
                <asp:Label ID="lblAvailability" runat="server" Text="Type a username" ForeColor="DimGray"></asp:Label>
            </div>

      

    </asp:Content>

Pingbacks and trackbacks (4)+

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading