为什么不是所有具有 Text 属性的 System.Web.UI.WebControl 类都实现 ITextControl?
我很好奇为什么只有一些 System.Web.UI.WebControl
控件在具有相同的接口属性时实现某些接口。
例如,有很多控件具有 Text 属性,但只有以下实现 ITextControl
:
- Label
- Literal
- DataBoundLiteral
- TextBox
- ListControl
(TextBox 和 ListControl 实际上实现 IEditableTextControl,而 IEditableTextControl 实现 ITextControl)
TableCell、Button、HyperLink 等不是吗,所以我必须编写这样的代码,
ITextControl textControl = control as ITextControl;
TableCell tableCell = control as TableCell;
if (textControl != null)
{
textControl.Text = value;
}
else if (tableCell != null)
{
tableCell.Text = value;
}
而不是这样
control.Text = value;
这是一个设计决定还是一个疏忽?
I'm curious why only some System.Web.UI.WebControl
controls implement certain interfaces when they have the same properties of an interface.
For instance, there are plenty of controls that have a Text property but only the following implement ITextControl
:
- Label
- Literal
- DataBoundLiteral
- TextBox
- ListControl
(TextBox and ListControl actually implement IEditableTextControl which implements ITextControl)
TableCell, Button, HyperLink and others don't so I have to write code like this
ITextControl textControl = control as ITextControl;
TableCell tableCell = control as TableCell;
if (textControl != null)
{
textControl.Text = value;
}
else if (tableCell != null)
{
tableCell.Text = value;
}
instead of this
control.Text = value;
Was this a design decision or an oversight?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为它的设计是好的,我不认为这是一个疏忽;在这些控件中,文本是控件用途的主要焦点。我确实明白你的观点,因为让控件利用更多此类接口会非常方便。
I think it was designed ok, I don't think it was an oversight; those are the controls where text is the primary focus of the purpose of the control. I do see your point because that would be very convenient to have controls utilize more of these types of interfaces.