Using the jQuery UI Datepicker with Fancybox

0

Posted by Joe | Posted in jQuery | Posted on 25-08-2010

I just wasted what seems to be far too much of my life (about 30 minutes), trying to figure out why the jQuery UI Datapicker wouldn’t work when it was applied to a text box within a Fancybox.

It turns out it was working, but it was being placed behind the Fancybox which had it’s z-index set to 1101.

Adding the following CSS worked a treat

#ui-datepicker-div
{
    z-index: 1102 !important;
}

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

ASP.NET MVC authorize attribute using action parameters with the ActionFilterAttribute

1

Posted by Joe | Posted in C#, MVC | Posted on 19-08-2010

ASP.NET MVC provides the AuthorizeAttribute which ensures there is a logged in user. You can also provide parameters to restrict actions or controllers to only be accessible to certain roles or users. You can also create your own custom authorization attribute derived from AuthorizeAttribute to provide any custom authorization.

In addition to this general authorization you may want to restrict access based on the current user and a parameter from the action. For example, say you have an action method to edit the details of a product.  You would pass the ID of the product to the action method, and you may only want certain users to be able to edit this particular product.  The AuthorizeAttribute doesn’t allow you to do this but you can create your own attribute derived from ActionFilterAttribute which gives you the desired result.

Read the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Linq lambda expression IEqualityComparer for IEnumerable.Distinct and Except

2

Posted by Joe | Posted in C#, Linq | Posted on 17-08-2010

One of the things that annoys me with the IEnumerable.Distinct method is that it has no overload allowing you to give a lambda expression to specify a particular property to perform the distinction, you have to give an IEqualityComparer.

I did a quick Google search and found this post. The guy here provides the following LambdaComparer class:

Read the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

ASP.NET MVC Textbox with characters remaining HtmlHelper extension method

1

Posted by Joe | Posted in C#, JavaScript, MVC | Posted on 14-08-2010

Here is a couple of HtmlHelper extension methods, CharactersRemainingTextBoxFor and CharactersRemainingTextAreaFor which render a textbox/textarea with a span tag that displays the number of characters remaining.  They also include the JavaScript to make this work, and to limit the number of characters entered in the textarea. The maximum limit comes from the model by using the StringLengthAttribute.

Download source

Read the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

WordPress automatic upgrade timeout

0

Posted by Joe | Posted in Personal | Posted on 14-08-2010

I was just trying to upgrade my wordpress blog to version 3 using the automatic upgrade but it kept stopping after 30 seconds while unpacking the update. I checked on my server and it hadn’t completed the unpacking.  I loaded up the PHP.ini file and after a look around found the max_execution_time setting which was set to 30 seconds. I increased this and the update worked fine.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Dell Warranty Ripoff

1

Posted by Joe | Posted in Personal | Posted on 12-07-2010

About 15 months ago I bought a new Dell Studio 17; with the added options is cost me around $3,500. I thought I paid extra for an additional 12 month warranty on top of the standard 12 months but when I rang recently to get a faulty hard drive replaced it turned out my warranty had expired.

The guy on the phone went through to sales to get me a quote for a new 12 month warranty. He came back with a “discounted” price of $700, with some sales spiel about how with this warranty my “hard drive problem would definately be rectified”. When I told him that was ridiculous and that I could fix it myself he backtracked and said they could get the price down as that cost included all sorts of other crap I didn’t ask for… not so much Mr Dell.

I ended up buying a new 500GB 7,200rpm Seagate drive for around $100, swapping it over myself in about 5 minutes, then used Acronis Home to image my failing drive onto the new one to avoid having to reinstall everything (which I doubt Dell would have done).

It irritates me that Dell try this kind of BS as people who don’t know better would just pay. I guess the fact that I also bought a Dell Netbook just a couple of months ago doesn’t count for anything either.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

How to convert a JSON date serialized by an ASMX web service to a JavaScript Date object

1

Posted by Joe | Posted in ASP.NET, Ajax, JavaScript | Posted on 29-06-2010

JSON doesn’t have a standard way to represent a date. You can read about the reasons behind this here.

If you are using an ASMX web service returning JSON then you’ll find it serializes the DateTime object to a string that looks like this:

/Date(1278943200000)/

The numer in this string is the number of milliseconds since January 1st 1970 UTC, and this number can be used as a constructor argument to the JavaScript Date object.  So all we need to do is extract the number of milliseconds from the string.  This can be done easily using the regular expression shown in the post linked above.

var dateString = "/Date(1278943200000)/";
var date = new Date(parseInt(dateString.replace(/\/Date\((\d+)\)\//, '$1')));

In the above example I’ve manually set the date string, but you may get this from an AJAX response from the ASMX web service.  I then create a new Date object by using the regular expression and parsing the result at an integer.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

How to get the time difference between two DateTimes using C#

0

Posted by Joe | Posted in C# | Posted on 25-06-2010

Here’s a quick an easy way to show the time difference between two DateTime objects using C#.

The DateTime structure has an overridden subtract operator which return a TimeSpan object when subtracting two DateTimes:

public static TimeSpan operator -(DateTime d1, DateTime d2);

You can then use this TimeSpan to get the amount of time between each DateTime:


DateTime fromDate = DateTime.Now.AddDays(-1).AddMinutes(-10).AddSeconds(-20);
DateTime toDate = DateTime.Now;
TimeSpan timeSpan = toDate - fromDate;

string timeString =
    string.Format(
        "{0:00}:{1:00}:{2:00}",
        (timeSpan.Days * 24) + timeSpan.Hours,
        timeSpan.Minutes,
        timeSpan.Seconds
    );

In the above example I’m comparing a date in the past to the current date. This could be useful for working out the total amount of time that has passed since something has happened.

I’m then using the Hours, Minutes and Seconds properties of the TimeSpan to format a string displaying the amount of time. If the hours goes over 24 then the Days property is used, so for my hours I’m also adding on the number of days multiplied by 24.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Convert a comma separated string of numbers to an integer array using C#

0

Posted by Joe | Posted in C#, Linq | Posted on 10-06-2010

Today I needed to convert a comma separated string of numbers in an integer array. Here is how you can do it in one line using Linq:

string csv = "1,1,2,3,5,8,13,21,34";
int[] numbers = csv.Split(',').Select(n => int.Parse(n)).ToArray();

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Running MSTest on your build server without Visual Studio

0

Posted by Joe | Posted in Visual Studio 2008 | Posted on 25-05-2010

Recently I had a problem where I added a Visual Studio Test Project to a solution.

As part of our CI, the build server failed to build as it didn’t contain the necessary files for the test framework. One option would have been to install Visual Studio Team System on the build server, but it seems a bit crazy to do that just to get the tests to build.

I found a post here by Mark Kharitonov that explains a method to get tests building without having to install Visual Studio which worked perfectly for me.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit