Posterous theme by Cory Watilo

Filed under: code

Add a user interface to mailto hyperlinks with a jquery plug-in, manyMail

default mailto link behaviour

Hi,

In an upcoming toepoke.co.uk build we've added the ability to e-mail all the players in a match. Just for simple stuff like arranging lifts, last minute changes, etc.

We didn't want to get into creating a full blown e-mailing system, there's an app for that wink. Instead I simply used a normal mailto link, which brings up whatever e-mail client is installed.

Now this works fine if the user has an e-mail client installed, but what if you use web based e-mail client? If you're interested in knowing more, please read the full article with demos, code and full explanation of the plug-in

Please note that version 1.0.2 has been released today (01-Jul-11).  The article above has been updated to reflect the changes.

Hope you find this useful,
Team TP.

  • kick it on DotNetKicks.com
  • Share
  • Shout it

Capitalise text inputs with css

When I was build the sign-up screen for toepoke.co.uk I wanted to tidy up the data entry for the player’s name so you get capitalised names.

 

Image001

 

My first thought was to tie into the jQuery focus event and do a little JavaScript once the user left the input field.

 

Thankfully I thought a little bit more and remembered the text-transform property in CSS, and wondered if this would work against text input fields as well as heading and p tags, etc.

 

.tcase {

    text-transform: capitalize;

}             

 

Turns out it works quite well, and the above works under most of the modern browsers.  Tested and works in Chrome 7,IE 8, FF 3.6, Safari 5, but sadly not Opera 10.  Of course the text-transform property is an aesthetic effect, so I can live with it not working in Opera. 

 

Also bear in mind if the user enters their name in lowercase, their name will be in lowercase when the form is posted to the server, so you’ll still need to capitalise on the server-side.  Something like the following C# string extension should do the trick. (or see http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitle... as kindly pointed at by Maarten van der Lee below)

 

     /// <summary>

    /// Converts the input string into an initial version, so "fred" or "FRED" becomes "Fred"

    /// </summary>

    /// <param name="input"></param>

    /// <returns></returns>

    public static string ToInitial(this string input)

    {

        if (string.IsNullOrEmpty(input))

            return "";

 

        return 

            input.FirstOrDefault().ToString().ToUpper() +

            input.Substring(1).ToLower();

    }

 

