1

There are a lot of ways to build custom controls for Asp.Net. Some people use the System.Web.UI.WebControls.WebControl as a base class and other people use the System.Web.UI.Control as a base class for their new controls.

What are the reasons to choose the one or the other as a base?

Jacob
  • 77,566
  • 24
  • 149
  • 228
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
  • 2
    possible duplicate of [\[ASP.NET\] What are the differences between User Controls, Server Controls & Custom Controls?](http://stackoverflow.com/questions/994009/asp-net-what-are-the-differences-between-user-controls-server-controls-custo) – Jacob Apr 06 '11 at 18:25
  • 1
    @Jacob this question isn't a duplicate because it is talking about base classes. Not about control types (ascx vs. c# controls). – Kees C. Bakker Apr 06 '11 at 18:28
  • 1
    Also, both of these would fall under the category of a Custom Control, with .Controls being for things w/o a UI and .WebControls being for controls with a UI. – ben f. Apr 06 '11 at 18:29

3 Answers3

1

Control does not have a user interface where as WebControl renders to the response object.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • User interface can be rendered on a Control by overriding the Render method. – Kees C. Bakker Apr 06 '11 at 18:23
  • 1
    Yes, but at an astronomical amount of extra unneeded development effort on your part. Plus, the WebControls class brings in all those popular little features that we all expect from our visible ASP.NET controls (like the Visible property). – ben f. Apr 06 '11 at 18:27
  • @Ben f. check: http://msdn.microsoft.com/en-us/library/system.web.ui.control.visible.aspx Control has a Visible as well. – Kees C. Bakker Apr 06 '11 at 18:51
1

System.Web.UI.WebControls.Webcontrol derives from the System.Web.UI.Control and adds support for styling (BackgroundColor, Style, etc.).

Control doesn’t have that support by default, you’ll have to implement it yourself by creating properties and handle those on the Render() method. WebControls also come with theming and toolbox support for certain common properties.

Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
Erik Dekker
  • 2,395
  • 5
  • 33
  • 55
0

You should use System.Web.UI.WebControls.WebControl when your control is going to have a UI component visible on the rendered page, as it contains the various plumbing code generate a UI (though still requiring considerable work from you to create that UI).

System.Web.UI.Controls are for when your control will not have a UI on the rendered page (think some of the ASP.NET DataSource controls that reside in the toolbox, can be dragged onto a page, don't render anything, and act as a conduit for getting data to a GridView control that does have a UI).

See this MSDN article.

ben f.
  • 750
  • 6
  • 14
  • The only difference is UI? Like BackgroundColor and stuff like that? – Kees C. Bakker Apr 06 '11 at 18:31
  • The difference is that WebControls have a client-side, rendered in the browser after the .aspx page is interpreted, UI, while Controls do not have any descernible UI to the enduser (they are strictly behind the scenes plumbing code) – ben f. Apr 06 '11 at 18:35