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