Showing posts with label User Controls. Show all posts
Showing posts with label User Controls. Show all posts

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.