Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider
New Things in .NET 3.5 SP1 – Moving Hidden Fields to the Bottom of the Page

If you have not downloaded SP1 for Visual Studio 2008 and .NET 3.5, please go ahead and get to it. If you installed the Beta, like me and many other folks you will want to run the prep tool first. I am not going to do one of those here is a list of new features in SP1, there are plenty of those all over the web by now. I mean SP1 has been out all week long. Instead I want to create some post showing some of the important features in more detail.

Over the years I have worked on moving ViewState and other Hidden Input Fields to the bottom of the page. This is important for several reasons, but can also have impact on some content, like the DevExpress HTML Editor. However, moving non rendered content helps the page render faster and also helps place important content higher in the page, thus allowing the search engines know the content is more important. So often it is wise to move Hidden Fields to the bottom of the page.

ASP.NET SP 1 has added a new property to the Page configuration section, RenderAllHiddenFieldsAtTopOfForm. This is a great new property that gives you control over where the system generated hidden fields are rendered by the framework or page handler creates. That is important to note, because the property will not affect any hidden fields you add to the markup, those will still need to be handled by you. Personally I do not think I ever use hidden fields, so it should not be an issue for me and most likely not you. While this will not move the ViewState hidden field it will render other generated hidden fields at the bottom.

<pages theme="DarkBeer" masterPageFile="~/TBHMain.master" 
    maintainScrollPositionOnPostBack="true" 
    renderAllHiddenFieldsAtTopOfForm="false">

When this is an issue is when a a postback is performed before the page is completely rendered. This typically happens when an AJAX postback is performed as the page is being rendered. From my experience you have to test each of your pages to learn which ones will be affected. You will receive an “Invalid Postback” or “callback argument” error. Other things I have found is controls will not render correctly in some cases. I have found most of my problems are with third party controls, in particular WYSIWYG editors like FreeTextBox and the DevExpress HTML Editor.

<div>
    <input type="hidden" name="__SCROLLPOSITIONX" id="__SCROLLPOSITIONX" value="0" />
    <input type="hidden" name="__SCROLLPOSITIONY" id="__SCROLLPOSITIONY" value="0" />
    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" 
        value="/wEWBwLjspyADQKP0anjBQK41pvTDwK0yq71CQLBo8n8...PKC" />
</div>

One thing to note is since this is done in the web.config Page element, this feature is applied to all pages and cannot be controlled at the page level. So if you have any issues with this and have to turn it off, beware this will affect all the pages in the web site.

Two Non-Programming Technical Books We Should All Read

The year was 1993, I was attending Providence Baptist Church off Glennwood and DurRaleigh Rd. Little did I know that less than a mile away the FBI was conducting a huge raid and arrest of one of the world’s top hackers, Kevin Mitnick.

Kevin Mitnick was being arrested for being one of the most prolific hackers on the net. At the time the Internet and the World Wide Web were not part of the common vernacular, but I was there and this was really big to me. In the years since Kevin has served his time and now runs a security firm and has written a couple of fascinating books on hacking, but more importantly how easy it is to hack without even being very technical.

The Art of Deception: Controlling the Human Element of Security
by Kevin D. Mitnick, William L. Simon

Read more about this book...
The Art of Intrusion: The Real Stories Behind the Exploits of Hackers, Intruders & Deceivers
by Kevin D. Mitnick, William L. Simon

Read more about this book...

I know we have all watched movies where the main characters are able to trick naive people into helping them along with the covert tasks. Catch Me if you can and the Ocean’s 11,12 & 13 come to mind. This is not limited to just a Hollywood script, this stuff goes on every day, even in today’s world. How many times have you held the door for a stranger to enter your building without a valid ID? Do you wear your corporate ID facing out? These are common ways hackers find their way into the systems they want to compromise. Using these techniques and more are all covered in a series of amazing hacking stories, compiled by Kevin Mitnik and William L. Simon.

httpHandlers and httpModules at Pee Dee User Group

PDANUGI will be giving my httpHandlers and httpModules presentation next Tuesday night at the Pee Dee area (Florence, SC) .NET user group next week. Every time I give this presentation it is different than any other time because I am always improving my library of tools and application of handlers and modules. This will be no different.

