简体   繁体   English

DropDownList:DataTextField,DataValueField ... DataEnabledField?

[英]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). 我目前正在使用从DropDownList继承的控件(它具有一些与该问题并不真正相关的额外功能,但它为我提供了实施我要讨论的额外内容的好地方)。

We're using data binding, with the DataTextField and DataValueField working absolutely fine. 我们正在使用数据绑定,其中DataTextField和DataValueField可以正常工作。 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.) (我已经重写了控件的RenderContents方法,因此它可以正确地呈现未启用的项目,因为默认情况下DropDownList不会执行此操作。)

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. 因此,我想知道如何重写DropDownList的数据绑定,以使其完全正常工作,只是将每个DataSource项目的另一个字段应用于每个ListItem的Enabled属性。

(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 . DropDownList控件的绑定逻辑实际上是在其基类ListControl中实现的 The method you need to override is PerformDataBinding(IEnumerable) 您需要重写的方法是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. 您可以查看Reflector中的默认实现,以确保您不会错过任何内容,但是它是受保护的虚拟内容,因此您应该没有任何问题。 Just call base and then add the logic you need. 只需调用base,然后添加所需的逻辑即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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