Friday, July 31, 2009

Getting rid of the Formview templates

Having a readonly-mode and an edit-mode in a webform to manipulate data is a pretty common situation.

Microsoft promotes using the FormView control. This might be a good idea when there's not a lot of data to edit, and when the readonly-mode is very different from the edit-mode. But what I mostly see is that the readonly-mode is a copy of the edit-mode with some controls disabled, which is a pain in the ass to maintain.

That's why I got rid of the FormView!

I made a method which can be called from any page/usercontrol and which recusively searches the ControlCollection and sets the Enable-property for some controls.

Take a look at this codesnippet.



This method iterates through the ControlCollection and checks the type of every control. When this control is a TextBox, DropDownList, Button or an ImageButton the control gets casted to a WebControl and the Enabled property gets set.

1 comment:

  1. Neat, and now on to creating those controls dynamically

    ReplyDelete