Home

Events

 

Events Fundamentals

 

Introduction

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 perform actions on behalf of other classes. In some cases, to make this possible, a class must send a signal to another class or to other classes. 

An event is a signal that an object sends out to let any other object that is interested, to know that something happened.

As you may imagine, there are various types of events that can occur:

  • 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:

<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:

[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:

<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:

<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:

<%
    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:

<%@ 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 is a document that presents text, graphics, and other items to its visitor. An interactive web page is equipped with objects, called web controls, that a visitor uses to create or select values and send them to a web server. During this interaction, the visitor can click some text, push a button, or select from a group. Eventually, the user can click a button. As these actions are performed, the object on which they are acted on fires one or more event. You as the web developer must find out what object was used and you must identify what event was fired.

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:

Events

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:

<%@ 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:

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:

<%@ 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:

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

After specifying these attributes, you can use the class in the web page and you can reference the form in the events associated with the class.

 
 
   
 

Previous Copyright © 2009-2015, FunctionX, Inc. Next