asp.net viewstate and seo

asp.net viewstate is a wonderful invention, but can bloat your html.  For most users this is probably going to have minimal impact on the page load time.  But for search engines spiders you really want to minimise needless information, especially near the top of the page.

It’s best practice to turn off viewstate for the whole page or for individual usercontrols if they don’t require viewstate.  The deciding factor here is: does the page or usercontrol perform postbacks.

Even with careful pruning of viewstate you’ll probably find your viewstate is still large…

What you can do

Now there are a couple of strategies I’ve seen for working round this, both involve overriding the page

1 is to intercept the page rendering and move the viewstate hidden variable to the bottom of the page.

Here’s an example http://www.dotnetdiary.com/labels/Moving%20ViewState%20Field.html

2 is a very clever solution that keeps the viewstate information on the server and justs passes a key back and forth to the client.

I read about this approach here http://www.eggheadcafe.com/articles/20040613.asp

Here’s my approach

Now since I don’t think that viewstate bloat has too much of an impact on page performance for real users I’m only going to worry about search engine spiders.

asp.net has a way of detecting spiders – now I’m not saying it’s going to be 100% accurate but in my tests it’s been accurate enough for this technique.

In the page load of your page (or master page)

If Request.Browser.Crawler Then Page.EnableViewState = False

er that’s it, pretty simply eh? :)

You can verify if this is working by viewing the source of the cached version of the page that google holds.

Hope that helps!

ASP.NET Event Validation and “Invalid Callback Or Postback Argument”

I was getting this and pulling my hair out. My button (server-side) was pumping out a bit of javascript  And this kept causing the error. I tried using the prescribed fix: ClientScript.RegisterForEventValidation(mycontrol.uniqueid) in the prerender.  Still got the error. Here’s how I fixed it.  RegisterStartupScript does the job of running at startup by emiting the js right [...]

Use jQuery to replace <body onunload="GUnload()">

When using Google maps it’s important to use GUnload on the page’s unload event. Fine when you can access the body tag directly…. But if you have the Google map functionality wrapped inside an ascx you need a different approach. After a big of digging I found how to use jQuery to “catch” the unload [...]

Need to access session variables in an ashx?

Just a quick note if you need to access session variables from an ashx, you need to implement SessionState.IRequiresSessionState or IReadOnlySessionState if you only need to read the vars In your ashx change to

My first iPhone app

Despite not owning an iPhone (I want one!), I’ve built my first iPhone application. It’s a web app for playing Suduko.  It generates a new grid every day.  Utilises jQuery of course. Find it here http://sudoku.nerdboy.co.uk/ I used http://www.testiphone.com/ to see that it worked and fitted the screen. If anyone reading this actually has an iPhone and the [...]

Firebug for Google Chrome Campaign

I’ve been a Firefox fan for ages and when I discovered Firebug my life became a whole lot easier.  So much so, that I’d hate to be without it. But… I’m loving Google Chrome for it’s sheer speed.  I never realised Firefox was slow until I tried Chrome – maybe it’s all the extensions I [...]

Required field validator for an fckeditor

Unfortunately a requiredfieldvalidator doesn’t work with the fckeditor. The way to do this is to use a custom validator. Important note: you need to set validateemptytext=”true” otherwise it doesn’t fire Then add the following javascript (note that I’m using jQuery here to reference the validator control) You might also want to handle the server-side validation [...]

Find a control within a area

If you are using master pages, you may expect that you can find a control within an tag with the following me.findcontrol(“fckEditor”) But unfortunately that doesn’t work. You need to make a reference to the place holder the control is in, then find the control within the place holder. Another gotcha here is that you [...]

Lo-Fi unzipping functionality for .net

I’ve looked for unzipping functionality for .net a couple of times over the years and never found a totally satifactory solution. In a recent project I had to grab an attachment from a POP box and then unzip the attachment. I found an unzipping product called 7zip, it’s open source and there’s a command line [...]

asp.net outputcache based on url

The outputcache page directive is very handy – and I tend to use this where appropriate on my usercontrols (.ascx) You can optionally vary by querystring parameters. Something that’s missing from the default options is to vary based on the actual url of the page.  I was building a site where a random testimonial was [...]