I have a few more modules and handlers that I have built and a few that I have found that I want to demonstrate. In particular I will demonstrate how to stream a SiteMap.org site map and ELMAH error logging infrastructure.

You can find directions on the PDnug site, which is just east of I-95 and I-20. I look forward to seeing you there!

Professional Software Testing with Visual Studio 2005 Team System

Ok, so I know Visual Studio 2008 is the current version we are all using, right? But I have been a little behind on some of my books and well to be honest really using the cool features of Team System. So I got this book almost a year ago and finally got around to reading it earlier this week. I wanted to read it to get a better understanding of Team System and I am glad I did.

Professional Software Testing with Visual Studio 2005 Team System: Tools for Software Developers and Test Engineers
by Tom Arnold, Dominic Hopton, Andy Leonard, Mike Frost

Read more about this book...

I will be the first to admit my version of unit testing has been me writing my own testing frameworks or harnesses as I have always called them. I have also always lost when it has come to fighting source control systems, VSS, Vault you name it I have blown it up.

This book goes over how to use VSTS as a testing tool and covers all aspects of the testing features, including database unit testing features (Thanks Andy Leonard!). I particularly like the fact the authors took the time to explain what the different types of test were and what function they served. I had honestly wondered why there was a manual test and now I really know, duh.

The book covers introductions to VSTest, VSTESD and Unit Testing with both. Like I said it also goes through testing the database in Chapter 4. Then Web Testing (near to my heart), manual, ordered and generic testing. Then it reviews Load testing and and finishes up reviewing code and dynamic analysis. The book closes out with a 6 part appendix walking through various testing scenarios and installing VSTS.

Richmond Code Camp, Entity Framework and the Beer House

I will be presenting an introduction to the Entity Framework and the Beer House this Saturday at the Richmond Code Camp. I have been very busy with the Beer House application, Entity Framework, ASP.NET AJAX, CSS Layouts and much more the past 2 months and it will be wonderful to get out a little and share what I have learned.

This presentation will have maybe 4 slides in a slide deck and will be pretty much nothing but rolling up my sleeves and showing how to build an n-Tiered app using the Entity Framework as the Data Access Layer and the Repository Pattern. Be forewarned there will be no EntityDataSource in this presentation!

My goals are to show you how to get up and running using the Entity Framework, a good n-Tiered architecture and a web site. This will just touch the tip of the ice berg and I hope it wets your appetite to look more into the use of the Entity Framework as a data access option.

Kevin Hazzard has posted a list of all the Richmond Code Camp speakers and kudos to him for organizing the speakers! If you are in the area of Richmond I hope to see you there this Saturday.

Making Velocity Start When You Reboot

Microsoft announced a new distributed caching application, code name ‘Velocity’, at TechEd. Since then I have been sort of toying with it and I have my issues and dealing with the learning curve. One thing I kept noticing and had a hard time finding was a way to make Velocity start when I rebooted the computer.

Turns out Velocity runs as a Windows service, and thus you need to set the start options. First find it the list of the services, it is called “Microsoft project code named ‘Velocity’. Then double-click this item in the list and the properties dialog will be displayed.

I chose to set the Startup type to Automatic (Delayed Start). I think this means it will start when I reboot, but essentially be prioritized after getting back into my Vista machine (perceived user performance). Next if you are are not running, you can click the ‘Start’ button to start Velocity. The other alternative is to bring up the command line administration tool.

Share this post :
Using a Singleton In an ASP.NET Application

Recently I was asked how to use a Singleton in an ASP.NET application. To be more specific I was asked how would I use a singleton and make sure it was applied at the application level. If you have no idea what a Singleton is I will give you my simple definition. It is a pattern used to ensure that an object is only instantiated once. The classic example I offer is ensuring you only have one instance of a desktop application running.

I honestly was not sure about what I would do at the time, I felt I knew, but I was not sure about my thoughts. I was correct in what I was thinking at the time, but it was one of those moments where I just could not think of object names, etc. So it bothered me and as soon as I could get back to my laptop I did a quick and dirty example of what I was thinking.

