Stupid, Stupid, Stupid!!

For the past two days I have been banging my head against the wall with an ASP.Net problem.

I created a custom WebControl composite class, meaning that it contains other WebControls.

I overrode its render method and used the provided HtmlTextWriter to spit out the code specific to my WebControl and the HTML code of the child WebControls which, by the way, were dynamically created in my WebControl’s Init event handler.

Anyway, what counfounded the heck out of me is that if I would push a button on the ASPX page, thereby triggering a PostBack event, the ASPX page forgot all of its dynamically created controls. In other words, a richly populated page became a blank page just by me hitting the submit button!

After a lot of research on this problem, I stumbled across an article at http://scottonwriting.net/sowblog/posts/2129.aspx that didn’t directly provide the answer but proverbially slapped me across the forehead and jumbled the facts into place. I needed to add these child controls to my parent control’s Controls collection! Stupid, stupid, stupid!

I removed my custom render method and added the controls to the Controls collection, and it works like a charm. View state is preserved!

Leave a Reply

Your email address will not be published. Required fields are marked *