如何限制自定义数据绑定控件接受我的自定义集合作为数据源?
我已经根据我的要求创建了一个自定义数据绑定控件,现在我想限制此控件应该将自定义集合作为数据源,而不是每个实现 IEnuberable 接口的集合。
可以限制吗?如果是,如何限制?
提前致谢..
I have created a custom databound control as per my requirements and now I want to restrict this control should take custom collection as datasource instead of every collection which is impelmented IEnuberable interface.
Is it possible to restrict? If yes, how to restrict this?
Thanks in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你能在 DataSource 的 setter 中设置一个守卫吗?
Can you put a guard in the setter of DataSource ?
最简单的方法之一是将类型检查放在数据源相关属性中。例如,@Preet 已经指出了重写
DataSource
setter。该方法的唯一问题是,您还需要处理使用 DataSourceID 绑定控件的情况。我宁愿建议在 中进行类型检查DataBoundControl.PerformDataBinding 方法 - 这当然假设您是从
DataBoundControl
类继承的。调用此方法以便派生类可以实际绑定数据 - 因此它是一个很好的候选方法。另一个等效方法是在OnDataBind
中进行检查。另一种方法是将特定集合公开为接受数据的属性。在这种情况下,您应该避免公开与 DataSource 相关的属性。
One of the simplest way is to put the type check in Data Source related properties. For example, @Preet has already pointed out overriding
DataSource
setter. Only issue with the approach, that you need to also take care of a situation where the control is bound usingDataSourceID
.I will rather recommend putting a type check in DataBoundControl.PerformDataBinding method - this is of course assuming that you have inherited from
DataBoundControl
class. This method gets called so that derived class can actually bound the data - so it stands as a good candidate. Another equivalent is to put check inOnDataBind
.Yet another way is to expose specific collection as a property to accept the data. In such case, you should avoid exposing
DataSource
related properties.