Friday, 5 October 2012

Using jQuery ValidationEngine in an ASP.NET - multiple forms

If you have found this post then chances are you are using the great ValidationEngine (jQuery-Validation-Engine) plugin with your ASP.NET application.

Everything work fantastic until you want two separate sets of validation on one page.  Within PHP you can simply wrap each logical sets of validation within their own <form> tags but this is not possible with asp.net.

Instead this is the approach I have used:

Within a set of script tags at the top of my page I put the following (I do have "var $j = jQuery.noConflict();" ):

     $j(document).ready(function () {
        $j("#btnSave").click(function (e) {
            var stopForm = false;
            if ($j("#txtForename").val() == "") {
                $j('#txtForename').validationEngine('showPrompt', 'You must enter a First name', 'error', 'topRight', true);
                stopForm = true;
            } else {
                $j('#txtForename').validationEngine('hide');
            }
   if (stopForm) {
                e.preventDefault();
            }
        });

  1. The above waits for the click event to be fired on the btnSave button.
  2. Sets a boolean variable to false
  3. Checks to see the that textbox object txtForename has a value
  4. If it doesn't that it attaches a popup notification and sets the stopForm variable to true
  5. If there is a value it makes sure that any previous popup that may have been added to the control is closed
  6. Finally the method checks to see if the stopForm variable is set to true, if it is then it calls the e.preventDefault() menthod which will ensure that the btnSave click event is not passed back to the server, allowing your user to address the highlighted issue.
I recommend having the ClientIDMode="Static" declaration on your page to ensure that jQuery can get expected access tot he controls in the DOM.

Open multiple excel files each in their own instance or window - Windows 7


If you’re a user of Microsoft Excel you’ll no doubt be familiar with the irritation of opening multiple spread sheets as they will open in the one instance of Excel. Meaning you have to flick from spread sheet to spread sheet.

Running the little Microsoft fix found here will make a small change to the way Excel opens spread sheets and force a new instance of Excel on each file you open. Meaning you can use aero snap and have one on each monitor / half of the screen.

Friday, 17 August 2012

jQuery Validationengine arrows not showing (ASP.NET)

On a recent project the customer wanted to display custom error messages.  To achieve this I had to write my custom validation method as opposed to using the css="validate[required]".

This was fine until I hit the issue where by when calling the showPrompt method as follows:

if ($j("#lstCustomerType").val() == "") {
             jQuery('#lstCustomerType').validationEngine('showPrompt', 'Please select a customer type', 'error', true);
     }
The validation message was showing but was not including the nice little arrow despite me passing the variable of "true" for this optional parameter.

The solution for this was to pass all the optional variables through to the method as follows:

             jQuery('#lstCustomerType').validationEngine('showPrompt', 'Please select a customer type', 'error', 'topRight', true);

Tuesday, 29 May 2012

ASP.NET list of objects to excel

I needed a simple export to excel in .NET without using the Microsoft Interop DLL and found a neat way to export from a gridview from the code behind.

The code is simple and generates a gridview on the fly and outputs to the HttpResponse:


To call the method in my code behind .cs file all I need is:
Export(Response, fileName, List<T>);
For the code I was writing I had to implement a DTO as by default the list of objects are exported in the order of member variables within the class so needed reordering and excluding as the standard object included ids etc.

Tuesday, 15 May 2012

301 redirect TSOHost Apache

When creating a new website you often find that their established pages will be ranked in Google (or other search engines!) and already have Google Juice or equivalent.  You never want to lose this Juice or have your customers clicking a ranked link and being presented with a nice 404 error.  To get round this you have a couple of options but for my TSOhosts Umbraco setups I implement a 301 htaccess RewriteRule with mod_rewrite enabled.

Here is a simple example of an .htaccess file placed in my public_html (root) folder:
RewriteEngine On
RewriteRule ^contact\.htm /contact-us.aspx [R=301,L]
This is checking for any request that comes in for contact.htm (notice the backslash to escape the "." before htm) and redirects the user straight on to contact-us.aspx. 

The '^' is referred to as an anchor and when used means that the character(s) to the right of it must be the very first character(s) example:
^contact\.htm would match contact.htm but not nocontact.htm.  
You can also do the same with the last character(s) by inserting a '$'.

Don't forget that you have to have mod_rewrite enabled to make this work!


Wednesday, 9 May 2012

Umbraco 4 User Controls not rendering or firing

I recently needed to create a custom contact control for an Umbraco site that required jQuery validation and a number of triggers in the form that displayed different options for the user based on actions elsewhere in the form.

The only way to create this was to create a .NET User Control; there are two approaches that I am aware of for adding controls to your site which are:

  1. Open the site in Visual Studio using the File > Open Website option and point to the directory of your Umbraco site (remember there is no solution or project files).  From here you can add a new "Web User Control" and the dll and ascx file will be included in the website
  2. (My chosen route) Create a new ASP.NET Empty Web Application that can be your User Control library that can be used across multiple sites.  Then its just a case of copying any .ascx files in to the Umbraco usercontrols folder and the .dll file into the bin folder.
I created my contact form and tested without issue in the stand alone library project however when I came to add the custom control on to my Umbraco page wrapped as a macro no events fired.  After closer investigation I realised that my control was not wrapped by a <form> tag and as such was treated as standard HTML controls with not event firing.

The rule of thumb is that every ASP.NET User Control that has the runat="server" attribute must be placed inside a <form> tag that also contains a runat="server" attribute and a unique id.  You can either add the form tag within your User Control or on the page within the Umbraco Template.

Thursday, 26 April 2012

Getting Umbraco 4 working on TSOhost.co.uk

I have only been using Umbraco CMS for about 6 months now and despite the introduction of version 5 I am still yet to make the jump across due to time (the severe lack of it!)

If you are trying to set-up your Umbraco site with TSOhost then you may find you get errors when the site cannot read the umbraco config files.  Here are the steps to get your site working:
  1. Move your site to an isolated application pool via the TSO control panel.
  2. Set the permissions on the "public_html" folder to 777 ensuring that the changes are set to be recursive.  You can do this either via an FTP client or within the TSO file manager.
  3. Recycle the application pool
One thing I have noticed is that if your page template is modified via the Umbraco admin page the change may not appear straight.  The only way to get the changes to appear is to refresh the application pool.  These changes would come across eventually but only when the application pool recycles automatically after a period of inactivity on your site.

I host all my site with TSOhost who I can only recommend with the highest of praise.  I have had dealings with Rackspace when working with a FTSE100 and the support we got was as though we were a nobody.  Dedipower were better but still not the best with reaction to datacentre incidents.  TSOhosts are the perfect providers of ASP.NET SQL express hosting for SME's, Darren & Craig have been immense help.  Cheers guys!

Sunday, 22 April 2012

Configuring Google Apps MX records to work with 123-reg.co.uk domain name

I have to do this task many times but each time I need to setup the MX records for Google Apps Gmail for a domain registered with 123-reg.co.uk I always forget what I need to enter.  This may also help with other domain name providers when during the Google Apps setup process the only option you have is "other" as your domain name provider.

When selecting other Google presents you with a table similar to this (it may have changed since the time of writing but the principal will be the same, just make sure you use the values Google is providing with you now):
Priority Points to
1 ASPMX.L.GOOGLE.COM.
5 ALT1.ASPMX.L.GOOGLE.COM.
5 ALT2.ASPMX.L.GOOGLE.COM.
10 ASPMX2.GOOGLEMAIL.COM.
10 ASPMX3.GOOGLEMAIL.COM.
Looks confusing enough but all you have to do but follow these simple steps and you'll be up and running receiving emails in no time.

Go in to you 123-reg.co.uk control panel, select your required domain name and go in to the Manage Domain Name section.  From within here you will see the "Manage DNS" option under "Advanced domain settings":




Once in to this sectin select the "Advanced DNS" tab:





Delete any MX records that may already be present and then enter the new MX records as per the google setup website (example table above)


The trick that Google fail to tell you is that you need to enter an @ symbol as the "Hostname" on 123-reg.co.uk.  The other details you will see are as Google list them; another point that others fall short on is that the full stop after the Destination MX value is required!  Once that is setup you can start enjoying Google's great email services but routed through your own distinguishable domain!