Now I am not going to get into the details of how to implement a class as a singleton, there are lots of examples all over the web. What want to do is show how you might apply the principle at the application level of an ASP.NET application. In this example I will just store a the current time when an application in first kicked off. I will use a quasi formal singleton, meaning it is a property pattern I commonly use to create one instance of a value.

What I decided to do was modify the ASPPipeline HttpModule from my eBook. In ASP.NET there is an Application property on the current HttpContext. The Application property refers to the HttpApplication object for the web application. It defines many members at the application level, it is also allows you to add objects the same way you can to Session or ViewState. So what we are going to do here is just that, store a time value in the application object. Each time a page is requested my quasi singleton pattern will check to see if we have stored that value (object) and if not then store it.

Public ReadOnly Property StartTime() As String
    Get
        If IsNothing(HttpContext.Current.Application("StartTime")) Then
            HttpContext.Current.Application("StartTime") = Now.ToString
        End If
        Return HttpContext.Current.Application("StartTime").ToString
    End Get
End Property

This is a simple ReadOnly property called StartTime that returns a String. The first thing I do is to check and see if there is an object named “StartTime” established by using the IsNothing method. In C# the if statement would have used the != null check.

If the value has not been set then I set the value to the current time using the Now() method; HttpContext.Current.Application("StartTime") = Now.ToString.

Private Sub Application_BeginRequest(ByVal source As Object, _
        ByVal e As EventArgs)
    ' Create HttpApplication and HttpContext objects to access
    ' request and response properties.
    Dim application As HttpApplication = CType(source,  HttpApplication)
    sb.AppendFormat("BeginRequest - {0}{1}", Now.ToLongTimeString, vbCrLf)
 
    sb.AppendFormat("Start Time - {0}{1}", StartTime, vbCrLf)
 
End Sub

I will not go into the details of the HttpModule, there are other posts about custom HttpModules and my eBook, where I go into details about this module. But basically in the BeginRequest event handler I am going to set the value in the application object by calling the StartTime property. Now each time a request is made to an ASP.NET application the initial value will be logged to the log file, while the other times for each event in the ASP.NET life cycle will get stored at the time they are called.

Share this post :
Using the title Attribute to Enhance Your Pages

The other day I made a post about some search engine optimization techniques for ASP.NET developers and making compliant HTML. Scott Hanselman also recently published an episode of Hanselminutes on making accessible web pages. The reason I want to point to both those resources is to bring attention to why you would want to use the title attribute in your HTML markup.

<p title="Add the title to add value to your pages.">

<a href="foobar.aspx" title="The title attribute adds extra value to links.">foobar</a>

This attribute can be added to elements, such as an anchor tag, to add description to the element. From a search engine point of view it adds value to your page and the links that help it to identify not only your page, but the destination of the link. For example if you have a link coming to your site, having a title attribute associated with that link helps identify a potential search phrase.

Inserting a title attribute in Windows Live Writer

Browsers will typically display the title value in a tool tip as the user rolls over the link with a mouse. Screen readers will use the title to add description to the link as they read it to the user.

So as we continue the discussion about being compliant with HTML markup, consider adding the use of the title attribute to make your page more descriptive.

Share this post :
Publishing Compliant HTML and Web Markup

Yesterday my friend Keith Elder posted a poignant editorial Blog about ASP.NET web sites that were not compliant with all browsers. Keith is very justified in his argument and points us to some examples. The reality is that most web developers do not know what standards compliance is, or how to determine what is and is not compliant. Too often a business makes a conscience decision to not support any browser but Internet Explorer, which is a decision that is bad on many levels. It is one thing to dictate to employees what browser they use behind your firewall, but not on any public facing property.

This week Internet Explorer 8 release Beta 2, which folks means the ultimate release of Internet Explorer 8 is just on the horizon. The reason why this will be a huge release is that IE will finally be ‘standards compliant’ itself. Which for many naive site owners means their site will look like crap and be completely unusable. I am sure they will blame Microsoft for breaking their site, but really the issue lies in the developers and designers they hired to produce their site. This includes the neighbor kid or their youngest niece who was looking for some experience that did not really know what they were doing. But even worse it includes professionals who either do not know what they are doing or are just plain lazy about producing good markup.

