Control State (finally loaded)

Published 12 July 04 08:04 PM | despos

You see that summer time is here not from the outdoor temperature (pleasantly fresh these days here), but from the silly mistakes one (me, actually) makes while programming... Just the mistakes that another one (my wife, actually) would say due to the (too) hot weather.

What did I do?

I was trying to implement control state for ASP.NET 1.x controls. Fritz Onion blogs about control state here and he does it better than I did myself here. I wrote a new TextBox control to see if there was a way to persist properties like ReadOnly and BackColor even when the viewstate for the page (or the TextBox itself) is disabled.

  • I created a couple of protected methods and named them LoadMyControlState and SaveMyControlState. (Similar methods exist in 2.0.)
  • The former retrieves the contents of a custom hidden field named CONTROLSTATE, parses it and maps values to the aforementioned properties
  • The latter compacts the properties into an array of objects and serializes that to a string. The string is then stored in the custom CONTROLSTATE hidden field.

The infrastructure is ready but I need a couple of entry-points to make sure that the methods above are called. Here's when the temperature gets important... I thought that there was no better place to call methods than overridden versions of LoadViewState and SaveViewState. So I did and start testing the sample app.  

No way my control state handlers were called. Why?

If the viewstate is disabled (obvious test scenario), how can LoadViewState and SaveViewState be actually invoked? They are invoked anyway in ASP.NET 2.0, but because the ASP.NET 2.0 infrastructure is modified in light of control state support.

So I modified the TextBox so that LoadMyControlState is called from within OnLoad and SaveMyControlState care of OnPreRender.

It works like a champ. Now.

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)