Author: Dflying Chen ( Http://dflying.cnblogs.com/ )
I saw the park yesterday.AdonioFriend'sUseCustomvalidatorClass timely verification UsernameThis is a common functional requirement and usesAjaxTo provide a better user experience.AdonioFriends use unpackagedAjaxMethod, in fact, we can fully based on the existingAtlasFramework to develop easy-to-use components.
Here I encapsulate the above functionsAtlasInBehaviorTo facilitate reuse. AboutAtlasOfBehavior, See:InASP. NET AtlasCreate a customBehavior.
Check whether the user name is registeredValidateusernamebehaviorThere are several attributes:
- Servicename: Get or set the check user nameWeb Service. You must specify this attribute.
- Methodname: Get or set the check user nameWeb MethodName. You must specify this attribute.
- Checkresultlabelid: Get or set the display of check resultsDomElementID. You must specify this attribute.
- Validmessage: Get or set the text displayed when the user name is checked (the user name is available.
- Invalidmessage: Get or set the text displayed when the check fails (the user name is unavailable.
At the same time, the server checksWeb MethodYou must have the following signature. Input a user name to be checked and return the boolean result after the check (available/Unavailable ):
Public Bool Checkusername ( String Usernamecandidate)
About thisValidateusernamebehaviorYou can downloadSource codeCombination laterInASP. NET AtlasCreate a customBehaviorThe five-step analysis mentioned in is actually very simple. Note the following:
Function Blurhandler () {
If ( This . Control. element. Value = '') {
_ Checkresultlabel. innerhtml='';
Return;
}
SYS. net. servicemethod. Invoke (
_ Servicename,
_ Methodname,
'',
{Usernamecandidate:This. Control. element. Value} ,
_ Onmethodcomplete
);
}
Function _ Onmethodcomplete (result)
{
_ Checkresultlabel. innerhtml=Result?_ Validmessage: _ invalidmessage;
}
InTextboxWhen the input focus is lostBlurhandler ()Will be called and usedAjaxSends a user name check request to the specifiedWeb Service. In the callback functionOnmethodcomplete (), Set the corresponding display result based on the returned value.
As a developer,Use thisValidateusernamebehaviorVery simpleSee the following example:
First, createWeb ServiceAnd the method to check the user name. Here, the implementation logic is very simple, onlyDflyingAndAdminYou can change the two users according to the actual situation:
Using System;
Using System. Web;
Using System. Web. Services;
Using System. Web. Services. Protocols;
[WebService (namespace = " Http://www.dflying.net/ " )]
[Webservicebinding (conformsto = Wsiprofiles. basicprofile1_1)]
Public Class Userregisterservice: system. Web. Services. WebService
{
Private Static String [] M_userdata = {"Dflying","Admin"} ;
[Webmethod]
Public Bool Checkusername ( String Usernamecandidate)
{
Foreach ( String S In M_userdata)
{
If(S=Usernamecandidate)
Return False;
}
Return True ;
}
}
And then addScriptmanagerAnd reference thisValidateusernamebehavior. jsFile:
< Atlas: scriptmanager Runat = "Server" ID = "Scriptmanager1" >
< Scripts >
< Atlas: scriptreference Path = "Validateusernamebehavior. js" />
</ Scripts >
</ Atlas: scriptmanager >
Then the user is provided to enter the user nameInputAnd the corresponding display of the inspection resultsSpan:
< Div >
Username:
< Input Type = "Text" ID = "Username" Class = "Input" />
< Span ID = "Usernameerror" > </ Span >
</ Div >
FinallyAtlasOfXML scriptInputUpgradedAtlas textboxControl, in itsBehaviorAdd ourValidateusernamebehaviorAnd specifyBehaviorProperties:
< Script Type = "Text/XML-script" >
< Page Xmlns: script = "Http://schemas.microsoft.com/xml-script/2005" >
< Components >
< Textbox ID = "Username" >
< Behaviors >
< Validateusernamebehavior Checkresultlabelid = "Usernameerror" Servicename = "Userregisterservice. asmx"
Methodname = "Checkusername" />
</ Behaviors >
</ Textbox >
</ Components >
</ Page >
</ Script >
Test in the browser and enter an available User Name:
InputDflying, Oh, it has been registered:
This exampleProgramAnd the correspondingValidateusernamebehaviorSourceCodeDownload here: http://files.cnblogs.com/dflying/ValidateUserNameBehavior.zip