Online training on user controls in ASP.NET
Part five of a five-part series of blogs

User controls are the key to using ASP.NET effectively - this blog explains how and why to create and use user controls, with code examples for VB and C#.

  1. User Controls in ASP.NET
  2. How the User Controls will Work for our Case Study
  3. Raising Events for User Controls - Visual Basic
  4. Raising Events from User Controls in C#
  5. Problems with User Controls in ASP.NET (this blog)

This user controls tutorial is part of a much larger ASP.NET tutorial series of blogs.  Wise Owl's main business is running classroom-based training courses - have a look at our other .NET courses.

Posted by Andy Brown on 17 October 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Problems with User Controls in ASP.NET

User controls are like fairies: they only exist if you believe in them.  What this means is that you have to be confident that you're doing the right thing to incorporate user controls in your websites (as you should do).

Problem 1 with User Controls - Compilation Errors

If you have a single error in a user control, you'll get multiple errors in your website.  Let's introduce a single error, by omitting a semi-colon at the end of a C# line:

Missing semi-colon at end of line

There should be a semi-colon where the blue wavy line is in this code-behind file for the ucTaskRecord user control.

This single error creates 14 errors:

List of compilation errors

Just some of the 14 error messages generated.

The trick is to sort the errors by number:

Sorting error list

Click at the top of the error number column as shown to sort the errors into order.

You can now double-click on the first error to correct it.

Even then, you often have to rebuild your website to convince it that the user control should be trusted!

Problem 2 with User Controls - Refreshing the Control List

The biggest nuisance with user controls is persuading the containing page that they exist, although this is irrelevant for VB:

The list of user controls

Getting the user control you've written to appear in this list can take forever!


Even when the user control does appear, your custom events might not:

List of events for user control

Even when you've chosen a user control, its events don't always refresh immediately.

When you're certain that you've saved your user control and built your website without any compilation errors, the only way I've found to speed up refreshing the list of events is to close down the entire website in Visual Studio and reopen it.  If anyone knows of a better way, please comment at the bottom of this page!

Problem 3 with User Controls - Hiding Them

Normally, setting the Visible property of a user control to False is sufficient to make sure it doesn't appear when you first load a page:

3 user controls with Visible set to False

These 3 user controls should be hidden when you load the containing page.


Occasionally, however, you seem to have to hide them when first loading the page.  Here's the VB code to load the main page of our site:

Protected Sub Page_Load(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles Me.Load


'hide error messages on every load

ucError1.Visible = False


If Not Page.IsPostBack Then


'ensure can only see search form initially

Me.ucTaskList1.Visible = False

Me.ucTaskRecord1.Visible = False

Me.ucTaskSearch1.Visible = True



End If

End Sub

And here's the C# equivalent:

protected void Page_Load(object sender, EventArgs e)


// hide error on every load

ucError1.Visible = false;


if (!this.IsPostBack)


// ensure can only see search form initially

this.ucTaskList1.Visible = false;

this.ucTaskRecord1.Visible = false;

this.ucTaskSearch1.Visible = true;




In both cases we formally hide the user controls we don't want to see initially, just in case.

This blog has 0 threads Add post