ASP.NET

Events

Normally, a class is meant to interact with other classes, to request values from them, to call their methods, to provide values to other classes, or to…

4 min read984 words10 code samples

ASP.NET With C# - Lesson 16: Events

Events#

Events Fundamentals#

Introduction#

Classes are generally designed to collaborate with one another by requesting data or invoking methods. This type of interaction typically happens when a particular event alerts an object to take action.

  • The operating system of the computer a web page visitor is using can initiate an action and must signal it to the (or some) objects of the same computer
  • The operating system of the computer of a web page visitor can initiate an action and must signal it to the objects of the web page the visitor is using
  • An object on a web page can initiate an action and must signal it to the operating system on the computer the web visitor is using
  • An object on a web page can initiate an action and must send a signal to the server that is hosting the web site
  • An object on a web page can initiate an action and must signal it to one or more objects on the same web page
  • An object on a web page can initiate an action and must signal it to one or more objects on other web pages of the same web site

When the operating system, a web page, or an object signals an occurrence, it is also said to fire an event.

Event Creation#

An event is declared like a pseudo-variable but based on a delegate. Therefore, to declare an event, you must have a delegate that would implement it. Here is an example:

C#
<script runat="server">
public delegate string Simple();

public class Exercise
{
    public string Welcome()
    {
	return "ASP.NET Development With C#.";
    }
}
</script>

To actually declare an event, you use the event keyword with the following formula:

C#
[attributes] [modifiers] event type declarator;
[attributes] [modifiers] event type member-name {accessor-declarations};

The attributes factor can be a normal attribute. The modifier can be one or a combination of the following keywords: public, private, protected, internal, abstract, new, override, static, virtual, or extern. The event keyword is required, followed by the name of the delegate that specifies its behavior. Like everything in a program, an event must have a name. This would allow the clients to know what (particular) event occurred. Here is an example:

C#
<script runat="server">
public delegate string Simple();

public class Exercise
{
    public event Simple Example;

    public string Welcome()
    {
	return "ASP.NET Development With C#.";
    }
}
</script>

After declaring the event, you must define a method that calls the event. Here is an example:

C#
<script runat="server">
public delegate string Simple();

public class Exercise
{
    public event Simple Example;

    public string Welcome()
    {
	return "ASP.NET Development With C#.";
    }

    public string Announce()
    {
	return Example();
    }
}
</script>

When the event occurs, its delegate would be invoked. This specification is also referred to as hooking up an event. As the event occurs (or fires), the method that implements the delegate runs. This provides functionality to the event and makes the event ready to be used. Before using an event, you must combine it to the method that implements it. This can be done by passing the name of the method to the delegate, as if the delegate was a constructor. You can then assign this variable to the event's name using the += operator. Here is an example:

C#
<%
    Exercise exo = new Exercise();

    exo.Example += new Simple(exo.Welcome);
%>

Once this is done, you can call the method that is associated to the delegate. Here is an example:

C#
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">
public delegate string Simple();

public class Exercise
{
    public event Simple Example;
    
    public string Welcome()
    {
	return "ASP.NET Development With C#.";
    }

    public string Announce()
    {
	return Example();
    }
}
</script>

<title>Exercise</title>
</head>
<body>

<%
    Exercise exo = new Exercise();

    exo.Example += new Simple(exo.Welcome);

    Response.Write(exo.Announce());
%>
</body>
</html>

Instead of the += operator used when initializing the event, you can implement add and remove of the event class.

Events on a Web Page#

Introduction#

A web page functions as a document displaying text and images to users. Through an interactive page, visitors can communicate with the backend by filling out text boxes, choosing from lists, or clicking buttons.

Implementing an Event#

When designing a web page, you decide what objects you want to add to the page. After adding the control, you must identify what event you want to implement. You have two options:

  • Every web control has a default event. That event is identified as the most likely to be used. To launched that event, you can double-click the control
  • To implement another event, on the web page, click the control. In the Properties window, click the Events button to see the list of events that are available:

To launch an event, double-click the name of the event or the empty box on its right side. Any of these two actions would open the Code Editor and skeleton code would be written for you. In the event's body, you can then write the necessary code.

Code Inline and Code Behind#

Inline Code#

So far, to create a class, we include its code in the head section of a web page, using a script. This technique is referred to as code inline because the class is created in the web page that will use it. Here is an example:

C#
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">
class House
{
}
</script>

<title>Exercise</title>
</head>
<body>

</body>
</html>

Code Behind#

The inline code makes short the number of files involves on a web page. It also assumes that only one person is working on the web page. If you have a team of programmers, designers, and implementers working on the same project, it is better to separate the web form from the code that manages it. To do this, you can create the class that manages a form in a separate file. This is referred to as code behind. To create the class in its own file, create the class as a partial class and save it as a C# file. Here is an example of a class named House and created in a file named House.cs:

C#
partial class House
{

}

In the body of the class, implement it as you want. The members of that class must be created either as public or protected. After creating the class, to reference it in the page that contains the form, in the <%@ Page %> tag, add an attribute named CodeFile and assign the name of the partial file to it. This would be done as follows:

C#
<%@ Page Language="C#" CodeFile="house.cs" %>

After specifying the file that holds the class, you must also specify the name of the class. To do this, add another attribute named Inherits and assign the name of the class to it. This would be done as follows:

C#
<%@ Page Language="C#" CodeFile="house.cs" Inherits="House" %>

After defining these attributes, the class can be utilized within the web page, allowing you to access its fields and properties as needed. Previous Copyright © 2009 C# Key Next