You can see the above in action on our sign-up page (http://toepoke.co.uk/user.aspx/create). Naturally this can be extended to use the uppercase and lowercase text-transform rules (uppercase is quite useful for postcodes too).

Hope this proves useful for someone.

 

Kind regards,

Team TP.

 

 

Find this useful?

kick it on DotNetKicks.com Shout it

Share  

 

Debugging ASP.NET MVC Source Code

This is more for my memory than anything else, but hopefully will be useful for others too.

To debug the ASP.NET MVC source code you need the following steps:

  1. Go to Tools > Options and tick Enable .NET Framework source stepping.
  2. Did it work? If not (like me) continue with the rest of these steps.
  3. First off download the source code. I’m going to be debugging version 2 at : http://aspnet.codeplex.com/releases/view/41742
  4. Once downloaded, extract the zip out (you may want to unblock it first [Right click on the ZIP, Properties > Unblock) otherwise you may hit issues when trying to debug later.
  5. Add the System.Web.Mvc.csproj as an existing project in Visual Studio.
  6. Replace the normal MVC reference in your web project with the new project with the source code.
  7. Open the web.config (the one in the root, not the one in Views):
  8. Comment out {add assembly=“System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL” /}
  9. Add in {add assembly=“System.Web.Mvc” /}
  10. Do a Clean, then Rebuild.
  11. Bit of luck, jobs a good ‘un.

28th Sep 2010 Addendum

I’ve subsequently come across this article that outlines an alternative method by using the source code server in Visual Studio:

http://weblogs.asp.net/gunnarpeipman/archive/2010/07/04/stepping-into-asp-net...

I’ve had a go, but I couldn’t get anything coming out of the watch windows (due to the assemblies being optimised) which kind of limits what you can do (unless I’m doing something wrong – please add a comment if I am!).

Hope this is useful,

Regards,

Team TP!

Finding redundant code in Visual Studio solutions.

In the early days of the development of toepoke.co.uk I didn't have any proper source control management (yeah I know, I know !)

Anyway the days of commenting out code whilst I try stuff out and using my own source control management system are long gone (thankfully!).

However I still have a lot of commented out code hanging around from those days and I really need to get rid of it and to tidy things up. Thing is there's load of files and I didn't fancy going through every single file, so I figured there must be an easier way.

I figured typically I'm commenting out multiple lines of (C#) code, along the lines of this:



//public void SomeLazyCommentedOutMethod()
//{
//}


So all we need to do is search for //{ or // { or basically // followed by anything with { at some point afterwards. Sounds like a regular expression search to me. Ordinarily my ability to build a regular expression from scratch extends to this. But thankfully even I can cope this one!

(:b)*(//)+([a-zA-Z0-9])*\{

Which breaks down to:

  1. (:b)* = Zero or more tabs or spaces, followed by
  2. (//)+ = One or more slashes (/), followed by
  3. ([a-zA-Z0-9])* = Zero or more letters or numbers, followed by
  4. \{ = followed by an opening curly brace ({).

So if you bob (:b)*(//)+([a-zA-Z0-9])*\{ into the Visual Studio Find In Files dialog (CTRL + SHIFT + F) and hit Find All you should find most of the areas where you've commented out code.

C#

c# find in files dialog

VB.NET

vb.net find in files dialog

I reckon you should be able to the same in Visual Basic.NET by replacing the // with ' (so (:b)*(')+([a-zA-Z0-9])*\{ ) - however I've not tried it.

Well I've tried (the C# version) on the toepoke.co.uk code and found 158 hits which on the up side means it's probably working, on the downside I've been a naughty boy !

Regards,
Team TP!

Simple example on using ForEach generic method

Working on toepoke today and I wanted to loop through a list of objects and sync up some properties, so I started off by looping through a normal foreachconstruct. I vaugely remember there was a ForEach method hanging off the intellisense for list objects, so a quick google revealed ... not much (granted I didn't do a extensive search ... I'm very much in the "Not on the first page ... I'll figure it out myself!" camp :-)). Anyway seems like a good opportunity for a blog.So what example to use as an illustration? What about a noddy Personobject with the following properties:

  • Name - useful for seeing what's going on
  • DOB - which we'll generate when create a list of Person
  • Age - which is the property we're going to loop around and calculate.

Which gives us the following noddy class:

 public class Person {
    public string Name { get; set; }
    public DateTime DOB { get; set; }
    public int Age { get; set; }
 }
 

We're going to drive this example through a unit test (see full excert below), but I'll just concerntrate on the relevant bits here.  So next up let's populate a list of people to create our test data.

 List people = new List() {
    new Person() {       Name = "Fred Flintstone", DOB = DateTime.Now.AddYears(-65) },
    new Person() { Name = "Homer Simpson", DOB = DateTime.Now.AddYears(-40) }
 };
 

Simple enough, couple of cases, one aged 65, the other aged 40.  Our simple ForEach will loop through and work out the age of each instance.  To do this we'll just take the DOB away from the current time to delivery the number of years.  The ForEbach method is simply a delegate (which in essence is just a function) which may or may not take any arguments.  Ours isn't taking any arguments (it's a simple example after all :-)).  The code looks like this:

 people.ForEach(
    person => {
       person.Age = (DateTime.Now - person.DOB).TotalYears();
    }
 );
 

Now we're working against a list of type Person which is the person => bit.  The right hand side inside the curly brackets is in basically a method created on the fly for the purposes of the ForEach method.  So in essence the above is saying "for each person in the list of person set the age to the total number of years between the person's date of birth and now".  Doesn't look to scary now does it :-)? The more observant of you will have noticed that TotalYears doesn't exist in a TimeSpan object.  And you'd be absolutely correct; sadly the TimeSpan object doesn't provide a TotalYears method because it's quite complex to work out due to leap years and ... erm ... other stuff that's hard to work out ;-).  So to help out with our example, it's time for a little extension method on the TimeSpan class:

 public static class TimeSpan_Extensions {
    public static int TotalYears(this TimeSpan ts) {       return (int)ts.TotalDays / 365;
    }
 }
 

 

I would recommend you don't use that method for any production code whatsoever, I will guarantee bugs!  But it's perfect for our purposes.  Hopefully you've found this useful, and the full source is posted below, good luck and good searching.

   

 

 

 

 using System;
 using System.Text;
 using System.Collections.Generic;
 using System.Linq;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 namespace Blog.Tests {
    public static class TimeSpan_Extensions {
       public static int TotalYears(this TimeSpan ts) {
          return (int)ts.TotalDays / 365;
       }
    /// Summary description for ForEach
    [TestClass]
    public class ForEach {
       private class Person {
          public string Name { get; set; }
          public DateTime DOB { get; set; }
          public int Age { get; set; }
       }
    public ForEach() {}
     
    [TestMethod]
    public void Can_Calculate_Age_Using_ForEach() {
       List people = new List() {
          new Person() { Name = "Fred Flintstone", DOB = DateTime.Now.AddYears(-65) },
          new Person() { Name = "Homer Simpson", DOB = DateTime.Now.AddYears(-40) }
       };

       // Yup we could use ForEach here too, but I don't want to confuse what we know have to use (foreach) with what we don't (ForEach)! 
       foreach (Person p in people) {
          Assert.AreEqual(0, p.Age);
       }
       people.ForEach( a => {
          a.Age = (DateTime.Now - a.DOB).TotalYears();
       }
);        // element 0 is Fred Flintstone
       Assert.AreEqual(people[0].Age, 65);
       // element 1 is Homer Simpson
       Assert.AreEqual(people[1].Age, 40);
       people.ForEach( a => {
             TestContext.WriteLine(string.Format("Person ({0}, {1}, {2})", a.Name, a.DOB.ToShortDateString(), a.Age) );
          }
       );
    } // Can_Calculate_Age_Using_ForEach
    } // class ForEach
 } // Blog.Tes