Translate

Wednesday, March 25, 2015

Utillities

public static DataTable ToDataTable(List items)
{
var dt = new DataTable(typeof (T).Name);
PropertyInfo[] props = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var prop in props)
{
dt.Columns.Add(prop.Name, BaseType(prop.PropertyType));
}
foreach (var item in items)
{
try
{
var values = new object[props.Length];
for (var i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
dt.Rows.Add(values);
}
catch (Exception)
{   
}
}
return dt;
}

public static Type BaseType(Type oType)
{
if (oType != null && oType.IsValueType && oType.IsGenericType &&
oType.GetGenericTypeDefinition() == typeof (Nullable<>))
{
return Nullable.GetUnderlyingType(oType);
}
else
{
return oType;
}
}

Monday, March 23, 2015

Custom jQuery validation plugin client side validation rules

When using jQuery Validation plugin setting up a new validator for a Html form is easy. Adding custom rules for validation is just as easy. Creating a custom rule on the client side could be more efficient than doing server side validation after the form is submitted.
Take for instance this javascript that creates a Validation object for a Html5 input element of type number:
// your Validation object
Var myValidator;

// Place custom validation rule here
// setting up the rules and message for the input element
myValidator = $( "#form1" ).validate({
    rules: {
        input1: { // selector of your input textbox
            digits: true,
            minlength: 9,
            maxlength: 9,
            messages: {
                digits: "Value has to be a number",
                minlength: $.validator.format("Value {0} is to low"),
                maxlength: $.validator.format("Value {0} is too high")
            }
        }
    },
    showErrors: function(errorMap, errorList){
            // place you validation error summary rendering code here
        });
    }
});
Now if you had a javascript function bound to the input element's keyup, input events your call to validate the value of the input element could be similar to the following javascript:
$( "#input1" ).bind( "keyup input", function(e) {
        myValidator.element( "#input1" );
    }
});

Now the custom rule part. Take for instance you wanted to prevent a value to be submitted that was already on display in a different element on the form. Instead of performing server side validation or making a separate ajax call, add a custom validation method to the jQuery Validation plugin. (The syntax is available at http://jqueryvalidation.org/jQuery.validator.addMethod/).

// Place custom validation rule here
// addMethod ( method-name-string, function(value, element){
//  validation code
//}, validation-message-string );
// e.g.
$.validator.addMethod( "notequalto", function( value, element ) {
    var currentVal = $( "#txtBoxCurrentValue" ).text();
    if ( value === currentVal ) {
        return false;
    } else {
        return true;
    }
}, "That value is already set" );

Now update you rules section for input1 in your validator object to be:
    rules: {
        input1: { // selector of your textbox
            digits: true,
            minlength: 9,
            maxlength: 9,
            notequalto: true, // no quotes around the new method name, must validate to true
            messages: {
                digits: "Value has to be a number",
                minlength: $.validator.format("Value {0} is to low"),
                maxlength: $.validator.format("Value {0} is too high")

            }

MSBuild TransformXMl Task when converting from VS 2010 to VS 2012

Sometimes within a VS project Xml files are used to hold configuration settings for different build types, e.g., Debug vs. Release, etc.
A build warning will be displayed such as:
The "TransformXml task could not be loaded from the assembly C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Task.dll.

This warning means that you migrated your VS 2010 project into a VS 2012 solution successfully, but, forgot to change the references for VS 2010 MSBuild assemblies to VS 2012 ( which is v11.0 ).

To Fix:
- Unload the project in the VS 2012 IDE, and edit the project file.
- Change all Microsoft.Web.Publishing.Task.dll version v10.0 references to v11.0
- Change the $(MSBuildExtensionsPath) variable name to $(MSBuildExtensionsPath32)

- Save your edits and reload the project

Wednesday, March 4, 2015

Custom jQuery validation plugin client side validation rules

When using jQuery Validation plugin setting up a new validator for a Html form is easy. Adding custom rules for validation is just as easy. Creating a custom rule on the client side could be more efficient than doing server side validation after the form is submitted.
Take for instance this javascript that creates a Validation object for a Html5 input element of type number:
// your Validation object
Var myValidator;

// Place custom validation rule here
// setting up the rules and message for the input element
myValidator = $( "#form1" ).validate({
    rules: {
        input1: { // selector of your input textbox
            digits: true,
            minlength: 9,
            maxlength: 9,
            messages: {
                digits: "Value has to be a number",
                minlength: $.validator.format("Value {0} is to low"),
                maxlength: $.validator.format("Value {0} is too high")
            }
        }
    },
    showErrors: function(errorMap, errorList){
            // place you validation error summary rendering code here
        });
    }
});
Now if you had a javascript function bound to the input element's keyup, input events your call to validate the value of the input element could be similar to the following javascript:
$( "#input1" ).bind( "keyup input", function(e) {
        myValidator.element( "#input1" );
    }
});

Now the custom rule part. Take for instance you wanted to prevent a value to be submitted that was already on display in a different element on the form. Instead of performing server side validation or making a separate ajax call, add a custom validation method to the jQuery Validation plugin. (The syntax is available at http://jqueryvalidation.org/jQuery.validator.addMethod/).

// Place custom validation rule here
// addMethod ( method-name-string, function(value, element){
//  validation code
//}, validation-message-string );
// e.g.
$.validator.addMethod( "notequalto", function( value, element ) {
    var currentVal = $( "#txtBoxCurrentValue" ).text();
    if ( value === currentVal ) {
        return false;
    } else {
        return true;
    }
}, "That value is already set" );

Now update you rules section for input1 in your validator object to be:
    rules: {
        input1: { // selector of your textbox
            digits: true,
            minlength: 9,
            maxlength: 9,
            notequalto: true, // no quotes around the new method name, must validate to true
            messages: {
                digits: "Value has to be a number",
                minlength: $.validator.format("Value {0} is to low"),
                maxlength: $.validator.format("Value {0} is too high")
            }

Now you have a custom validation rule for comparing a value against another Html element on the same form. Happy validating!