I’ve gotten a lot done this past week building a web site. From the master page, to the web forms, to user controls, I’ve been working hard to create a layout that facilitates changes and is easy to reuse. The scripts are saved in separate JavaScript (ECMAScript) files, CSS is pulled out in most places rather than in-line, and I’m using jQuery to wire up behaviors to elements.
Some important tips and tricks:
- Declare a ScriptManager in the MasterPage. Only use ScriptManagerProxy objects in pages and controls. This ensures that you will be able to attach scripts at any level without that warning about having more than one ScriptManager object.
- Create separate JavaScript files for each page and control, then reference them using the ScriptManagerProxy on the object. Name the script files based on the object name for convienience.
- If you are using jQuery, make sure that you download the Visual Studio documentation file. This vsdoc/vsdoc2 file must be place at the same location as the main library file. Do not reference the vsdoc file from anywhere. Just make sure that it has the same file name as the main file with the “–vsdoc” added (including version number or anything else).
- In external script files, Visual Studio has no context for other script files that are loaded in the object. To get around this, reference the file like this:
/// <reference path=”jquery-1.3.2.js”/>
This does nothing at runtime, but gives you that wonderful Intellisense at code-time. - Use multiple style names on elements. Unlike C#, Java, and other object-oriented languages, you can have multiple inheritance in CSS. Some can affect layout, some behavior, and some will just end up being decorators to help you with jQuery selectors.
- Databind wherever possible. If there’s one thing that I’ve learned from WPF and Silverlight, working with ControlName.Text or ControlName.Value is no fun! Learn how to do binding to simplify your life.
- If possible, leave the design work to someone else! Focus on div elements anywhere that you can. Drop in a CSS sheet from someone else and markup the div’s with class names and let that be taken care of. It may not work out that cleanly, but ideally, do as little layout and design as you can and get the functional bits working. Keep design out of it as long as possible.
