The prop snippet and Visual Studio 2008

by Henrik Stenbæk 15. March 2008 19:34

In Visual Studio 2005 typing "prop[tab][tab]" will give you something like this:

vs2005

This is fine and one of the most time saving code snippets ever invented. If one type the same in Visual Studio 2008 it's ends out a little different:

vs2008

This is due to the new Automatic Properties Feature in .NET 3.0/VS 2008. This is somehow also fine as long as one don't want to maintain a .NET 2.0 project with VS 2008, doing this will end up with an compile time error saying:".. must declare a body because it is not marked abstract or extern"

must declare a body because it is not marked abstract or extern

A solution to this is to download this file:

prop.zip (604,00 bytes)

place in your "My Code Snippets" folder:

C:\users\*your user name*\Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets

Now you can simply type propp[tab][tab] in Visual Studio 2008 and get the VS 2005 style property snippet. Why propp? After typing porp your finger is over the [p] button so I thought it would be the fastest solution. If you don't like it: open the prop.snippet file with Visual Studio or any text editor and edit the shortcut tag:

<Shortcut>propp</Shortcut>

Currently rated 4.5 by 6 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories: Source code

FlickrRSS user control (for BlogEngine.Net)

by Henrik Stenbæk 19. February 2008 17:42

When I started out creating this control my idea was to create a Flickr photo plug-in for BlogEngine.Net but after a while I realised that I could just create a traditional ASP.NET user control for reading RSS streams from Flickr. So here it is:

FlickrRSS.ascx the user control that allows you to display Flickr photos on your weblog, homepage or wherever you have an ASP.NET based web application.

The user control supports user, public and group photostreams.

 

How to use the control

Download the control FlickrRSS.ascx.zip (1,96 kb) and unzip the files to your project (FlickrRSS.ascx and FlickrRSS.ascx.cs).

Register the control in the page where you want to show the pictures:

<%@ Register src="FlickrRSS.ascx" tagname="FlickrControl" tagprefix="onesoft" %>

Add the control to the page:
<onesoft:FlickrControl ID="FlickrControl1" runat="server" />

Properties

RssType The type of rss read from flickr the options are: User, Group, Allpublic
FlickrId Optional if RssType=Allpublic. Required if RssType is User or Group. This must be a valid Flickr userid or groupid, this is not the same as username or groupname the format is something like 12345678@00,
Tags Optional. a comma separated list of tags with no spaces eg.: Tags="adam,laura" – find photos tagged with Adam AND Laura
NumberOfImages The maximum number of images to appear in the control.
CacheDuration The time in minutes that the RSS result is cached on the server

Output

The control returns a list of thumbnails in the format:

<ul>
   <li>....</li>
   <li>....</li>
</ul>

And can easy be formatted with CSS ;-)

<onesoft:FlickrControl ID="FlickrControl1" runat="server" RssType="user"
        CacheDuration="10" FlickrId="34434281@N00" NumberOfImages="8"
        Tags="celina" />

returns this (after some formatting):

flickr

 

Download

FlickrRSS.ascx.zip (1,96 kb)

 

Improvements

This control could be better - but how? Any comments and suggestions are welcome.

  • The XML read must be asynchronous

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories: Source code | Featured

ASP.NET AJAX username availability with suggestions

by Henrik Stenbæk 19. July 2007 19:13

Based on my resent post, I have rewritten the code and turned it in to a custom control:

  • It is no longer depending on the Futures CTP.
  • If the username isn&rsquo;t available it&rsquo;s possible to get suggestions with available names.
  • The web service UsernameResult is now returning a class which is automatically JSON serialized by ASP.NET AJAX and passed into JavaScript as a parameter.
  • Fully integrated with the asp.net 2.0 membership.

Try out the online demo.

 

How to use the control

Register the control on the page:

<%@ Register TagPrefix="one" Namespace="onesoft.common.controls" %>

Then add the control

Remember to set ControlToTest - just like on a validator control :-). All the other properties has defaults as shown above.

Add a ScriptManager to your page (before the usernameAvailability control) and define a ServiceReferance:

<one:usernameAvailability runat="server" ID="IsAvalible" 
ControlToTest="Username" 
ForeColor="DimGray" 
NotAvailableTextColor="Red" 
AvailableTextColor="CornflowerBlue" 
StartUpTextColor="DimGray" 
AvailableText="{0} is available" 
NotAvailableText="{0} is not available" 
StartUpText="Type a username" 
SuggestionLinkText="Suggest" 
UseSuggestion="true"></one:usernameAvailability> 

The webservice has 2 functions: 

IsAvailable - checking the availability of the current name
suggestAvailable &ndash; returning suggestions for available name.

Remember to include the file UsernameService.asmx to your root and add UsernameService.cs to App_Code.

That's it - now start checking for available usernames.

Try it out with the online demo - type your preferred name, if it's available create the new user and retry with the same name - now try the suggestion link.... ok I know it isn't clever, but it is available usernames ;-)


Download the full sourcecode including the demo.

AJAXUsernameControl.zip (257,01 kb)

I welcome any comments and suggestions.

Enjoy ;-

Henrik

Currently rated 3.9 by 9 people

  • Currently 3.888889/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: AJAX | Source code | Featured

ASP.NET AJAX username availability check without UpdatePanel

by Henrik Stenbæk 9. July 2007 17:03

 

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)

Currently rated 3.9 by 10 people

  • Currently 3.9/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: AJAX | Source code