basic differences between User Controls and Custom Controls


Partly a reminder for myself, but hopefully useful for others…


User control

Custom control

  • Designed for single-application scenarios
  • Deployed in the source form (.ascx) along with the source code of the application
  • If the same control needs to be used in more than one application, it introduces redundancy and maintenance problems
  • Designed so that it can be used by more than one application
  • Deployed either in the application’s Bin directory or in the global assembly cache
  • Distributed easily and without problems associated with redundancy and maintenance
Creation 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
Content A much better choice when you need static content within a fixed layout, for example, when you make headers and footers More suited for when an application requires dynamic content to be displayed; can be reused across an application, for example, for a data bound table control with dynamic rows
Design Writing doesn’t require much application designing because they are authored at design time and mostly contain static data Writing from scratch requires a good understanding of the control’s life cycle and the order in which events execute, which is normally taken care of in user controls

How to create a user control

The syntax you use to create a user control is similar to the syntax you use to create a Web Forms page (.aspx). The only difference is that a user control does not include the <html>, <body>, and <form> elements since a Web Forms page hosts the user control. To create a user control, follow these steps:

  1. Open a text or HTML editor, and create a server-side code block exposing all the properties, methods, and events.

    <script language="C#" runat="server">
       public void button1_Click(object sender, EventArgs e)
          label1.Text = "Hello World!!!";
  2. Create a user interface for the user control.

    <asp:Label id="label1" runat="server"/>
    <asp:button id="button1" text="Hit" OnClick="button1_Click" runat="server" />

How to use a user control in a Web Forms page

  1. Create a new Web Forms page (.aspx) in Microsoft Visual Studio .NET 2002, Microsoft Visual Studio .NET 2003, Microsoft Visual Studio 2005, or any text editor.
  2. Declare the @ Registerdirective. For example, use the following code.

    <%@ Register TagPrefix="UC" TagName="TestControl" Src="test.ascx" %>

    Note Assume that the user control and the Web Forms page are in the same location.

  3. To use the user control in the Web Forms page, use the following code after the @ Registerdirective.

              <form runat="server">
                   <UC:TestControl id="Test1" runat="server"/>

How to create and use a simple custom control that extends from System.Web.UI.Control using Visual Studio

  1. Start Visual Studio.
  2. Create a class library project, and give it a name, for example, CustomServerControlsLib.
  3. Add a source file to the project, for example, SimpleServerControl.cs.
  4. Include the reference of the System.Web namespace in the references section.
  5. Check whether the following namespaces are included in the SimpleServerControl.cs file.

  6. Inherit the SimpleServerControls class with the Controlbase class.

    public class SimpleServerControl : Control
  7. Override the Rendermethod to write the output to the output stream.

    protected override void Render(HtmlTextWriter writer) 
    	 writer.Write("Hello World from custom control");

    Note The HtmlTextWriter class has the functionality of writing HTML to a text stream. The Write method of the HtmlTextWriter class outputs the specified text to
    the HTTP response stream and is the same as the Response.Write method.

  8. Compile the class library project. It will generate the DLL output.
  9. Open an existing or create a new ASP.NET Web application project.
  10. Add a Web Forms page where the custom control can be used.
  11. Add a reference to the class library in the references section of the ASP.NET project.
  12. Register the custom control on the Web Forms page.

    <%@ Register TagPrefix="CC " Namespace=" CustomServerControlsLib " Assembly="CustomServerControlsLib " %>
  13. To instantiate or use the custom control on the Web Forms page, add the following line of code in the <form> tags.

    <form id="Form1" method="post" runat="server">
        <CC:SimpleServerControl id="ctlSimpleControl" runat="server">
        </CC:SimpleServerControl >

    Note In this code, SimpleServerControl is the control class name inside the class library.

  14. Run the Web Forms page, and you will see the output from the custom control.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s