Assignment Title: Web Programming Assignment 1 For: Michael Dacey

salmonbrisketΛογισμικό & κατασκευή λογ/κού

2 Νοε 2013 (πριν από 4 χρόνια και 2 μήνες)

82 εμφανίσεις




Assignment Title:

Web Programming


Assignment 1


For: Michael Dacey


By: Neil Page


Date: 23/02/2007







1

Contents Page



1.0 Introduction
................................
................................
................................
...............

2

2.0 Code
-
Behind Pages

................................
................................
................................

3

3.0 HTML Controls and Web Server Controls


The Differences

......................

5

4.0 View State

................................
................................
................................
..................

7

5.0 Page Events

................................
................................
................................
..............

8

6.0 Page Directives

................................
................................
................................
......

10

8.0 Bibliography

................................
................................
................................
...........

15






























2




1.0 Introduction


Following extensive research into the .Net framework, a report will be written
describing the following aspects:


Code
-
Behind pages


Differences between HTMLControls and Web Server Controls


Vie
w State


Page Events


Page Directives
and in particular the Debug,
Trace attributes

and the
Control Tree


The

Calendar control

In order to explain these items correctly, several screenshots have been taken,
and annotated.





















3

2
.0
Code
-
Behind Pa
ges


When a user begins a new project within the ASP.Net framework, they are given
the choice of creating either one, or two different types of ASP.Net pages.

If a user chooses to create only one ASP.Net page, then the page controls and
the page code are p
laced on the same page. Alternatively, if two files are
created, the page code is placed in a separate page. This is what is referred to
as a ‘code
-
behind’ page.

The page code is the code that is contained inside the <script runat=”server”>
tag.

When a cod
e
-
behind page has been utilised, it is indicated within the @ Page

directive of the main web form (1).


The ‘ticked’
checkbox signifies
that the user
wishes to have the

page code

placed
on a

separate
page
.



4




Although utilising a code
-
behind page can simplify the overall code, there are
disadvantages of using them as well. According to Ladka

(2)
, co
de
-
behind pages
do not import any namespaces, as .aspx pages do. These namespaces must be
explicitly included in the code
-
behind page.

Additionally, a class must be created to contain the logical code in the code
-
behind page so that the developer can inher
it this class into their .aspx page.

Finally, all instances of every object that is in your .aspx page that you will
reference in your code
-
behind page must be created. This is done in the same
way that any other object is created, but the name of the obj
ect created in the
code
-
behind page must be identical to the ID of the object in the .aspx page.

For highly complex pages, the number of objects and namespaces you need to
create in your code
-
behind pages can be so extensive that this extra work can
outwei
gh the advantage of being able to separate your code in the first place.







An example of
what the code
-
behind page looks
like with a small
piece of page
code on it.


5

3
.0
HTML Controls and Web Server Controls


The Differences


Within ASP.Net, HTML controls are initiated with the runat=”server” attribute. For
example, the following HTML creat
es an instance of HTMLInputText named
textbox1:

<input type=”text” runat=”server” id=”textbox1”
value=”test”


These

controls map directly to their HTML counterparts and are useful for
providing back
ward compatibility with ASP.” (3
)

A user developing a web
form page within ASP.Net not only has server
-
side
HTML controls to program with, but also a far richer set of controls known as
‘web server controls’.

Web server controls provide substantial benefits in visual
appearance and functionality

compared with sta
ndard HTML controls. Delphi
programming

(4)

state that “
w
hile server
-
side HTML controls map one
-
to
-
one to
HTML elements, Web Server controls are defined as abstract controls in which
the actual HTML rendered by the control can be quite different from the m
odel
that you program against.


HTML controls provide an easy transition from ASP to
ASP.Net. They do not provide a useful object model and are difficult to program
against.

Web server controls include common form controls, such as buttons and
textboxes, b
ut also offer more complex controls such as calendars and lists.
These controls can also be manipulated, and styled as the user sees
appropriate.

At design time, there are big differences between HTML controls and Web server
controls
, however, when the cod
e is run within a browser, both sets of controls

6

will be rendered to standard HTML code. For example, an ASP.Net checkboxlist
will look something like:

<asp:checkboxlist id=CheckBoxList1





runat="server"></asp:checkboxlist>


When this source code is
viewed within a browser, it will look something like:

<input id="CheckBoxList1_0"
type="checkbox" name="CheckBoxList1:0"
/>


































7

4
.0
View State


V
iew State is a function built within ASP.Net which enables controls to retain the
v
alues of their properties between posts. For example, if the user typed some
text into a label control, and submit the form, when the page is rendered again,
the contents of the label control are preserved. Walther explains that “the magic
of view state is

that it does not depend on any special server or browser
properties. View state is implemented with a hidden form field called
VIEWSTATE that is automatically created in every Web Forms page.”



It is also possible to disable the view state function;
if for example, a form
contained a radio button list, with 1000 options. The rendering of this list again
and again could well cause the rendering of the page to be slowed considerably.
(5)













Example of the
viewstate function
employed within this
website. It is only
visible when the
source code is
viewed in the
browser.


8

5
.0
Page Events


When an ASP.Net

page is called, they
go through a series of routines. These
routines are known as page events. These events occur in the same way, in the
same order, every time.

Ladka

(2)

identifies the three main page events as:


Page_Init


Page_Load


Page_Unload

