UPDATE: ALWAYS DOWNLOAD LATEST SOURCE CODE FROM PROJECT PAGE ON CODEPLEX
Few weeks ago Dave started the Advanced ASP.NET AJAX Server Controls book giveaway contest. And yesterday he announced that the control is almost completed, but the contest door is still open. So I thought to grape the bits and start testing it and build a simple sample. [View Demo]
The sample that I am going to demonstrate here will use ASP.NET Create User Wizard control with UserName Availability Validator. In this sample I'll show the important properties of this control as well as the current issues exist.
Prerequisites:
I'll assume that you already know how to configure and use the existing built in ASP.NET Membership provider. You'll need a sample database with ASP.NET Membership services installed on it.
The sample provided has ASP.NET Membership already installed and configured, you can download the sample and start explore it.
You page must have ScriptManager control on it, of course this is an ASP.NET AJAX control at the end.
Using CreateUserWizard control:
In order to be able to use the UserName Availability Validator control with CreateUserWizard ASP.NET Control, you need to customize create user step. To do that you can follow these steps:
- Add CreateUserWizard control to your page.
- From the Smart Tag menu select Customize Create User Step.
- Drag and drop Username Availability Control From toolbox just next UserName TextBox. And set ControlToValidate property to UserName TextBox.
For step 3, you can do that through ASPX code all you need to do is to register the control on the page:
<%@ Register Assembly="UAV" Namespace="Encosia" TagPrefix="cc1" %>
Then add the control near UserName TextBox:
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server"
ControlToValidate="UserName" ErrorMessage="User Name is required."
ToolTip="User Name is required."
ValidationGroup="CreateUserWizard" Text="*"/>
<cc1:UsernameAvailabilityValidator ID="UsernameAvailabilityValidator1"
runat="server" ControlToValidate="UserName"
ErrorMessage="Username is not unique" />
The control starts at line 6.
UserNameAvailabilityValidator Properties:
This control inherits from BaseValidator control. So basically it inherits all its properties. Here I'll focus on the new functional properties provided by the exclusively by the control:
- ServicePath: Path of the web service that is responsible to check for UserName availability. Checkout this thread post.
- ServiceMethod: Name of the web method in the web service specified in ServicePath that is responsible for performing the logic to check for UserName availability. It is called when the validation process starts. This method must accept one parameter string called "username" and returns a boolean value. Checkout this thread post.
- MinimumLength: Gets or sets a value indicating minimum number of characters in UserName before validation process take place.
- ValidateOnKeyPress: A boolean value that indicates whether the validation process should take place upon keypress or not.
- KeyPressDelay: Only has effect with ValidateOnKeyPress is set to true. A value in milliseconds to indicate how long after keyup validation happens.
Membership Service:
In order to complete this, a web service is required to check for username availability in data source. And as I'm using ASP.NET Membership Services, this service is very simple. I just need one method as the following:
[WebMethod]
public bool IsUsernameAvailable(string username)
{
return System.Web.Security.Membership.GetUser(username) == null;
}
And here here is the full Control Declaration:
<cc1:UsernameAvailabilityValidator ID="UsernameAvailabilityValidator1"
runat="server"
ControlToValidate="UserName"
ErrorMessage="Username is not unique"
KeyPressDelay="500"
MinimumLength="5"
ServiceMethod="IsUsernameAvailable"
ServicePath="~/MembershipService.asmx"
ValidateOnKeyPress="True"/>
It worth to mention that you can use Page Method as well. Just declare one as the following in your User Creation Page:
[System.Web.Services.WebMethod]
public static bool IsUsernameAvailable(string username)
{
return System.Web.Security.Membership.GetUser(username) == null;
}
Then modify Control ASPX code -Line 8- for the ServicePath to point to your page:
<cc1:UsernameAvailabilityValidator ID="UsernameAvailabilityValidator1"
runat="server"
ControlToValidate="UserName"
ErrorMessage="Username is not unique"
KeyPressDelay="500"
MinimumLength="5"
ServiceMethod="IsUsernameAvailable"
ServicePath="~/CreateUser.aspx"
ValidateOnKeyPress="True"/>
Known Issues:
The control isn't in release yet, this is just an early demo to show how to use it. So there is no wonder if you faced some issues.
The issues that I found are as the following:
- When ValidateOnKeyPress is set to true, any key press will trigger the validation process. Which on the other hand will initiate AJAX request.
- When ValidateOnKeyPress is set to true and KeyPressDelay is set to a small value. And you typed your username very quickly, multiple validation requests will be triggered with multiple AJAX calls to the service. This will cause an error message box to be displayed frequently.
- There is an issue that appears with FF, Opera, Safari and Chrome, not a high rank issue. Immediately after finishing typing your username and just upon validation process starts the error message flicker for few milliseconds and then disappears till the validation completes. I didn't notice this issue with IE7.
Conclusion:
I liked the idea of having a contest around free give away book, it actually brings out the good of being in a community and participate even if you might not win. You'll see different developers thinking together and exchange ideas to build something and resolve its issues.
If you want to participate, return to Dave's posts and visit the project page on CodePlex. I really hope that this control continue developing and enhance its features. It is simple but cool and handy.
You can download this sample and view the demo here.
