Friday, October 31, 2008

Sharing User Controls between Web Projects


How many times have you had rearrange your solution or copy code around in order to share user controls between projects? This is one place where Asp.Net is seriously lacking. A user control is supposed to be a piece of functional content that is broken into a separate control for maintainability and code reuse. Why then can I only reuse them within one project?

Well I guess there is one method for doing this by using virtual directories in IIS. So the better question would be why I cannot reuse them easily across multiple projects.

There are some additional steps to make it much easier to actually use these controls as well as access there public properties and methods. Here is the whole process:

1) Create a new web project for your user controls.

2) Create a folder in this project where you will put these controls. (This is not necessary since you can reference the folder of the project but I think that having the user controls folder named the same across projects can make things a little clearer.

3) In a separate web project from where you want to use your shared user controls, create a directory with the same name as the directory from the previous step. (In my example I call this “UserControls”.

4) In the Solution Explorer, right click on this directory and select Add->Existing Item…



5) Browse to the folder in your shared user controls project. Select all the controls along with their code-behind files. But instead of clicking the add button, click the down arrow on the right side of the button. Then select Add As Link.



6) You can now repeat this for any other web projects in your solution. When you’re finished, your solution will look something like this.



7) You can now repeat this for any other web projects in your solution. When you’re finished, your solution will look something like this.

8) Remember to make sure that IIS is aware of your “UserControls” directories as virtual directories so that the asp.net worker process compiles your pages properly.

Enjoy!

Thursday, October 9, 2008

Ctrl+F is changing the way we read

I don’t know about you but I love [ctrl+f] a.k.a the Find command.  Just as copy/paste changed the way that we write and edit text, [ctrl+f] is changing the way that we read.

When I’m browsing a web site in search of some data, I find [ctrl+f] to be the easiest way of scanning the page to see if it might contain the nugget of knowledge I’m looking for.  Especially today when most pages are full of images, ads, and other noise, [ctrl+k] will take you right to the text your looking for.  And you instantly know, regardless of what the Google search brought up, if the page actually contains what you’re looking for.

Visual Studio also has [ctrl+shift+k] which is the short-cut for “Find in Files”.  This allows me to search the entire codebase for any string (or regex) that I’m looking for.  This is great when I need to maintain (read fix a bug) in a part of a system that I’m unfamiliar with.

For example, let’s say that some value isn’t displaying properly on a web page.  Using Firebug I can usually find the control’s ClientID.  This value usually contains part of the control’s ID.  [Ctrl+shift+k] and I can find all the files (pages and user controls) that use this ID.  Then you can find the type, class declaration… and finally fix the bug.


Imagine having this kind tool when reading a reference book.  Sure the table of contents and index are useful.  But just think of how quickly you could find information if you could quickly search the whole book for a word like ListView.




As more and more text based content is available online and we do more and more of our reading online, it will become imperative for any good UI to allow for instant text searching.  That this isn’t already a standard feature in Windows causes a lot of grief.  Just as all text boxes allow cut/copy/paste, they should also allow “find”.  

Tuesday, October 7, 2008

SEO friendly forms in ASP.NET

If you ever need to create a page in ASP.NET that uses some kind of Url mapper, you may run into some problems with posting back. If you look closer, you’ll find that the Url of the request is no longer the SEO friendly one but the actual one (possibly including some querystring parameters if they’re part of the mapping.) And .NET does not make it easy to override the address in the “action” attribute of your form.

The best way I’ve found to fix this problem is to create a Server control that inherits from the standard HtmlForm and overrides the RenderBeginTag() method.



You can now register and plug this new form into any page. You can then set the attribute AllowPostBack to false if there are no postbacks on your page. Or you can override PostBackUrl to set the “action” attribute on the form.