Image resizing

Using 3rd party components while essential, can also be a headache. All to often you can find yourself spending hours following the setup instructions to the letter only for it to not work. Other times you know it’s the exact component you want to use but it seems while the guys may have spent a huge effort in writing to component they seemingly never got round to writing anything to tell you how to use it.

Every so often though I come across a component that works first time and does exactly what you want. One such component that I’ve found this with recently ImageResizer.

I needed a simple way for images to be resized for a product listing on an eCommerce platform written in Classic ASP. Image resizing can be a real pain, there’s the decision of do you do it when the images are uploaded or on the fly, if you do it on the fly there’s a a huge number of mistakes you can make and nearly every example of how to do image resizing includes a couple of them. Then lastly in either scenario you need to make sure the end result is actually decent quality.

With image resizer I was done in 5 minutes! It’s actually a .net component rather than Classic ASP but that wasn’t an issue. You create a bin folder, add a couple of simple lines to your web.config file then put an extra extension on the image path plus parameters for the size you want and that’s it!

If your doing a .net project it’s also available on NuGet making it even easier to get up and running.

Custom Validator Error from Server Side

The built in ASP.NET validators are amazing as we all know. You just drag them on the page, tell them what control to validate, give them a summary control to list the errors and they do it. But what if there’s something you need to add server side? Such as something that needs to check with the database before saving. You already have your validation summary control, so it would be nice to re-user that and have everything automatically looking the same. But it would appear there’s no easy way of doing it built in, so here’s an easy way of doing it… 

Creating a Custom Validation Error

First your going to need a class with some static class’s that you can pass your error message to. Here I have two functions one for simply adding the error to the page and the other for adding the error to the page with a specific validation group. I am using a CustomValidator object to make this all work, another option is to implement IValidator but it’s actually more effort than’s needed. The other section to note is that I’m setting the Error.Text to a non breaking space (this is what would normally go next to the form field you’re validating). This is because if you don’t then it will default to the ErrorMessage which we only want to go into the summary. If you try setting it to a normal space it will still also default to the summary text.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for Validator
/// </summary>
public class ValidationError

    public static void Display(string Message)
        Display(Message, "");

  public static void Display(string Message, string ValidationGroup)
  CustomValidator Error = new CustomValidator();
        Error.IsValid = false;
        Error.ErrorMessage = Message;
        Error.ValidationGroup = ValidationGroup;
        Error.Text = "&nbsp;";

  Page currentPage = HttpContext.Current.Handler as Page;

Now to trigger the error you just need to called the function as below:

ValidationError.Display("Useful error message", "ValidationGroupName");