I’m sitting in the Asp.Net tips and tricks sessions given by Scott Guthrie at the CodeMash conference. Below are some of the items Scott covered.
Visual Studio Tips and Tricks
First up is the web site project model and the web application project model. Essentially there are two types of web solutions. One is a web site project which is the default in Visual Studio 2005. It essentially allows you to store your web site in a folder0. For most content driven sites this is fine but this isn’t what we were use to in VS2003. The other solution type is a download that was provided after VS2005 was launched. Those of us that were using VS2003 didn’t like the new way with the App_Code folder and so on. The web application project download provides developers the ability to get back to the way we were used to in VS2003. Typically enterprise customers building large scale applications will utilize the web application project model and content sites use the built-in web site project. There are also tools to migrate between the two projects if you decide to change your solution later on. Here are the main things the web application project provides:
- All files contained within the project are defined within a project file (as well as the assembly references and other project meta-data settings). Files under the web’s file-system root that are not defined in the project file are not considered part of the web project.
- All code files within the project are compiled into a single assembly that is built and persisted in the \bin directory on each compile.
- The compilation system uses a standard MSBuild based compilation process. This can be extended and customized using standard MSBuild extensibility rules. You can control the build through the property pages, for example, name the output assembly or add pre- and post-build actions.
Optimizing VS 2005 Web Site Build Performance
If you’ve had build performance issues with Asp.Net there are some tricks and reasons why sometimes when you make a small change the entire solution rebuilds. Instead of me getting into this to deep, Scott already has a lengthy blog article with screen shots that outlines what is happening under-the-hood and how to speed up build times.
<form defaultbutton=”button1″ runat=”server>
<asp:button id=”button1″ runat=”server” />
<asp:panel defaultbutton=”button2″ runat=”server>
<asp:button id=”button2″ runat=”server”/>
How to shift focus
You can write java script to shift focus to a certain input control but in .Net 2.0 you don’t have to. In .Net 2.0 there is an attribute called defaultfocus which you can set on your form control to automatically shift the focus to the input control. To take this a step further you can also do this in code: Page.SetFocus(control).
Another way shift focus comes into play is on validation errors. When you are using the validation controls set the property SetFocusOnError=”true”. By default it is false for backwards compatibility so it is something you have to set. Setting it to true it will auto shift to the control throwing the error validation. Very nice!
Using CSS Adapters
In Asp.net 2.0 the Control Adapter API was introduced which provides alternative rendering of controls. What this gives you is a hook to modify or override a control’s rendering output. All events and other things stay the same, but the output of HTML can be altered. The CSS Adapter Toolkit, which can be downloaded from http://www.asp.net, makes this possible. For example instead of the menu control outputting tables, it can output a div tag with ul and li tags. This of course allows a designer to then style the menu as they would want.
I asked the question if this is going to be baked into the next release of Visual Studio and Scott said it would be, but to what extent he wasn’t sure. It is an interesting problem to discuss because you have an interesting design experience to overcome in Visual Studio. While they could change the output of the menu control to display div tags by default it wouldn’t render properly without a style sheet. While it is a little more work, I think the solution would be for the Asp.Net team to provide a configuration wizard to configure the control, or maybe extend the smart tag. I would like to see the default option that is rendered from the controls to be all compliant HTML and then just simply switch a property to change the style to A, B, C, D, or Custom. This is just an initial quick thought but hopefully you see where I am going.
Register User Controls in web.config
If you use user controls in your pages you typically have to add the user controls in the top of each page. If you add this following code below into your web.config file, these controls will be automatically loaded for you and save you this step. And for the record there is no performance penalty in taking this approach.
<add tagPrefix=“theelder“ src=“~/controls/header.ascx“ tagName=“tag“ />
Server Side Comments
If you have ever had a time when you wanted to not render a control in a page you may have just done this:
<!– <asp:Button runat=”server” id=”button1″ /> –>
Taking this approach is still going to render the button and fire events it is just going to be hidden in the browser. However, if you really want to remove the control from the page see the sample below. Doing it this way will just skip this button all together.
<form id=”form1″ runat=”server”>
<%–<asp:Button runat=”server” ID=”button1″ />–%>
Technorati tags: CodeMash