<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Travis Collins  : PageMethods</title><link>http://weblogs.asp.net/traviscollins/archive/tags/PageMethods/default.aspx</link><description>Tags: PageMethods</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Check Username Availability with ASP.NET AJAX</title><link>http://weblogs.asp.net/traviscollins/archive/2008/02/12/check-username-availability-with-asp-net-ajax.aspx</link><pubDate>Tue, 12 Feb 2008 13:49:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5763723</guid><dc:creator>travistx</dc:creator><author>travistx</author><slash:comments>24</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/traviscollins/rsscomments.aspx?PostID=5763723</wfw:commentRss><comments>http://weblogs.asp.net/traviscollins/archive/2008/02/12/check-username-availability-with-asp-net-ajax.aspx#comments</comments><description>&lt;P&gt;Here is a little trick you can use to spice up your asp.net registration pages. I will use ASP.NET AJAX to inform the user whether the username they have entered is available. Rather than use the UpdatePanel, I will slim down the amount of data going over the wire for each ajax request. I will accomplish this using the PageMethods feature.&lt;/P&gt;
&lt;P&gt;This tutorial assumes that you have a working knowledge of how to start an ASP.NET AJAX web application, drop a script manager control onto your form, and use the CreateUser wizard control, or another method of registering users.&amp;nbsp; A little JavaScript knowledge will help too.&lt;/P&gt;
&lt;P&gt;In order to use PageMethods, I will need to set the EnablePageMethods property to true on my ScriptManager.&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: gray 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: gray 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 8pt; OVERFLOW-X: auto; PADDING-BOTTOM: 20px; MARGIN: 20px 0px 10px; BORDER-LEFT: gray 1px solid; WIDTH: 97.5%; CURSOR: text; LINE-HEIGHT: 12pt; PADDING-TOP: 4px; BORDER-BOTTOM: gray 1px solid; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BACKGROUND-COLOR: #f4f4f4"&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&amp;lt;asp:ScriptManager ID=&lt;SPAN style="COLOR: #006080"&gt;"ScriptManager1"&lt;/SPAN&gt; runat=&lt;SPAN style="COLOR: #006080"&gt;"server"&lt;/SPAN&gt; EnablePageMethods=&lt;SPAN style="COLOR: #006080"&gt;"True"&lt;/SPAN&gt; /&amp;gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;DIV&gt;My registration page is called register.aspx. I will create a static method in the code behind for this page. The method will check to see if a username exists, and return a Boolean indicating whether the username is available or not.&lt;/DIV&gt;
&lt;DIV style="BORDER-RIGHT: gray 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: gray 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 8pt; PADDING-BOTTOM: 4px; MARGIN: 20px 0px 10px; OVERFLOW: auto; BORDER-LEFT: gray 1px solid; WIDTH: 97.5%; CURSOR: text; MAX-HEIGHT: 200px; LINE-HEIGHT: 12pt; PADDING-TOP: 4px; BORDER-BOTTOM: gray 1px solid; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BACKGROUND-COLOR: #f4f4f4"&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;[WebMethod]
&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;bool&lt;/SPAN&gt; IsUserAvailable(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; username) 
{
    MembershipUser usr = Membership.GetUser(username);
     &lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt; (usr == &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;);
}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The WebMethod attribute is needed to mark this as a method that can be executed from Javascript.&lt;/P&gt;
&lt;P&gt;Now I will go ahead and create some markup create some markup for my username field on your registration page. Something like this should do.&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: gray 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: gray 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 8pt; OVERFLOW-X: auto; PADDING-BOTTOM: 20px; MARGIN: 20px 0px 10px; BORDER-LEFT: gray 1px solid; WIDTH: 97.5%; CURSOR: text; MAX-HEIGHT: 200px; LINE-HEIGHT: 12pt; PADDING-TOP: 4px; BORDER-BOTTOM: gray 1px solid; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BACKGROUND-COLOR: #f4f4f4"&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;asp:Label&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;ID&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="lblUserName"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="server"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;AssociatedControlID&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="UserName"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;Desired Username&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;asp:Label&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;asp:TextBox&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;ID&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="UserName"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="server"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;CssClass&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="txt"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;onkeyup&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="usernameChecker(this.value);"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;/&amp;gt;&lt;/SPAN&gt;
&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;span&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;="spanAvailability"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;span&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;There are a couple of things to take note of here. The onkeyup javascript event will be fired every keypress, and will be used to check the availability of the username entered.&amp;nbsp; Also take note of the span with an id of spanAvailability. The JavaScript will use this span to write output letting the user know whether the username is available.&lt;/P&gt;
&lt;P&gt;Here's the last part, the Javascript that wires everything together.&amp;nbsp; I will insert this at the bottom of my register.aspx page:&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: gray 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: gray 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 8pt; PADDING-BOTTOM: 20px; MARGIN: 20px 0px 10px; OVERFLOW: auto; BORDER-LEFT: gray 1px solid; WIDTH: 97.5%; CURSOR: text; LINE-HEIGHT: 12pt; PADDING-TOP: 4px; BORDER-BOTTOM: gray 1px solid; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BACKGROUND-COLOR: #f4f4f4"&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&amp;lt;script type=&lt;SPAN style="COLOR: #006080"&gt;"text/javascript"&lt;/SPAN&gt;&amp;gt;
&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt; usernameCheckerTimer;
&lt;SPAN style="COLOR: #0000ff"&gt;var&lt;/SPAN&gt; spanAvailability = $get(&lt;SPAN style="COLOR: #006080"&gt;"spanAvailability"&lt;/SPAN&gt;);

