Not too long ago I was working with a company whose specialty was auto dealer inventory management systems and providing solutions for getting their listings placed in auto search sites. While there, we were working on an application that would get auto listings from dealers and upload them into a central database that would feed multiple dealership website search results. The application was fairly large and there was a lot of overhead where the data was concerned. As the app grew, so did the amount of time it took to load pages when users – testers – performed searches. After wringing their hands and banging their heads, one of the developers asked me to look at the code and see if I could speed up the data calls.
As I reviewed the app and made a few changes to speed up the data, I was quite intrigued to find that, even with fast data calls, the pages were taking minutes to load. Having the data mindset, since that was what I was working on at the moment, I wasted about an hour trying to determine why the data was taking so long to get back to the site. Once I did a view source on the page, I wised up. The view state for the page was over 2MB. This got me curious and I started looking at other sites that were a bit slow, not just within that company, but others I’d been associated with or expected to be associated with in the future. It was pretty surprising how many of these sites were using view states that didn’t need to.
So to help others who could benefit from it, here are a few tips on optimizing the view state in .Net.
Don’t use the view state if you don’t need to. The view state is on by default. You can kill it using <@% EnabledViewState = false %>.
For pages you do need the view state, disable it for the controls you don’t need in the view state. You can do this by setting the EnableViewState property to false. This property is only available in server side controls (runat=server), so not all controls will have this property. This is how we resolved the issue in the app to get auto listings.
For large datagrids that are read only, it may be better to disable the view state and rebind the datagrid each time the page loads. If you do disable the view state, though, be sure you’re binding the datagrid outside of the IsPostBack check so the data binds each time.
As a side note, website owners, with whom you have no association, do not like to receive unsolicited advice about their sites, in this case the view state. Though sometime, along with a snotty response, they do make the suggested change.
Tim Hall is a Software Engineer, Graphic Designer, IT Manager, and SEM Consultant. He builds your websites for you through Alien Head Software Web Development. He runs this blog to help others be inspired by those who have inspired him.
Friday, September 18, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment