|
Web Forms
Web Forms are based on ASP.NET. Working with Web Forms is similar to working
with Windows Forms. But the difference is that we will create Web pages with Web forms
that will be accessible by a Web browser. Web Forms are Web pages that serve
as the user interface for a Web application. A Web Forms page presents information
to the user in any browser or client device and implements application logic using
server-side code. Web Forms are based on the System.Web.UI.Page class.
The class hierarchy for the page class is shown below.
Object
Control
TemplateControl
Page
Components of Web Forms
In Web Forms pages, the user interface programming is divided into two parts: the visual
component (design page) and the logic (code behind
page).
The visual element is the Web Forms page. The page consists of a file with static
HTML, or ASP.NET server controls, or both simultaneously. The Web Forms page works
as a container for the static text and the controls we want to display. Using the
Visual Studio Web Forms Designer and ASP.NET server controls, we can design the form
just like in any Visual Studio application.
The logic for the Web Forms page consists of code that we create to interact with
the form. The programming logic is in a separate file from the user interface file.
This file is the "code-behind" file and has an ".aspx.vb"
(VB) or ".aspx.cs" (C-Sharp) extension. The logic we write in the code-behind file
can be written in Visual Basic or Visual C#.
The code-behind class files for all Web Forms pages in a project are compiled into
the project dynamic-link library (.dll) file. The .aspx page file is also compiled,
but differently. The first time a user loads the aspx page, ASP.NET automatically
generates a .NET class file that represents the page, and compiles it to a second
.dll file. The generated class for the aspx page inherits from the code-behind class
that was compiled into the project .dll file. When the user requests the Web page
URL, the .dll files run on the server and dynamically produces the HTML output for
your page.
When you open a new ASP.NET Web Application in Visual Studio .NET the form that
is loaded (WebForm1.aspx) looks like the image below.
The form opens in design mode and you can switch to HTML view by clicking on the HTML
tab. Startvbdotnet.aspx file in the image above is standard HTML with ASP elements
embedded in it. The file looks like the image below.
From the above image notice the Codebehind attribute (second
line). The codebehind attribute connects this code to the appropriate Visual Basic
or C# code (code behind file). Startvbdotnet.aspx.vb, the codebehind file for Startvbdotnet.aspx
where you write your logic looks like this:
Public Class Startvbdotnet Inherits System.Web.UI.Page
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
Private Sub InitializeComponent()
End Sub
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
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load
'Put user code to initialize the page here
End Sub
End Class
|
Notable Properties of Page Object
Below are some notable properties of Page objects:
Application: Gets an application object
ClientTarget: Gets/Sets if you want to override automatic
browser capabilities detection and handle page rendering for specific browsers
ErrorPage: Gets/Sets an error page's URL in case there are
unhandled page exceptions
IsPostBack: Indicates if a page was created after a client
postback or if it is being loaded for the first time
IsValid: Indicates if the page validation was successful
Request: Gets the current http request object
Response: Gets the current http response object
Server: Gets the current server object
Session: Gets the current session object
Site: Gets Web Site data
User: Gets data about the user
Validators: Gets a collection of validation controls on
the page
The ASPX Extension
Many of us wonder why the extension for ASP.NET is .aspx. Well, long time ago,
when ASP.NET was being developed at Microsoft it was referred to as ASP+ (ASP Plus). You
can't use a "+" symbol in a filename but if you turn the + symbol about 45 degrees,
it looks like a x. Microsoft chose .aspx as the extension of ASP+. After the name
was changed to ASP.NET, Microsoft did'nt change the extension and left it as aspx.
|