The Page_Init event occurs firs
t when an ASP.Net page is executed. During this
event, controls are created. Therefore, it would be bad practice to try and access
controls in this event because there is no guarantee that they will have been
created yet.

It is important to note that Page_
Init only fires the very first time, and will not
occur when you use post back within a web form.

The most commonly utilised event is the Page_Load event. This occurs after the
Page_Init event, and at this point, all of the objects within the page have bee
n
created, and are ready for use.

The Page_Load event is usually where the
majority of the web developers’ work will be carried out.

The Page_Load event fires every single time a page is loaded. This includes
when a post back is used within a web form.



9























An example of a page event
in action. In this case, when
the page loads, it

will send
the date and time located on
the server to the relevant
label.


10

6
.0
Page Directives


“Page
Directives specify the settings used by the page and the user control
compiler when they process ASP.Net Web Form pages (.aspx) and

User Control
(.ascx) files.” (6
).

Two of the most commonly used directiv
es are the ‘@ Page’ directive, and the
‘@ Control’ directive.

The ‘@ Page’ directive applies page
-
specific attributes to a web form, such as
which scripting language is used on the page.



Some other useful @ Page directives have been identif
ied and ex
plained by
Ladka (2
).

They are:


EnableSessionState



This defines session
-
state requirements for the
page. It is set to true by default, but can be disabled by being set to false,
and also given read
-
only properties by being set to ‘read
-
only’.


EnableViewS
tate



This direct control defines whether ViewState is
maintained across page requests. It is set to true by default.

An example
of the

@ Page directive
being used to define
the scripting language,
master page file, title,
and related theme of
the page.


11


Explicit



This specifies whether all of the variables within a page are
declared with a ‘Dim’, ‘Private’, ‘Public’ or ‘ReDim’ statement
in VB.Net. It
is set to false by default.


Src



Defines the name of a code
-
behind page if one has been utilised.


An extremely useful tool that can be utilised within Visual Studio.Net is the Debug
attribute. “Because ASP.Net pages are compiled into assemb
lies, you can debug
them in much the same way you might debug any other .Net component.”

(7
)

In
order to initiate a debug, first you must be sure you compile your pages with
debug symbols. To do this, the debug attribute in the page directive must be set
t
o ‘true’.








Another
useful tool used for debugging is the trace feature. “It allows you to have
ASP.Net write a whole host of information about the currently executing request
to the page or to a trace
-
log.”
(8
) The information that is provided wil
l include the
SessionID, the time, type and status code of the request. Also, timing information
for events such as Init, PreRender, SaveViewState and Render. In addition, a
Control Tree of all controls in the page.

The debug
attribute has
been set to ‘true’
here within the
page directive.


12







According to Walther (5), durin
g a trace, “the Control Tree displays the hierarchy
of controls in a page. Both the render size and the view state size for each
control are displayed in bytes.”













The debug
attribute has
been set to
‘true’ here
within the
page directive.


An example of a
control tree

following
a trace.


13

7
.0
Calendar Control


The

calendar control is one of the rich controls offered
by ASP.Net. “It allows the
user to select a certain date,
all days in a week or month.” (9
). In order to setup a
calendar, the following simple code is required:

<asp:calendar runat=”server” />


This
would provide the user with a basic, ‘no frills’ cale
ndar.

However, as with
lots of other controls within ASP.Net, there are lots of different ways in which a
calendar can be customised.

The following is an example of the properties that the calendar control supports:


DayNameFormat


The appearance of the da
ys of the week can be
specified.

This is the calendar that has
been created using the
<asp:calendar> control. It
has also been customised
using some of the many
different ways in which
calendars can be altered.


14


NextPrevFormat


Format of next and previous month links are specified
with this property,


SelectedDates


Enables you to get asset or collection of selected dates.

There are three events that the calendar control provides
that are not inherited
from other control classes. These are:

i.

DayRender


This occurs when each day is created in the control
hierarchy for the calendar control

ii.

SelectionChanged


This occurs when the user selects a day, a week, or
an entire month using th
e calendar’s date select controls

iii.

VisibleMonthChanged


This occurs when the user navigates to the next
or previous month using the navigation controls in the title bar.














15

8
.0 Bibliography


(1)


S.
Walther
,
ASP.Net 2.0: Unleashed
, Indianapolis, SAMS,

2006

(2)


P. Ladka,
ASP.Net for Web Designers
, Indianapolis, New Riders, 2002

(3)


H. Bhashin,
ASP.Net Professional Projects
, Boston,
Thomson Course
Technology
, 2002

(4)


Z. Gajik,
An introduction to ASP.NET Web Server controls
, Delphi
Programming, [Online], Availabl
e at:
http://delphi.about.com/od/aspnet/l/aa091404a.htm
, [Accessed:
08/02/2007]

(5)


S. Walther,
ASP.Net Unle
a
shed
, Indianapolis, SAMS, 2003

(6)


N. Pandey,
Microsoft ASP.NET: Fast & Easy Web Development
,
Boston,
Thomson Course Technology
, 2002

(7)

F. Onion,
Essential

ASP.Net with Examples in Visual Basic .Net
, Boston,
Addison
-
Wesley, 2003

(8)

G. A. Duthie, M. Macdonald,
ASP.Net in a Nutshell
,
Sebastopol
, O’Reilly,
2003

(9)

D. Wolthuis, D. Mack, D. Seven,
Programming Data
-
Driven Web
Applications With Asp.Net
, Indianapolis, SA
MS, 2001