cardiffcoder
My favourite mistake, databinding too early.

If there is one mistake I make almost every single time when working with webforms it is this one, databinding controls during the OnLoad event. This is particularly perilous for DropDownLists and RadioButtonLists and I’ll tell you why …

OnLoad is fired very early on in the ASP.NET page lifecycle and what this does is wipe out any selections you made before postback because the control has been cleared and then re-populated, clearning SelectedValue, SelectedItem etc.

I’ve seen many different ways that people databind controls to overcome this, but the plain, simple and easiest way is to just databind later, after you have dealt with the SelectedValue or SelectedItem properties. Personally, I databind during PreRender.

  1. cardiffcoder posted this