Friday, 30 January 2009

CSS "or" separator

<div style="background-color: white; margin-left: -10px; margin-top: 40px; padding: 5px; position: absolute;">or</div>
<div style="border-left: solid 1px #999; height: 8em; margin:auto; width: 1px;"></div>

Example:

or

* Copied from Stack Overflow

Monday, 26 January 2009

0x80048820

I recently kept getting an error after trying to sign into Windows Live Messenger or sync Windows Live Mail. My laptop had been unplugged at the mains for over a week and the date/time settings were scrambled. I noticed the time was wrong but I failed to spot the year was 2002. I simply changed it to 2009 and hey presto, problem solved.

Friday, 16 January 2009

How do I clean up my log files?

Create a scheduled task that will execute the following command:

C:\WINDOWS\system32\forfiles.exe /p C:\logs /s /m *.log /d -7 /c "CMD /C del @FILE"

This example recursively deletes "*.log" files older than 7 days. If you simply wanted to move the files, change the comannd parameter - this provides a nice way to archive.

For further information, see the Forfiles documentation.

Friday, 9 January 2009

Custom controls in ASP.NET MVC

Finally, my article on custom controls in ASP.NET MVC has been published on CodeProject so check it out:

http://www.codeproject.com/KB/custom-controls/MVCCustomControls.aspx

Thursday, 8 January 2009

jQuery watermark plugin

Here is a jQuery plugin for adding a watermark effect to elements (e.g. text box):

(function($) {
// The element that has focus (or null when nothing has focus).
var inputFocus = null;

// This keeps track of the element that has focus.
$(":input").focus(function() {
inputFocus = this;
}).blur(function() {
inputFocus = null;
});

$.fn.hasFocus = function() {
return this.attr("id") == $(inputFocus).attr("id");
}
})(jQuery);

(function($) {
var watermarkDefaults = {
watermarkedClass: "input-watermarked",
watermarkedText: ""
};

$.fn.watermark = function(settings) {
settings = $.extend({}, watermarkDefaults, settings);

var self = this;

self.addWatermark(settings);

if (self.isWatermarked(settings)) {
settings.watermarkedText = self.val();
}

self.focus(function() {
self.removeWatermark(settings);
});

// This might be an expensive exeucution!?
// Added to prevent strange behaviour when the user
// sets the focus before everything has fully loaded.
self.keydown(function() {
self.removeWatermark(settings);
});

self.blur(function() {
self.addWatermark(settings);
});

// Clear the watermark text when the form is submitted.
self.parents("form:first").submit(function() {
self.removeWatermark(settings);
});
}

$.fn.addWatermark = function(settings) {
settings = $.extend({}, watermarkDefaults, settings);

if (!this.hasFocus() && (this.val().length === 0 this.val() === settings.watermarkedText)) {
this.addClass(settings.watermarkedClass);

this.val(settings.watermarkedText);
}
}

$.fn.isWatermarked = function(settings) {
settings = $.extend({}, watermarkDefaults, settings);

return this.hasClass(settings.watermarkedClass)/* && this.val() === settings.watermarkedText*/;
}

$.fn.removeWatermark = function(settings) {
settings = $.extend({}, watermarkDefaults, settings);

if (this.isWatermarked(settings)) {
this.removeClass(settings.watermarkedClass);

this.val("");
}
}
})(jQuery);

Examples:

$(function(){
$("#id1").watermark({
watermarkedClass: "custom-input-watermarked",
watermarkedText: "Click here..."
});
});

$(function(){
$("#id2").watermark({
watermarkedText: "Click here..."
});
});

Tuesday, 6 January 2009

EventType clr20r3

I was recently on call when I got alerted to a scheduled task that failed to run - the following error was added to the Event Viewer:

EventType clr20r3, P1 MyApp.exe, P2 1.0.0.21357, P3 4935215a, P4 MyApp, P5 1.0.0.21357, P6 4935215a, P7 5, P8 1, P9 system.typeinitialization, P10 NIL

The exe is executed on the first day of every month and we didn't have any problems the previous time. After doing some digging, I got the impression there wasn't a single cause as everyone had a different story. One possible explanation was unhandled exceptions but I knew the application hadn't been updated since it's last execution. What I failed to notice was a config file that had been updated - some required settings simply vanished. Running the exe through a command line interface returned the exception which highlighted the missing values.

Monday, 5 January 2009

jQuery $(document).ready()

We all know that anything inside the $(document).ready() function will execute in between loading the DOM and content. Here is a syntax alternative for those who want to save a few characters:

$(function(){
// Do something.
});

Vs

$(document).ready(function() {
// Do something.
});