Defining Web Form Functionality


Let’s create a new project in Visual Studio .Net. Begin by creating a virtual directory called MyASP.NETPages. Then open Visual Studio .NET and create the new ASP.NET web application project called IFCE. Initially, the location where the application will be stored looks like this: http://localhost/WebApplication1. Rename it to the correct project name, “IFCE.” Also, rename public class1 to Register. Notice how public class Register inherits from class System.Web.UI.Page. The next item of interest, Private Sub InitializeComponent(), calls the subroutine InitializeComponent(). Next, Private Sub Page_Init(ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Init initializes the page.

Public Class Register
Inherits System.Web.UI.Page
#Region "Web Form Designer Generated Code"
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration
is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent ()
End Sub
#End Region
End Class

WebForm1.aspx displays the code-behind feature. Inside the ASP.NET script delimiters, the page language is VB; the CodeBehind is Web Form1.aspx.vb. The code indicates that the web form inherits from the IFCE WebForm1. The form ID is Form1; the method is Post, specifying that the page must run on the server.

%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="IFCE.WebForm1"%>
<html>
<head>
<TITLE>WebForm1</TITLE>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE LANGUAGE" content = "Visual Basic .NET 7.1">
<meta name="defaultClientScript content="JavaScript">
<meta name="vc_targetSchema content="http://schemas.Microsoft.com/
intellisense/ie5">
</head>
</html>
<body MS_POSITIONING="GridLayout">
<form method="post" runat="server>
</form>
</body>
</html>

Next, click on the Register icon in the Solution Explorer and select View Designer. Then, click on the Toolbox icon and drag a label onto the web form. Select the label’s properties and type RegisterClient to the right of the text property. Finally, compile the application, and the label will display in Internet Explorer as follows:

 Register Client

Although this ASP.NET server page does not allow for any user interaction, it does demonstrate browser-based application functionality in its most elementary form.

The Global.aspx.vb file contains information about the sequence of events and when they fire. The file begins by importing System.Web and the System.Web.SessionState. Public Class Global inherits the System.Web.HttpApplication class. After the InitializeComponent() method performs its task and allows for any other initialization, several events fire. The first event is the Application_Start method. The next event, Session_Start, fires, followed by the Application_BeginRequest method, which fires at the beginning of each subsequent request. Then the Application_AuthenticateRequest method fires. Other methods include Application_Authentication, Application_Error, Session_End, and Application_End. Here is the Global.aspx.vb file:

Imports System.Web
Imports System.Web.SessionState
Public Class Global
Inherits System.Web.HttpApplication
#Region " Component Designer Generated Code "
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub

#End Region

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
'Fires when the application is started
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
'Fires when the session is started
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
'Fires at the beginning of each request
End Sub
Sub Application_AuthenticateRequest
(ByVal sender As Object,
ByVal e As EventArgs)
'Fires upon attempting to authenticate the user
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
'Fires when an error occurs
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
'Fires when the session ends
End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
'Fires when the application ends
End Sub

End Class

The lesson learned from examining this file is that the ASP.NET execution works entirely differently from classic ASP pages. Every .aspx page is automatically converted to a class and subsequently compiled to an assembly the first time it is accessed by a client. Expect some latency (delay) from this scenario.

When the user accesses the page, the generated assembly executes and creates an instance of that page. The page object provides a method for each event and generates output that is ultimately sent to the client’s browser.

Creating a Web Form

The following ASP.NET browser-based application shows how to enter data and display it back to an ASP.NET label control.

Open Visual Studio .NET and create a new project. Select Visual Basic Projects and click on the Web Application icon. Provide a project name such as RegisterNewClient. Then, select the machine where you wish to create the web site. Click OK to begin the process of creating a new web application. Rename WebForm1 to RegisterNewClient.aspx. Next, select the Toolbox and create the form with the appropriate controls itemized in Table 6-2.

Table 6-2: Creating a Web Input Form

Control Type

Property

Value

Label

Name

Label1

TextBox

Name
Text

txtFirst

Label

Name
Text

Label2

TextBox

Name
Text

txtLast

Label

Name
Text

Label2

Button

Name
Text

btnSubmit

Once you verify that this form runs properly, right-click the Register.aspx page and click Set as Start Page. Run the form, and you will be able to enter data in the text fields. Then, click the Register button so the browser will render your page. However, you will not see anything you entered because the page does not contain instructions to do anything yet. Double-click the Register button and enter the following code:

Private Sub btnSubmit_Click 
(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles btnSubmit.Click
lblName.Text = txtLast.Text & ", " & txtFirst.Text
End Sub

Run Register.aspx again, and the result displays in the label as “Peltzer, Dwight.” You have retrieved the data you just entered in the Last Name and First Name text boxes and placed the text in the label control.




.NET & J2EE Interoperability
Microsoft .NET and J2EE Interoperability Toolkit (Pro-Developer)
ISBN: 0735619220
EAN: 2147483647
Year: 2004
Pages: 101
Authors: Simon Guest

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net