Online training on user controls in ASP.NET
Part three 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 (this blog)
  4. Raising Events from User Controls in C#
  5. Problems with User Controls in ASP.NET

This user controls tutorial is part of a much larger ASP.NET tutorial series of blogs.  Wise Owl also run a full range of ASP.NET training 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.

Raising Events for User Controls - Visual Basic

This page shows how to get our user controls to talk to each other, using the example of the search form.

I've missed some of the events out from this code - you can get the full picture by downloading the full application from the tutorial home page.

The Search Form User Control Events

When a user clicks on the search button in the user control called ucTaskSearch, the search form user control must tell the main page what's happened:

Search form filled in

If you fill in the search form and click on the Search button, the user control must tell the main page what's happened.


The first thing to do is to add a callable event to the user control:

Partial Class blog_ucTaskSearch

Inherits System.Web.UI.UserControl


'callable event for user control

Public Event Searching(ByVal StatusId As Integer,

ByVal ContainsText As String)

We've defined two arguments:

  1. The status dropdown list choice made (an integer); and
  2. The text that the to-do task must contain (a string).

We can then call this event when someone clicks on the Search button:

Protected Sub btnSearch_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles btnSearch.Click


'search for this information

RaiseEvent Searching(StatusId, txtContains.Text)


End Sub

Property StatusId As Integer


Return Convert.ToInt16(ddlStatusId.SelectedValue)

End Get

Set(ByVal value As Integer)

ddlStatusId.SelectedValue = value.ToString

End Set

End Property

The StatusId property just allows us to get at the value chosen in the dropdown list.

Handling the Search Events on the Main Page

Our main page - frmTask.aspx - should now listen out for and handle any events raised by the user control called ucTaskSearch.  First choose the user control on the main page:

Choosing user control object

In code view, click on the drop arrow to choose the user control on the page to which you want to attach code.


Having chosen this user control, you can now choose one of its events:

User control event choice

The events already coded are shown in bold. Most of the events listed are standard ones which will apply to any user control.

It can take a very long time for each of these lists to appear when you've added or changed a user control, and if you have a single compilation error anywhere in a user control it will never appear as a selectable object for the main page.

Here is the all-important code in the main form handling the Searching event on the user control:

Protected Sub ucTaskSearch1_Searching(ByVal StatusId As Integer, _

ByVal ContainsText As String) Handles ucTaskSearch1.Searching


'trying to search for tasks with these criteria

Dim spTaskList As New clsSpQuery("spTaskList")


If StatusId > 0 Then spTaskList.AddParameter("StatusId", StatusId)

If ContainsText.Length > 0 Then spTaskList.AddParameter( _

"ContainsText", ContainsText)


Dim dtTask As DataTable = spTaskList.DtSelectedRecords

If dtTask.Rows.Count = 0 Then


'if there aren't any tasks, redisplay search form

ucTaskList1.Visible = False

ucTaskSearch1.Visible = True


'show error message

ucError1.ErrorMessage = "No tasks for these criteria"


Exit Sub


End If


'otherwise, display results and hide search form

ucTaskList1.dtTasks = dtTask

ucTaskList1.Visible = True

ucTaskSearch1.Visible = False


End Sub

The code runs a stored procedure called spTaskList to show all of the records for the status and/or text chosen. In order for this to work, the ucTaskList user control will have to have a write-only property telling it which records to display:

WriteOnly Property dtTasks As DataTable

Set(ByVal value As DataTable)


'set gridview to display chosen tasks

gv.DataSource = value



End Set

End Property


The full website is downloadable from the main page of this tutorial, and includes events for all of the user controls.

This blog has 0 threads Add post