The Canadian Developer Connection posted a Blog post early this morning about producing compliant sites for IE 8 and have a video interview with Pete LePage about how IE 8 is important to web developers. They also have links to some good resources to use as well.

Before I go any further, I am guilty. Yes, I have produced code that does not work in every browser. I know I still do, but I know I try not to. I try to make sure each page, form and image looks and performs according to plan in IE, FireFox, Opera (if you have not tried this browser I recommend it) and Safari. These are the four I focus on because they all have a windows version and will include almost a perfect 100% of web users. But sometimes I miss something. Just last night I noticed a drop shadow effect was not rendering correctly on a site in FireFox, and you can bet I will work on that today.

Producing standards compliant code is like any other exercise, once you get used to using it, integrate the better code into your code generation templates and normal routines you will not think about it much. It could be a simple and including a closing ‘/’ in your <img> elements. I bet you will actually learn things that you can do with elements you were not aware of as well. Ultimately you will not only make content that works across all browsers, but you will find yourself making better sites too.

A good free resource to use is the W3C Markup Validation Service. You can go to this page, enter a URL to your site and it will tell you what does not comply on that page. I tried a URL of mine this morning and came up with 250 issues. Most were benign (many of my images did not have the closing ‘/’ for example, but I will also address them today as well. You can also upload a file or paste in a snippet of code for evaluation.

There is also a CSS, RSS, Mobile, and broken link validator available on the site. They also have a page of tools that can also be used for specification validation.

So as you go forward with your web development, please be conscience of HTML and CSS compliance. Your image is riding on it.

Share this post :
Visual Studio Next Wish List

As a .NET developer I live in Visual Studio as my primary tool. I think it is a fantastic environment, but with anything you live with there are many things I want to see in the next version. So I thought I would start logging them by category.

Visual Studio

Add Filtering to Member Intellisense - I really like the Intellisense window that pops up with all the members of a class when you are working in code. What I do not like is that there are not tabs or some other mechanism to let you filter by member type. There is a Common and All tabs, but what I would really like to see are three more tabs, Methods, Properties and Events.

I really noticed this when I was learning event declarations in C# because I found myself not necessarily remembering the event name I wanted to hook into. I was forced to scan up and down the entire list of members and mentally filter out everything without a lightening bolt to the left to determine what the best event to hook into would be.

Add Intellisense to Generic Handlers – As you can guess I really like to use custom httpHandlers. Writing code for them is fairly straight forward, but I like to write custom classes and use the Generic Handler depending on the situation. I have found you do not get any Intelisense love when working in a Generic Handler. This would be a fantastic feature.

I am horrible with name, whether it be people or classes and their members. I only have so much mental capacity and I do live on Visual Studio helping me not have to reference documentation all the time, especially when I am just starting to use a class for the first time in months. More Intelisense is all I ask.

Expandable Add References Dialog – There is so much to be said about this dialog, but I need to be able to expand the tiny window that list the assemblies. Many of their names are long and there could be a long list of similar names.

Better Experience with the Add References Dialog – This dialog often takes forever to build a list of components. When it comes to the the COM tab, just do it on the way home and hope it is done when you get in tomorrow morning. Better yet, Friday afternoon to Monday. It is a list of items that should be ready, please fix this one!

More Control Over New Item Dialog – How confusing is this dialog when you have any third party components or SDKs installed? Please give us the ability to better organize this experience.

Refactoring of Inline CSS – I think it would be nice to be able to say right-click a style of a control or element and move it to a stylesheet for the Web Site. A simple automation of a common task. 

Visual Studio Theming – I know I can import and export settings, I am working in a dark theme right now for my day to day development. Which is what inspired this request. First I noticed the battery life of my laptop seemed to increase once I started using this theme. I think because the monitor uses much less energy to light up pixels.

But something I recently noticed on a cross country flight was how the bright frame of Visual Studio really stood out to me on that dark red eye flight. It was an annoying glare to me. It seems that with the theming built into WPF, this should be a great new feature that we can have in the next version of Visual Studio.

Visual Basic

OK, I am biased here. The VB experience in Visual Studio has always seemed to be a few steps ahead of C# because of market driven demands. It seems that many of my C# friends say they do not want the productivity enhancements of VB in Visual Studio, then go buy Resharper and CodeRush to get those enhancements, you do the logic.

C#

Make Event Handler Creation Easy: OK, so this does have a snippet to help, but it is not intuitive, especially compared to the way it can be done in VB.NET. Type this (or a control’s name) . then sorting through a long list of members trying to find the right event, then += TAB TAB is just not intuitive. Please add the 4 mouse click method from VB.NET to C#.

True Background Compiling: I think this was somewhat added in SP1 this summer, but after spending some time in C# this summer I find myself constantly recompiling my code to make syntactical errors go away. I realize that many C family die-hards think this is a horrible feature, but let them turn it off if they want.

XML Literal – I realize this is not so much of a Visual Studio thing as a language specification, but seriously this needs to be part of the C# experience.

More Snippets – I think VB.NET comes with like 400+ snippets. C# is around 40, maybe. Again make writing the language not get in the way of building solutions to customer problems.

Error Management

Allow Filtering of Errors: Often I will encounter a situation where my page markup has gone awry and I want to just go to the error. But the markup has made control declarations to not be valid, which leads to a long list of errors in the code behind. Scrolling through the long list of errors in the code-behind only makes the process harder. If I am in the markup, let me filter just for that page. The same can be said for a code file as well.

Easier Implementation of Interface Members -  This is a feature I love while developing VB.NET. Once you declare the class implements an interface, just hit RETURN after the Interface(s) name and BAM!! All the signature methods are created for you. I realize you can right-click and select the XXXX context menu selection to get the members implements, but I have to tell you it is so much easier in VB.NET.

WPF

Better WPF Design Experience – I have to wonder why we actually need Blend for Designing WPF layouts. Why can’t we have a richer designing experience in the development environment?

JavaScript

Better Intelisense – Well I think this is self explanatory, but not an easy feat.

Keep Improving the Config File Intellisense – I know they have done a good job with this, but there still seems to be some fringe cases where things fall apart.

Summary

Ultimately parity will be the ultimate goal with the new release of Visual Studio, which means the differences between the two core languages will be even less important going forward. I know with the complete rewrite that many nagging issues will ultimately be handled and we will no doubt have a whole new list to complain about. That is the life cycle of any application. Please feel free to add your suggestions or extend the thoughts I banged out.

Search Engine Optimization for ASP.NET Web Sites

Most of the last two years of my career I have been developing applications behind the firewall and let’s face it most ASP.NET applications sit behind the firewall. This is unfortunate because ASP.NET offers the most scalable and performant (yes that is a word in our development world) platform for web sites.

I feel comfortable saying that because some of the few sites that are public, like MySpace, Plenty of Fish and Microsoft are all running on much fewer servers than they non ASP.NET/IIS counterparts. And to that point, according to what I learned at MIX IIS 7 is the best platform for PHP because it allows PHP to be multithreaded, but that is another story.

My friend Dana Coffey posted a series of really awesome articles on Search Engine Optimization for your Blog this morning; Part 1 Blog Setup, Part 2 Blog Posts, Part 3 Tips for ASP.NET Developers. There is a lot of great content in these post for more than just how to optimize your Blog for Search Engines. I was inspired and thought I would share some resources to help you optimize your site for Search Engine visibility.

Search Engine Perspective and Tools

I wanted to share some resources to help you make your web site more search engine friendly. This first is how to know what the search engines know about your site. Each of the big three search engines have some sort of web master tools that allow you to garner information about how they see your site.

Live Search Web Master Central

Google Web Master Tools

Yahoo Site Explorer

Books

There are a lot of Search Engine Optimization books on the shelves. Many are outdated. So check the copyright date and make sure it was written since 2005. This is important as the concepts of search engine optimization have evolved over the past few years and the search engines often change criteria each day to a certain degree. For example 10 years ago Meta tags for Description and Keywords were helpful in distinguishing your site from the competition. Today this has almost no bearing on search engine placement.

These are a couple of current books that I have reviewed and find their content to be helpful for most web masters to get their site more visibility. One thing to keep in mind is making your site optimized for search engines should also help your site retain users and make their experience better.

Professional Search Engine Optimization with ASP.NET: A Developer's Guide to SEO (Wrox Professional Guides)
by Cristian Darie, Jaimie Sirovich

Read more about this book...
Search Engine Optimization: Your visual blueprint for effective Internet marketing (Visual Blueprint)
by Kristopher B. Jones

Read more about this book...

Tools

Having tools on your desktop is the ultimate weapon in getting better search engine visibility. Having tools on your desktop is like having a control center at your fingertips. I have two tools that I use all the time, Keyword Elite and SEO Elite.

Keyword Elite is one of the most indispensable tools I have purchased because it will examine what keyword phrases people are using to find content through the search engines. This is important because as a business owner, which lets face it owning a web site is owning a business, you tend to think of just the small sub-set of phrases used to find information related to a business or subject matter. My experience has shown me that the phrases I use to find information are completely different than the audience. I have seen this same restrictive tactic practiced by my clients over the years as well.

Keyword Elite gives me insight into what is actually being used to find information on the web, which leads me to create targeted long tail content for these phrases. An example is from time to time I will use results to write blog articles.

SEO Elite is a tool that extends the information available from the search engine web master tool sites while at the same time collecting much of the information into a single application. One of the main features I like about it is its ability to monitor my site’s ranking across search engines for specific keywords. This helps me keep track of how visible I am for specific phrases. What it is not great for is knowing what you rank for that you are not monitoring. And outside of analyzing your site logs for phrases you are likely not to really know what phrases you actually rank.

What is really great about each of these tools are detailed video tutorials on using them and frequent, FREE updates.

IIS Log Parser

Using the segway from analyzing logs for keywords, IIS Log parser can be a great friend in knowing how your site is being used by visitors. I am not going to get into the details of using this tool, but I will recommend a book on using the Log Parser.

Microsoft Log Parser Toolkit
by Gabriele Giuseppini, Mark Burnett, Jeremy Faircloth, Dave Kleiman

Read more about this book...

There is an art and a science to making optimized content for the web. Following solid guidelines and creating good content is the art part. But analyzing numbers is the science part and being on top of the numbers game is very key in knowing how to apply your energy to the content creation part of an optimized web site.

If You Have Not Upgraded From .NET 1.1, You Need To NOW!

Mainstream support for .NET 1.1 is retiring on October 14, 2008. What this means is if you have not installed at least Visual Studio 2005 and .NET 2.0 by now, you really need to. Yes you can buy an extended support contract for mega bucks, but seriously how long are you going to stake your business on an aging platform like 1.1. The current version of .NET is now 3.5 SP1. That means there have been a ton of updates, upgrades and bug fixes since 2002. You are also missing out on some great advances in technology in the past 6 years. You will also find it harder and harder to find programmers to maintain your application because any programmer worth their salt will not want to work with an old platform because they will be risking their personal future.

So please head my advice and upgrade finally .NET 2.0/3.0/3.5 and 3.5 SP1 is free and it works great. It won’t bite you, I promise.

CodeStock Rocked!

This weekend was a great weekend for .NET developers in the Smoky Mountains as CodeStock rocked the mountains. There were several hundred passionate geeks that came out to Pellissippi State to learn, socialize and have an all around good times, and I guess a few wanted the Master Chief helmet too!

I really want to give a special thanks for Michael Neal, Alan Stevens and Wally McCluer for organizing the event for everyone. They did a fantastic job. The Knoxville community really needs to let those guys know you appreciate their work.

I did a presentation on custom Modules and Handlers. I wanted to post the slide deck and source projects for everyone. It was a great group to share the topic. I like giving this talk because it provides a good introduction to a topic every ASP.NET developer should get into.

Code Stock Content

Creating a Background Thread to Log IP Information

Trolling the ASP.NET forums this afternoon I came across this thread about creating a custom httpHandler to log IP information to a database. What the requester was trying to do was call a remote site’s (www.ShowMyIP.com) REST API to get information about the visitor’s IP address, such as location and store it in the database. U