简体   繁体   中英

DropDownList: DataTextField, DataValueField… DataEnabledField?

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.)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM