views:

31

answers:

1

I'm currently working with a control that inherits from DropDownList (it has some extra functionality that isn't really relevant to this question, but it gives me a nice place to implement the extra stuff I'm gonig to discuss).

We're using data binding, with the DataTextField and DataValueField working absolutely fine. However, I find myself in need of a similar field that will control whether or not the item is enabled. (I've already overridden the RenderContents method of the control so it properly renders non-Enabled items, since DropDownList doesn't do this by default.)

Thus I'd like to know how to override the DropDownList's data binding so it acts exactly as normal, except a further field from each DataSource item is applied to each ListItem's Enabled property.

(NB: the field I'll be binding to isn't a straight boolean, and doesn't have an explicit/implicit cast to boolean, so I'll probably be "passing" it as a string.)

A: 

The binding logic for the DropDownList control is actually implemented in its base class ListControl. The method you need to override is PerformDataBinding(IEnumerable)

You can look at the default implementation in the Reflector to make sure you don't miss something, but it's protected virtual, so you should not have any problems. Just call base and then add the logic you need.

Slavo
Thanks for pointing me in the right direction; I've now got this doing what I want :) After calling base I'm assuming the list's Items are in the same order as the dataSource IEnumerable, but this seems to work.
Rawling
Heh, and after checking Reflector I've seen that I don't even need to override RenderContents as it does actually pay attention to the Enabled field - I must've messed up when I was testing before.
Rawling
I'm glad you made it work :)
Slavo