Monthly Archives: November 2015

Handling CSS Classes with HtmlTextWriter

using System.Collections.Generic;
using System.IO;
using System.Web.UI;

public class HtmlTextWriter : System.Web.UI.HtmlTextWriter
{
    private ICollection<string> cssClasses;

    public HtmlTextWriter(TextWriter writer)
        : base(writer)
    {
        cssClasses = new List<string>();
    }

    public void AddCssClass(string cssClass)
    {
        if (!cssClasses.Contains(cssClass))
        {
            cssClasses.Add(cssClass);
        }
    }

    private void AddCssClassAttribute()
    {
        AddAttribute(HtmlTextWriterAttribute.Class, string.Join(" ", cssClasses));
        cssClasses.Clear();
    }

    public override void RenderBeginTag(HtmlTextWriterTag tagKey)
    {
        AddCssClassAttribute();
        base.RenderBeginTag(tagKey);
    }

    public override void RenderBeginTag(string tagName)
    {
        AddCssClassAttribute();
        base.RenderBeginTag(tagName);
    }
}

Relative Date Validation in ASP.NET MVC

using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public sealed class LaterThanAttribute : ValidationAttribute
{
    public override bool RequiresValidationContext
    {
        get { return true; }
    }

    public string ComparedPropertyName { get; private set; }
    public string ComparedPropertyDisplayName { get; set; }
    public bool AllowEqual { get; set; }
    public bool IgnoreTime { get; set; }

    public LaterThanAttribute(string comparedPropertyName)
    {
        ComparedPropertyName = comparedPropertyName;
        AllowEqual = false;
        IgnoreTime = false;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value == null)
        {
            return ValidationResult.Success;
        }
        DateTime dateTime = (DateTime)value;
        PropertyInfo comparedProperty = validationContext.ObjectType.GetProperty(ComparedPropertyName);
        object comparedValue = comparedProperty.GetValue(validationContext.ObjectInstance);
        if (comparedValue == null)
        {
            return ValidationResult.Success;
        }
        DateTime comparedDateTime = (DateTime)comparedValue;
        if (IgnoreTime)
        {
            dateTime = dateTime.Date;
            comparedDateTime = comparedDateTime.Date;
        }
        if (dateTime > comparedDateTime)
        {
            return ValidationResult.Success;
        }
        else
        {
            if (AllowEqual && dateTime == comparedDateTime)
            {
                return ValidationResult.Success;
            }
            else
            {
                return new ValidationResult(string.Format(
                    "The {0} field must be later than{1}the {2} field.",
                    validationContext.DisplayName,
                    AllowEqual ? " or equal to " : " ",
                    ComparedPropertyDisplayName ?? ComparedPropertyName));
            }
        }
    }
}