06 August 2016

user control vs custom control in c#


























































Custom ControlUser Control
A loosely coupled control w.r.t code and UIA tightly coupled control w.r.t code and UI
Derives from ControlDerives from UserControl
Defines UI in a ResourceDictionaryDefines UI as normal XAML
UI is skinableChild controls are skinable
Has dynamic layoutHas static layout
UI can be changed in different projectsUI is fixed and can't have different looks in different project
Has full toolbox support, Jst drag n drop from toolboxCan't be added to the toolbox, Jst drag n drop from sol’n explorer to page(aspx)
Defines a single controlDefines a set of controls
More flexibleNot very flexible like a Custom Control
Reusability of control (extend functionality of existing control), Designed so that it can be used by more than one applicationReusability web page, Designed for single-application scenarios
Compiled into dllNot compiled into dll
Creation is similar to the way Web Forms pages are created; well-suited for rapid application development (RAD)Writing involves lots of code because there is no designer support

Button, CheckBox, TextBox etc., even a UserControl is nothing but a Custom Control. You can easily load them inside a XAML page.

Custom Controls are compiled into a DLL assembly and can be reused in multiple places very easily. You have total control over the code, thus gives you more flexibility to extend the behaviour. Once you build and add a reference of the custom control in your project, you can find it in the toolbox. Thus, you will be able to drag and drop the control in your Design view and start working with it very easily.

  • When you have a rapid and fixed content in your UI, use UserControl.

  • When you want to separate some basic functionality of your main view to some smaller pieces with reusability, use UserControl.

  • When you want to use your control in different projects and each project may want to change the look, use CustomControl.

  • When you want to implement some additional functionality for a control, create a CustomControl derived from the base control.

  • When you want to apply themes to your controls, use CustomControl.

  • When you want to add toolbox support for your control, so that your user will be able to do drag and drop to the designer, use CustomControl.


For user control:
<%@ Register TagPrefix="UC" TagName="TestControl" Src="test.ascx" %>

For custom control:

<%@ Register TagPrefix="CC " Namespace=" CustomServerControlsLib " Assembly="CustomServerControlsLib " %>

 

No comments: