Tuesday 26 February 2008

What I aim to talk about at BarCamp Manchester UK

So on Saturday it's the first BarCamp in Manchester, UK. I have to say I'm quite excited about attending and from the beggining have been pondering on what to talk about. Within the last week I have formalised this and got the majority of my two code demo's done and started the slides tonight. I'm aiming, if chosen, to talk about the process and stages I went through to make a simple ASP.Net application "web 2.0", in other words swishy and more "AJAX". I will highlight two methods, of endless methods I may add, the pros and con's of both, the decisions I made and why. I'm hoping this will help people who are trying to enhance existing code, be it personal or at a business level, to see what methods are available and at least somethings to consider before making a decision. Regardless of whether I do the talk at BarCamp I will aim to post slides and my examples on here by the end of the weekend.

Tuesday 19 February 2008

So What is it Code Behind or Code File??

Recently I have been working on a web project in VS2005. All has been going well, several class files, several pages, and a few web controls all make for a project of fun.


I came back to work on the project, and I happily added a few more web controls, compiled the site etc and got some work done.


Now here's where things began to go wrong, after some lunch I reopened my project, but this time I opened the projects solution properly. Did some work, then went to build the solution and..... It broke. :( Now a project not compiling occurs frequently, ie rubbish code, but this time it started winging on how all my controls in the custom web controls that I built before lunch were now out of scope.


Frustrating. I started poking around and tried to find the issue, and I couldn't find anything. It was at this point that I looked at my .ascx file and not the .ascx.cs


With ASP.Net you can have code seperation. Content in one file, .ascx if a control and logic in another.ascx.cs. This is called code behind, and in the content .ascx file you normally have a reference that links the logic file, .ascx.cs to the content control.


This is where my issue was. ASP.Net 1.1 used the syntax CodeBehind, and the code in the file was classed as a seperate class. .Net 2.0 however introduced partial classes and as a result Microsoft added the CodeFile syntax. This syntax CodeFile doesn't include the full code-behind class declaration, instead this is generated on page compilation. The page compilation step automatically generates members for each control and these are then fused with the CodeFile's partial class.


My issue was that my file was using the CodeBehind syntax. Although this is still supported in .Net 2.0, during the page compilation step of the build the generation of the members partial class is missed out. .Net 2 assumes that the codebehind file is a full class, not a partial class.


Now this is ok, as long as your code behind file contains a full class and not a partial class .Net 2 will compile the code using the old method. But somehow, and I'm still not sure how my Content file, had changed to using CodeBehind instead of CodeFile. Initially I missed this as CodeBehind isn't necessarily wrong, in the correct context.


Changing codebehind back to codefile corrected the issue and the partial classes were generated and fused properly.


After searching the net I also found that this can occur after upgrading a vs 2003 .net 1.1 project to vs 2005 .net 2.0 Web Application project and seems to cause a few people headaches.


So there you have it, if you find your code won't compile suddenly or after converting a 1.1 project to a 2.0 Web Application project, with your controls being out of scope, check whether you're using codebehind with partial classes and if so change to CodeFile.

Monday 4 February 2008

More Web Controls - jQuery

Ok so I have an obsession with web controls and since my last one, Yahoo! Media Player, I have been toying with the idea of doing one for John Resig's jQuery. Tonight I got around to doing this and am now releasing version 1 of it. I made the web control as often when I write Web User Controls that contain / use jQuery to enhance the controls I'm not sure if jQuery exists in my web.master or web page itself. This often leads to me having to go back and change pages or include it in the web control only to find one page already uses it. And yes I'm rubbish at remember what JS is used where :S. So this web control not only checks to see if the jQuery control has been rendered before, if not render it but also allows you to simply specify the jQuery version, path to the file and which type to use, normal, minified or packed. Download jQueryWebControl.zip

How To Use It

So add a register assembly into the top of your aspx file
<%@ Register Assembly="jQueryWebControl" Namespace="jQueryWebControl" TagPrefix="jQuery" %>
Then which ever page, master page or even web control will need jQuery add the jQuery Web Control.
<jQuery:jQuery runat="server" ID="jQuery"/>
As the control checks to see if a jQuery control has rendered before you don't have to worry about only including the control once. This should ensure you don't have to waste time checking but also means your controls can be used independantly in different sites.

Using the three optional parameters, Version, Type and Path.

Version - this is just a string parameter that allows you to specify the version of jQuery to use. E.G "1.2.2" - if this is left out then the control defaults to 1.2.0 Type - again a string parameter, this time with three options, empty string "" = default / development version, "min" = minified version, "pack" - the packed version for people who cant serve up gzipped files. - if left the control defaults to the normal development version Path - a string parameter indicating the path to the javascript files. E.G "/javascript/jquery". - if left the control defaults to the /javascript/ It is worth noting that the control expects you to host the jQuery files, so in order to render the script tag for version 1.2.2 pack'd jQuery file you will need jquery.1.2.2.pack.js in the location you specify with the path parameter.

Example of use using parameters

<jQuery:jQuery runat="server" ID="jQuery" Version="1.2.2" Type="min" Path="/javascript/jQuery/"/>
I personally have made use of the version parameter the most as it has means different sites that have been developed against different jQuery releases can be updated / forced to use one version of the library. It also works best if all your jQuery files are in one centralised location, using a virtual directory or a shared location. So hope you like the control, let me know any thoughts, comments or improvements and enjoy. Download jQueryWebControl.zip

An Update to the YMP Web Control

Tonight I was going over my YMP Web Control that I released in December. However I noticed that I could end up having multiple controls render onto a page. This could happen by having a control in my master page and then including it in a web user control, or countless other situations. This then results in your page not validating and the JavaScript going astray. So tonight I altered the code, it now checks to see a YMP control has already rendered and if so it doesn't render. This version should replace the previous version and works in exactly the same way. Download the new version.