Monday, December 28, 2009

Change Background on Invalid Fields

.NET doesn't provide a way to change the background color (or other attributes) of fields that fail validation when using the built-in validators. I think color prompting is a nice way to indicate bad user input, so rather than going to a whole lot of code-behind nonsense you can just shim on to the .NET validation display routine by putting this javascript on your ASPX page:


var OriginalValidatorUpdateDisplay = null;

function NewValidatorUpdateDisplay(val) {
OriginalValidatorUpdateDisplay(val);
if (val.controltovalidate) {
var ctrlIsValid=true;
var ctrl = document.getElementById(val.controltovalidate);
for (var i = 0; i < ctrl.Validators.length; i++) {
if (!ctrl.Validators[i].isvalid) {
ctrlIsValid = false;
}
}
if (ctrlIsValid) {
ctrl.style.backgroundColor = '';
ctrl.style.color = '';
}
else {
ctrl.style.backgroundColor = '#FF0000';
ctrl.style.color = '#FFFFFF';
}
}
}

if (typeof (ValidatorUpdateDisplay) == 'function') {
OriginalValidatorUpdateDisplay = ValidatorUpdateDisplay;
ValidatorUpdateDisplay = NewValidatorUpdateDisplay;
}


That's it!

The only downside is that as-written there is only one "pass" and one "fail" attribute per page. You could change classes on the fly and accomplish more customization, but that sort of violates the elegance of this approach.

Friday, December 18, 2009

Visual Studio GridView designer bug

There is a bug in Visual Studio 2008 that prevents property changes in design view from being reflected back to source code when controls are embedded in wizards or master pages. For simple controls this is not a big deal, but for complex controls, like DevExpress's ASPxGridView, the designer is a huge time saver -- which is the only reason you pay good money for a nice RAD toolkit in the first place.

Luckily, there is a hotfix:
http://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=17185

Get it.

Thursday, August 27, 2009

Move around a transparent AIR window

Some things are made out to be more complex than they have to be. If you want an Adobe AIR application without a standard window frame ("chrome" as they call it) then you have to manage allowing the user to move the window around yourself, as well as a way to close the app. It's harder to find a simple example than it should be.


// $background and $close can be any mouse-active
// display object on the stage, of course

// close the app on clicking a button
$close.addEventListener(MouseEvent.CLICK, closeApp);
function closeApp(ev:MouseEvent)
{
var myWindow:NativeWindow = this.stage.nativeWindow;
if (myWindow) myWindow.close();
}

// drag app around
$background.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
function startMove(ev:MouseEvent)
{
var myWindow:NativeWindow = this.stage.nativeWindow;
if (myWindow) myWindow.startMove();
}


You can do a lot more than that, of course.

Tuesday, August 25, 2009

Access variables passed from HTML in AS3

In AS2 accessing variables passed into Flash from a web page was as easy as referencing _root.{whatever}. AS3 makes things a little more complicated. Why? Just because. Deal with it.


// myVariable is the name of a var passed to the SWF, as in
// <EMBED ...>FlashVars="myVariable=Hello"
// or even
// mymovie.swf?myVariable=Hello

var myVariable:String = LoaderInfo(loaderInfo).parameters["myVariable"] || "defaultValue";
trace(myVariable);
// "Hello"


While working in the IDE, or if the page doesn't supply the proper variable, the "parameters" object property will be undefined. It's always best to set a default value, which is what that
|| "defaultValue"
bit is about.

If you have a lot of variables you need to access... well first off you should consider reading an XML file for configuration, but if that's too much trouble, try this, just for convenience:


var flashVars:Object = new Object();
for (var name in LoaderInfo(loaderInfo).parameters) {
this[name] = LoaderInfo(loaderInfo).parameters[name];
}
// now you can access variables like:
trace(this.myVariable);
// very much like the old AS2 _root.myVariable