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.