&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt; usernameChecker(username) 
{
    clearTimeout(usernameCheckerTimer);
    &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (username.length == 0)
        spanAvailability.innerHTML = &lt;SPAN style="COLOR: #006080"&gt;""&lt;/SPAN&gt;;
    &lt;SPAN style="COLOR: #0000ff"&gt;else&lt;/SPAN&gt;
    {
        spanAvailability.innerHTML = &lt;SPAN style="COLOR: #006080"&gt;"&amp;lt;span style='color: #ccc;'&amp;gt;checking...&amp;lt;/span&amp;gt;"&lt;/SPAN&gt;;
        usernameCheckerTimer = setTimeout(&lt;SPAN style="COLOR: #006080"&gt;"checkUsernameUsage('"&lt;/SPAN&gt; + username + &lt;SPAN style="COLOR: #006080"&gt;"');"&lt;/SPAN&gt;, 750);
    }
}

&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt; checkUsernameUsage(username) 
{
    &lt;SPAN style="COLOR: #008000"&gt;// initiate the ajax pagemethod call&lt;/SPAN&gt;
    &lt;SPAN style="COLOR: #008000"&gt;// upon completion, the OnSucceded callback will be executed&lt;/SPAN&gt;
    PageMethods.IsUserAvailable(username, OnSucceeded);
}

&lt;SPAN style="COLOR: #008000"&gt;// Callback function invoked on successful completion of the page method.&lt;/SPAN&gt;
&lt;SPAN style="COLOR: #0000ff"&gt;function&lt;/SPAN&gt; OnSucceeded(result, userContext, methodName) 
{
    &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (methodName == &lt;SPAN style="COLOR: #006080"&gt;"IsUserAvailable"&lt;/SPAN&gt;)
    {
        &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (result == &lt;SPAN style="COLOR: #0000ff"&gt;true&lt;/SPAN&gt;)
            spanAvailability.innerHTML = &lt;SPAN style="COLOR: #006080"&gt;"&amp;lt;span style='color: DarkGreen;'&amp;gt;Available&amp;lt;/span&amp;gt;"&lt;/SPAN&gt;;
        &lt;SPAN style="COLOR: #0000ff"&gt;else&lt;/SPAN&gt;
            spanAvailability.innerHTML = &lt;SPAN style="COLOR: #006080"&gt;"&amp;lt;span style='color: Red;'&amp;gt;Unavailable&amp;lt;/span&amp;gt;"&lt;/SPAN&gt;;
    }
}
&amp;lt;/script&amp;gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This effect is pretty easy to do, and adds a nice value to the user experience.&amp;nbsp; I hope you are able to find use for this technique in your applications.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt;&amp;nbsp;Download a sample project &lt;A class="" title=here href="http://weblogs.asp.net/blogs/traviscollins/Examples/CheckUsernameWithAJAX.zip" mce_href="http://weblogs.asp.net/blogs/traviscollins/Examples/CheckUsernameWithAJAX.zip"&gt;here&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Unit next time,&lt;/P&gt;
&lt;P&gt;Travis&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5763723" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/traviscollins/archive/tags/AJAX/default.aspx">AJAX</category><category domain="http://weblogs.asp.net/traviscollins/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/traviscollins/archive/tags/Membership/default.aspx">Membership</category><category domain="http://weblogs.asp.net/traviscollins/archive/tags/PageMethods/default.aspx">PageMethods</category></item></channel></rss>