继承 Control 与 Web Control
我读过这篇文章,其中解释了在创建自定义控件时何时选择 Web 控件与控件,但这还不够。 我见过自定义控件继承自两者,当它们都向 UI 提供内容时。
http://msdn.microsoft.com/en-us/library/yhzc935f。 aspx
“如果您的控件在客户端上呈现用户界面 (UI) 元素或任何其他可见元素,则应从 System.Web.UI.WebControls..::.WebControl (或派生类)。如果您的控件呈现在浏览器中不可见的元素(例如隐藏元素或元元素),请从 System.Web.UI..::.Control 派生您的控件。WebControl 类派生自 Control 并添加。与样式相关的属性,例如 Font、ForeColor 和 BackColor,此外,从 WebControl 派生的控件参与 ASP.NET 的主题功能,而无需您进行任何额外的工作”,
因此使用 WebControl 的唯一原因是您。想使用他们的造型功能吗? 我只是最终用字符串生成器输出字符串,所以我不关心这些东西。 我更喜欢使用直接的无表设计和字符串来形成我的控件无论如何都会呈现的 HTML。
I have read over this which sort of gives an explanation of when you'd choose Web Control vs. Control when creating custom controls but it's not quite enough. I've seen custom controls inherit from either when they're both rending out stuff to the UI.
http://msdn.microsoft.com/en-us/library/yhzc935f.aspx
"If your control renders a user interface (UI) element or any other visible element on the client, you should derive your control from System.Web.UI.WebControls..::.WebControl (or a derived class). If your control renders an element that is not visible in the browser, such as a hidden element or a meta element, derive your control from System.Web.UI..::.Control. The WebControl class derives from Control and adds style-related properties such as Font, ForeColor, and BackColor. In addition, a control that derives from WebControl participates in the themes features of ASP.NET without any extra work on your part. "
so the only reason to use WebControl is if you want to use their styling features? I'm just going to output strings with a stringbuilder utlimately so I don't care about that stuff. I'd prefer to use straight up tableless design and strings to form my HTML that my control renders anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Control
派生自 Control 类允许我们的控件利用 Control 类提供的渲染方法以及 ViewState 的使用。
WebControl
WebControl 类派生自 Control 类。 但是,如果我们从 WebControl 派生自定义控件,我们将获得对控件的许多视觉方面的免费支持,例如字体大小、CSS 类、背景颜色等。
我应该从哪个类派生?
当您创建需要很少或不需要 UI 的自定义控件时,您应该从 Control 类派生。 如果您的控件确实需要广泛的 UI 支持,那么您应该从 WebControl 派生。
来自:http://dotnetslackers.com/articles/aspnet/ASPNETCustomControlsPart1.aspx
Control
Deriving from the Control class allows our control to take advantage of the rendering methods provided by the Control class, as well as the use of ViewState.
WebControl
The WebControl class derives from the Control class. However, if we derive our custom control from WebControl, we get free support for many visual aspects of our control, like font size, CSS classes, background colour and so on.
Which class should I derive from?
When you are creating a custom control that requires little or no UI, then you should derive from the Control class. If your control does require the need for extensive UI support, then you should derive from WebControl.
From: http://dotnetslackers.com/articles/aspnet/ASPNETCustomControlsPart1.aspx
除非您指定,否则 WebControl 不会呈现表格或类似内容。 它所具有的是大多数用户期望从使用 UI 呈现的控件中获得的样式功能。
使用它并不需要花费太多费用。 尝试一下,看看它是否会给您带来任何问题。
WebControl doesn't render tables or anything like that unless you tell it to. What it does have is the styling features that most users will expect from a control that renders with a UI.
It doesn't cost you much to use it. Give it a try and see if it causes you any problems.
那么,您有问题要问吗? 我认为 MSDN 文章中很好地阐述了两者之间的差异。
So, do you have a question to ask? I think the differences between the two were well addressed in the MSDN article.