Online training on user controls in ASP.NET
Part four 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# (this blog)
  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 from User Controls in C#

This page shows how to get the search form to report back to the main page what's happened on it (which it does by raising an event).  I've concentrated on one aspect of the final website - you can download the full system from the home page for this tutorial.

It's worth mentioning at this point that I've used the simplest and most flexible techniques I could.  In particular, my functions can pass any arguments they like, whereas most C# programmers insist on restricting themselves to passing information wrapped up in a class which inherits from the EventArgs base class.  And if that sentence didn't make sense, that's my point!

Defining a Delegate for the User Control

To be able to call an event for a user control, you have to define it first.  This you do by creating a delegate (they used to be called function pointers in C#):

using System;

using System.Collections.Generic;

using System.Data;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public delegate void ucTaskSearch_SearchHandler(

int StatusId, string ContainsText);

 

public partial class ucTaskSearch : System.Web.UI.UserControl

{

This states that any containing page will be able to call an event of type ucTaskSearch_SearchHandler, and pass to it:

  1. An integer, known as StatusId; and
  2. A string of text, known as ContainsText.

Defining an Event-Handler for the User Control

Now that you have a delegate, you can create the event-handler for the user control:

public partial class ucTaskSearch : System.Web.UI.UserControl

{

public event ucTaskSearch_SearchHandler Searching;

This states that the user control exposes an event called Searching, which when raised will automatically refer to an instance of the delegate called ucTaskSearch_SearchHandler, and hence require the same arguments.

Raising the Event

When someone clicks on the Search button in our user control, it should raise an event to the main page saying what search criteria were entered:

Search form with criteria entered

Clicking on the Search button should automatically notify the main page about what's happened.

 

Here's the event-handler for this button

protected void btnSearch_Click(object sender, EventArgs e)

{

// search for this information

if (this.Searching != null)

{

this.Searching(StatusId, txtContains.Text);

}

}

 

int StatusId {

get {

return(Convert.ToInt32(ddlStatusId.SelectedValue));

}

set {

ddlStatusId.SelectedValue = value.ToString();

}

}

The StatusId property just provides an easy way to get at the value chosen for the status dropdown list.

Assigning an Event-Handler in the Main Page

Now that we've raised our event, it's time to handle it.  To do this, first attach an event-handler on the main page to the Searching event for the ucTaskSearch user control:

Choosing OnSearching event for user control

All events have the prefix On. Remarkably, when you start typing events that you've created, Intellisense kicks in.

 

We'll choose to call our event-handler SearchingHandler:

User control for searching

The user control now has a handler for the Searching event.

 

Writing the Main Page's Event Handler

The final step is to write the SearchingHandler event-handler in the main page:

public void SearchingHandler(int StatusId, string ContainsText)

{

// trying to search for tasks with these criteria

clsSpQuery spTaskList = new clsSpQuery("spTaskList");

 

if (StatusId > 0) { spTaskList.AddParameter("StatusId", StatusId); }

 

if (ContainsText.Length > 0) {

spTaskList.AddParameter("ContainsText", ContainsText);

}

 

DataTable dtTask = spTaskList.DtSelectedRecords;

if (dtTask.Rows.Count == 0) {

 

ucTaskList1.Visible = false;

ucTaskSearch1.Visible = true;

 

// there are no records - leave search form displayed

ucError1.ErrorMessage = "No tasks satisfy these criteria";

return;

}

 

// otherwise, display results

ucTaskList1.dtTasks = dtTask;

ucTaskList1.Visible = true;

ucTaskSearch1.Visible = false;

}

This event-handler will use a stored procedure called spTaskList to return all of the tasks which meet the search criteria set. The event-handler will work only if you define a write-only property called dtTask for the ucTaskList user control, to tell it what records to display:

public DataTable dtTasks {

set {

gv.DataSource = value;

gv.DataBind();

}

}

When set, this dtTasks property will automatically populate the gridview of tasks with data.

 

You can download the full website from the home page for this online ASP.NET training blog.

This blog has 0 threads Add post