Use ASP. Net Atlas to develop custom behavior that verifies user name registration in real time

Source: Internet
Author: User
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:

    1. Servicename: Get or set the check user nameWeb Service. You must specify this attribute.
    2. Methodname: Get or set the check user nameWeb MethodName. You must specify this attribute.
    3. Checkresultlabelid: Get or set the display of check resultsDomElementID. You must specify this attribute.
    4. Validmessage: Get or set the text displayed when the user name is checked (the user name is available.
    5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.