5
votes

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.

3

3 Answers

10
votes

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

2
votes

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.

0
votes

So, do you have a question to ask? I think the differences between the two were well addressed in the MSDN article.