views:

2121

answers:

8

Can someone explain when to use each of these? They almost seem interchangeable in many cases.

The Custom Control gets added to the toolbar while the User Control (ascx) can not. The Custom Control does not get rendered in the Designer while the User Control does. Beyond that, how do you choose which is the right one to use?

Also, I am looking for the best way to access the controls from JavaScript (GetElementById). So, a point in the right direction for adding client side support would be great.

+8  A: 

This is from Microsoft's site:

Web user controls

  • Easier to create
  • Limited support for consumers who use a visual design tool
  • A separate copy of the control is required in each application
  • Cannot be added to the Toolbox in Visual Studio
  • Good for static layout

Web custom controls

  • Harder to create
  • Full visual design tool support for consumers
  • Only a single copy of the control is required, in the global assembly cache
  • Can be added to the Toolbox in Visual Studio
  • Good for dynamic layout

http://msdn.microsoft.com/en-us/library/aa651710(VS.71).aspx

AaronS
A: 

For accessing them from JavaScript, you should use document.GetElementById('<%=TheControl.ClientID%>').
The difference between a web control and a user control is that a user control has the ascx file with the html definition while the web control does not; that is the cause for other differences. Also, for user controls you can't use new Control(), you need to use LoadControl instead because that loads the .ascx.
For simple controls that inherit from .Net controls, like a text box with validation or something like that, I tend to use web controls; for more complex controls with html and inner controls I tend to use user controls. But it's basically your personal preference.

configurator
This example does not always work for webcontrols. For example, suppose that your webcontrol produces two textareas, a label, and a button, all wrapped in a span tag?
Stephen Wrighton
This is the problem that I am having. How do I access my control when it contains other controls?
Mike
+1  A: 

I think what you are thinking of is a Custom Control versus a User Control, both of which are Web Controls. A usercontrol does not have designer UI, while a custom control can.

Typically we separate our UI into separate areas of functionality using UserControls. However if we create functionality that we want to use across multiple solutions we generally create them as Custom Controls.

Only custom controls can be added to the toolbox.

Here is an excerpt from Microsoft:

http://msdn.microsoft.com/en-us/library/aa651710(VS.71).aspx

Jason Stevenson
+2  A: 

A UserControl has to be hosted by a web site and is associated with an ASCX file using the codebehind model. Therefore, with a user control you can define the basic markup for the control in the ASCX file, and put all the code into the ASCX.CS file.

A WebControl is just a class, and doesn't let you define an associated ASCX file; you need to override the Render function to print out any markup the control is going to produce. However, because it doesn't depend on an ASCX it can be put into a shared library. (a DLL)

To respond to your question: both Web- and UserControls have the same benefit - they take some portion of a page and encapsulate it. I use UserControls when the code in question applies to only one of my sites; if I'm using similar code in multiple sites, then I'll convert the code to a WebControl and move it into a shared library. That way when I need to update it, I make changes in one place and not 3 or 4.

Tip: you can get around some of the trouble of defining your own WebControl by inheriting from one of the standard ASP WebControls. Many standard controls like Label or Image aren't sealed - you can inherit from them and override their methods to create your own specialized version of that control. This is much easier and less error-prone than extending WebControl directly.

The Digital Gabeg
A: 

Simple:

UserControl:

  • UserControl need *.ascx file to complete the instance initialization. Therefor you can not derive one UserControl from another.
  • UserControl has *.ascx file, so you can easy write a HTML. And ( the most advantage ) you can change content *.ascx of file, and change the look of control in run-time of web application.

WebControl:

  • WebControl is "only" class in assembly, so you can derive another control from them.
  • WebControl has no *.ascx ( or another ) file, co nobody can not change the look of this control ( unqualified web admins for example ).
TcKs
A: 

that's not quite true. A webcontrol is like a button, and you can build a designer for it so it does get rendered in the designer mode.

The main difference is that a webcontrol is an atomic unit. It's supposed to work just like all the other default server controls found in Visual Studio (including the designer mode). Additionally, it's built entirely in code, and stored in the DLL (i.e. there's no html side to it, and nothing's published to the website).

While a user control is a the .NET version of an ASP Include. There's a html snippet with it's corresponding code-behind page. There's an ASCX file that's pushed out to the website during publishing. An additional note, these are easier to develop than server controls.

Is one better than the other? That depends on what the goal is. But in general, if you're building something for other people/projects to utilize, go with a webcontrol. If you're building something for your own project's consumption then go with a user control.

Now, as far as JS is concerned, that's a harder thing to describe, and warrants a rather large discussion on its own. For server controls, you'll need to provide the hooks for the JS to get at the client ID for each of the internal controls. While a user control you can code the JS directly on the user control and access the controls the same way you would in an ASPX page.

Stephen Wrighton
Mike
create new readonly public properties that expose the ClientID of the internal controls.
Stephen Wrighton
+1  A: 

User controls are compiled with the project and it must be written in the same language as the project.

Custom controls can be dropped on the canvas and configured by setting properties without the programmer knowing all the internals (can be good or bad). Also since the custom control is precompiled in the dll, it does not need to be written in the same language as the project.

If attention is paid to detail, the Custom control can be written to display in the designer (although this may not be worth the trouble).

doug
A: 

User controls

  • Easy to implement because you can visually drag & drop other controls on the markup part.
  • Good designer support in Visual Studio
  • Can only be reused in the same project
  • You can also create templated user controls if you want (though not so commonly used)

Custom server controls

  • Harder to create but there's a whole variety of possible scenarios:
    • Inherit from existing controls like Label, Button, ...
    • Create a composite control
    • Make templated, + databound, controls
  • Great reusability in other projects
  • Ideal to make frameworks that can be used company wide
XIII