Sunday, December 13, 2009

jQuery Watermark Plugin

The following code was adapter from Very simple textbox watermark using JQuery and converted into a reusable jQuery plugin.

Given the following text box

<input type="text" id="textBox1" class="watermarkOn"
title="Type here" value="Type here" />

Add the following CSS style:
<style type="text/css">
.watermarkOn {
color: #CCCCCC;
font-style: italic;
}
</style>


jQuery Plugin Code

(function($) {
$.fn.extend({
//plugin name - watermark
watermark: function(options) {

//Settings list and the default values
var defaults = {
css: 'watermarkOn',
tag: 'title'
};

var options = $.extend(defaults, options);

return this.each(function() {
var o = options;

// When the textbox comes under focus:
// 1. Remove the watermark class and
// 2. clear the text box
$(this).focus(function() {

$(this).filter(function() {
//We only want this to apply if
//nothing was actually entered
return $(this).val() == "" ||
$(this).val() == $(this).attr(o.tag)

}).removeClass(o.css).val("");

});

// When the textbox loses focus:
// 1. Add the watermark class and
// 2. Set the default text
$(this).blur(function() {

$(this).filter(function() {
//We only want this to apply if
//nothing was actually entered
return $(this).val() == ""

}).addClass(o.css).val($(this).attr(o.tag));

});

});
}
});
})(jQuery);


Add the following lines to your document.ready function.
$(document).ready(function() {
$("#textBox1").watermark({css:"watermarkOn",tag:"title"});
});

Wednesday, November 25, 2009

JavaScript Object via JQuery Ajax to an Asp.Net WebMethod

Given the following WebMethod

[WebMethod]
public static string UpdatePerson(Person person)
{
Person p = Repository.Instance.Get<Person>(person.Guid);
p.Name = person.Name;
p.Age = person.Age;
Repository.Instance.Update(p);

var result = new WebMethodResult<Person>();
result.Entity = p;
return result.ToJson();
}

[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public string Modified { get; set; }
}

You can call this UpdatePerson method using the following jQuery code snippet.

function updatePerson() {
var request = {};
var person = {};
person.name = "Daffy Duck";
person.age = 12;
person.title = "Clown"; //Note that title is not a valid property
request.person = person;

var dataString = $.toJSON(request); //using jquery.json
$.blockUI();

$.ajax({
type: "POST",
url: "Default.aspx/UpdatePerson",
data: dataString,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(json) {
var data = eval('(' + json.d + ')');
if (data.hasErrors)
alert(data.message);
else {
alert("updated " + data.entity.Modified);
}

$.unblockUI();
}
});
}

Friday, September 25, 2009

Color Extensions: ToArgb

Cool extension from http://www.jeff.wilcox.name
internal static class ColorExtensions
{
public static int ToArgb(this Color color)
{
int argb = color.A << 24;
argb += color.R << 16;
argb += color.G << 8;
argb += color.B;

return argb;
}
}

Monday, September 14, 2009

Xml Serialization Helper Class


public static class XmlHelper
{
public static string ToXml(this object obj)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
using (StringWriter sw = new StringWriter())
{
serializer.Serialize(sw, obj);
return sw.ToString();
}
}

public static T FromXml<T>(this string data)
{
XmlSerializer s = new XmlSerializer(typeof(T));
using (StringReader reader = new StringReader(data))
{
object obj = s.Deserialize(reader);
return (T)obj;
}
}
}



So we can do this:


string personString = person.ToXml();
Person newPerson = personString.FromXml<Person>();

Monday, August 24, 2009

TypeConverter - DbType to >NET Type


public static class TypeConverter

{

private static readonly Dictionary typeToDbType = new Dictionary

{

{ typeof(string), DbType.String },

{ typeof(DateTime), DbType.DateTime },

{ typeof(DateTime?), DbType.DateTime },

{ typeof(int), DbType.Int32 },

{ typeof(int?), DbType.Int32 },

{ typeof(long), DbType.Int64 },

{ typeof(long?), DbType.Int64 },

{ typeof(bool), DbType.Boolean },

{ typeof(bool?), DbType.Boolean },

{ typeof(byte[]), DbType.Binary },

{ typeof(decimal), DbType.Decimal },

{ typeof(decimal?), DbType.Decimal },

{ typeof(double), DbType.Double },

{ typeof(double?), DbType.Double },

{ typeof(float), DbType.Single },

{ typeof(float?), DbType.Single },

{ typeof(Guid), DbType.Guid },

{ typeof(Guid?), DbType.Guid }

};



public static DbType ToDbType(Type type)

{

if (!typeToDbType.ContainsKey(type))

{

throw new InvalidOperationException(string.Format("Type {0} doesn't have a matching DbType configured", type.FullName));

}



return typeToDbType[type];

}

Wednesday, July 22, 2009

Card shuffling and dealing program

Simple card shuffling and dealing program in java (no Jokers)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DeckOfCards extends JFrame {
private Card deck[];
private int currentCard;
private JButton dealButton, shuffleButton;
private JTextField displayCard;
private JLabel status;

public DeckOfCards() {
super( "Card Dealing Program" );

String faces[] = { "Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };

deck = new Card[ 52 ];
currentCard = -1;

for ( int i = 0; i < deck.length; i++ )
deck[ i ] = new Card( faces[ i % 13 ], suits[ i / 13 ] );

Container c = getContentPane();
c.setLayout( new FlowLayout() );

dealButton = new JButton( "Deal card" );
dealButton.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e )
{
Card dealt = dealCard();

if ( dealt != null )
{
displayCard.setText( dealt.toString() );
status.setText( "Card #: " + currentCard );
}
else
{
displayCard.setText( "NO MORE CARDS TO DEAL" );
status.setText( "Shuffle cards to continue" );
}
}
} );
c.add( dealButton );

shuffleButton = new JButton( "Shuffle cards" );
shuffleButton.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e )
{
displayCard.setText( "SHUFFLING ..." );
shuffle();
displayCard.setText( "DECK IS SHUFFLED" );
}
});
c.add( shuffleButton );

displayCard = new JTextField( 20 );
displayCard.setEditable( false );
c.add( displayCard );

status = new JLabel();
c.add( status );

setSize( 275, 120 ); // set the window size
show(); // show the window
}

public void shuffle()
{
currentCard = -1;

for ( int i = 0; i < deck.length; i++ ) {
int j = ( int ) ( Math.random() * 52 );
Card temp = deck[ i ]; // swap
deck[ i ] = deck[ j ]; // the
deck[ j ] = temp; // cards
}

dealButton.setEnabled( true );
}

public Card dealCard() {
if ( ++currentCard < deck.length )
return deck[ currentCard ];
else
{
dealButton.setEnabled( false );
return null;
}
}

public static void main( String args[] ) {
DeckOfCards app = new DeckOfCards();

app.addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
} );
}
}

class Card {
private String face;
private String suit;

public Card( String f, String s ) {
face = f;
suit = s;
}

public String toString() {
return face + " of " + suit;
}
}

Thursday, July 2, 2009

IE Min/Max CSS Width

#page-wrap {
min-width: 960px; max-width: 1260px;
margin: 0 auto; padding: 0 10px;
width:expression(document.body.clientWidth < 961? "960px" : document.body.clientWidth > 1261? "1260px" : "auto");
}