Archive for the 'code' Category

Writing Better Code and Formatting Tip

Tuesday, May 6th, 2008

Sometimes when I work with other developers I inevitably find that all of the code looks different. Some developers like to write really compact code, as if we were still working on cartridges when disk space for the source code really mattered. Some developers also firmly believe that fewer lines of code in C# means it will somehow run faster because doing 5 things on a single unreadable line of code will somehow run in a single instruction in compiled code versus carefully expanded and readable code that performs the same 5 actions. I am not a fan of compact code. I prefer readable and verbose code with the occasional comment to help explain a bit of code that implements some strange business logic that was defined in the specifications but is not obvious from looking at the code.

(more...)

Shared Content Across Sites with ASP.NET User Controls and JavaScript

Saturday, March 15th, 2008

Rendering a User Control in ASP.NET using a web service or a generic handler is easy. Taking the string that is generated with a User Control and then serializing it to be embedded in JavaScript is also easy. Both combine to what you will need to create custom ads that are exactly like how Google Ads are served. This technique can also be used to share a common piece of markup across many web sites. As the central web site is updated so are all sites that reference it.

The first method below will render a User Control to a string. The second method will serialize that string for use with JavaScript as a JSON value. The value can then be included in a dynamically generated script for use from remote sites. I had some early troubles with the DataContractJsonSerializer which replaces the deprecated JavaScriptSerializer but eventually I got the following code to work reliably. Previously I created something similar but without User Controls a while back for the local .NET User Group to show banners for upcoming meetings.

Fixing the Jumping AJAX Rating Control

Friday, February 22nd, 2008

If you are using the Rating Control for the ASP.NET AJAX Control Toolkit on a long page you will notice that after selecting your rating the page jumps to the top of the screen. On a short page you will not notice anything but the hash sign appearing at the end of the URL. On a long page the jump is a big problem. It is due to the href attribute on the anchor tags being set to a hash instead of something like javascript:void(0); which would prevent the jumping. I fixed this with one line of JavaScript using jQuery to set the link value to ensure clicking does not have the unwanted side-effect.

In the below sample I have the Rating Control wrapped in a div tag with Rating set as the CSS class. This way I can have many ratings on one page and having this one line fix all of them.

Intellisense for jQuery in Visual Studio 2008

Friday, February 1st, 2008

These days I write more JavaScript than C#, or at least it seems like it sometime. To make my work a little easier I have started using the JavaScript support in Visual Studio 2008 that provides Intellisense support if you put Xml Doc into your JavaScript. The problem with working with jQuery in Visual Studio 2008 is that it fails due to how the jQuery script is written. For reasons why Intellisense fails, you can read more from Jeff King. Adjusting jQuery to make Visual Studio happy is not really a priority and not even necessary. I can simply reference the real jQuery script from the web pages and within my scripts I can reference a stub for jQuery that is simply a shell with Xml Doc.

[ Update: Hotfix for Visual Studio 2008 to fix jQuery Intellisense ]

(more...)

Web Deployment Projects in Visual Studio 2008

Friday, November 30th, 2007

The new release for Visual Studio has been out for MSDN subscribers for a couple of weeks now and if you have tried it out you may have discovered that the Web Deployment Projects are only registered to work with Visual Studio 2005 despite interest in having this capability added directly to the IDE. Instead the add-in will be updated and released for Visual Studio 2008. Today as I upgraded a set or projects from .NET 2.0 to .NET 3.5 I found that the Web Deployment project that I have set up for the website was not loaded, however, I am still able to make use of the project even if Visual Studio does not know what to do with it.

I am a big fan of automation and what MSBuild can do for you. There are bits and pieces of MSBuild sprinkled all over .NET projects. Most project files are actually MSBuild scripts that you can use like any other MSBuild script. What I like to do is place an MSBuild script called build.proj in the root folder of a Solution that I use to carry out common tasks such as building and packaging the Solution. One of these tasks has been to run the Build target in the Web Deployment project for a website and then package the contents of the output folder in a zip file. I prefer doing deployments this way instead of using the publishing feature in Visual Studio which seems to take longer most of the time. Using the MSBuild script also allows me to run the build without using Visual Studio, such as when the code is on a server and not on a development machine. Once the zip file is created I can simply upload it to the server, unzip it and place the files in the directory where IIS is configured to serve up the website. For important websites I make sure to keep an older zip file around in case I need to roll back the changes.

(more...)

Using Overflow for Inline Frames

Saturday, July 14th, 2007

I have always wanted to know how some of the fancy layouts that Google uses actually work. The mail and reader applications both have sections within the page which I always assumed where just inline frames with the iframe tag which I personally avoid. But I recently discovered that you can achieve the same affect using div tags with the CSS value for overflow set to auto or scroll while the height is also set.

(more...)

A Bug with Reflection and Inheritance

Wednesday, June 27th, 2007

Last week a bug in the String object was found. It was apparently due to a compiler optimization which will not be fixed for a while. But we have come this far without discovering it, so we can wait a little longer.

:)

The last few days I have been trying to fix a bug that was making absolutely no sense to me. It turns out my problem is another known bug in the .NET framework. I am using reflection to automatically bind data values to objects which inherit from a base class. The base class actually does all of the work while the inheriting classes define properties which line up with the data values from the database. I confirm that the name and type of the property is the same as the data value but when the SetValue method on the PropertyInfo object was called it threw a TargetException.

TargetException: Object does not match target type.

(more...)

HTTP Compression with AJAX

Wednesday, June 20th, 2007

I read HTTP compression in ASP.NET 2.0 by Mads Kristensen the other day and tried it out on a website that is using ASP.NET AJAX. I found that while the development web server run by Visual Studio handles it all just fine, IIS 6 on the server does not. Somehow the process is corrupting the page state. So while I want to use the compression to get what I can for bandwidth savings I had to work around this problem.

(more...)

LinkMindr.com Updated with More ASP.NET AJAX

Tuesday, May 1st, 2007

I have updated LinkMindr.com with a few improvements. The big change is limiting the tag cloud to just 35 of the most recently modified tags. At the bottom I have a drop down list for all of the tags so users can still access all of their tagged items. This conserves real estate on the page and makes the most used tags more accessible.

As a part of this change I also converted the page to use PostBack events as triggers for the UpdatePanel which is a features of the new ASP.NET AJAX control suite. Previously I had a clever jQuery call handle all of my AJAX functionality but with the latest changes it became easier to handle this functionality with event triggering.

(more…)

Post Build Deployments with MSBuild

Thursday, April 26th, 2007
7 Steps to MSBuild

MSBuild is a powerful tool for automation but as the name implies, it is a tool primarily for compiling code. Beyond the initial build MSBuild can also be used in preparation for deployments. To assist with the deployment of websites Microsoft produced Web Deployment Projects which carry out a series of tasks to build a website with the option to replace configuration sections which is a very useful feature. Once I have compiled a project I have multiple deployment targets so I wanted to just reconfigure a project in preparation for a deployment and I found a good way get this done outside of a web deployment project.

Replacing configuration sections is handled by the ReplaceConfigSections task which can be accessed in the Microsoft.WebDeployment.Tasks.dll assembly. You just have to pull it into your own script with the UsingTask directive which is shown in the example script.

(more...)