ASP.NET why doesn’t error?

Ok it is a strange title but…

One of my the most problematic areas of ASP.NET is the combination of Viewstate and the Page Life-Cycle. If you attempt to access the Viewstate before it’s ready then not only do you get incorrect results for that call but you corrupt the state for subsequent calls too. So given that ASP knows where it is in the life cycle and that you’re trying to access the state, why doesn’t simply raise an exception? If nothing else it would save about 100Gb worth of forum posts!

In a similar vein I was working on a HtmlInputText control the other day and fell for another issue. As a good ASP developer I knew that the ID I’ve given the control will be changed to some odd unique name with lots of "ctl_" text stuck in-front of it. (If I say I want to uniquely name it and take that responsibility then it should only use my ID, grrrr). So I when I wanted to generate some Javascript to access the control I passed in myControl.ClientID. However, my Javascript failed saying the object was null. Odd. A quick review of the code I noticed that I was accessing the ClientID before I’d added the control to the page. Ok, that makes sense, but again why is the ClientID populated with the name of the ID only before it’s been added to the page? Surely if you access the ClientID before the *page* has correctly populated it, then it should error? Oh well, just yet another pitfall to side-step.

Advertisements
This entry was posted in cSharp. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s