new class - University of Worcester

Download Report

Transcript new class - University of Worcester

COMP3241
E-Business Technologies
Richard Henson
University of Worcester
October 2012
Week 6: Coding a C#
Shopping Cart
n
By the end of this session you will be
able to:
explain the structure of a C# .aspx page
including the code-behind file & the design
file
apply principles of asp.net design to the
creation of a shopping system
C# Classes
n
A class is a bit like an entity in systems
analysis…
 can have “properties”
 equivalent to attributes…
n
Has to be carefully coded…
 could be “public” or “private”
 properties are variables
» should really be “private”
 code snippets (methods) – public
C# Properties
n
Required to “instantiate” a class
e.g. if a class is defined with colour as a
property
then a single value “red” would be enough
to create an “object” of that class
C# Constructors & Methods
n
These are different [public] code segments
associated with a class
 a constructor uses property(ies) to define or
“construct” a member/object of that class
 methods can include any programming structures
and do stuff using property values
“Partial” Classes and Methods
n
Partial Classes
 used to describe a situation where different parts
of a class are defined in different places
n
Partial Methods
 within a partial class
 must return a “void” value
 implicitly “private”
C# “classes”
and the Code
Behind model
n
When a .aspx page
is requested and
renders markup to a
browser…
 ASP.NET generates
& compiles the
“classes” that
perform the tasks
required to run the
page
“Classes” and
the code
behind model

the .net framework
also sets up the JIT
run-time
environment, and
converts source code
to “intermediate
language” (IL)
“classes” and
the code
behind model

This means that
the code that
runs is not
solely the code
that was created
for the .aspx
page and its
associated
“code behind” (!)
Generating and Running the
Page’s “class” Code

When a .aspx page runs, it has already
combined:
 the server-side elements in the page
» e.g. toolbox controls
 the event-handling code written as “code behind”

The class or classes that the compiler creates
depends on whether the page uses the
single-file model or the code-behind model
 no need to pre-compile such pages into
assemblies!
Generating and Running the
Page’s “class” Code

.NET framework dynamically compiles & runs
.aspx pages the first time user requests them:
 IF…any changes are subsequently made
» either to the page
» or resources the page depends on
 THEN… the page is automatically recompiled

.NET also supports precompilation of a Web
site:
 to enhance performance
 to perform error checking
 to support site deployment
Single-File Pages

When the page is compiled:
the compiler:
» generates then…
» compiles
EITHER
» a new class that derives from the base Page
class
OR
» a custom base class defined with the Inherits
attribute of the @ Page directive.
Single-File Pages
Example:
» IF… a new ASP.NET
Web page named
SamplePage1.aspx is
created in application
root directory
» THEN… a new class
named
ASP.SamplePage1_as
px will be derived from
the Page class
Single-File Pages
n
IF that single-file is inside an application
subfolder
subfolder name is used as part of the
generated class & contains:
» declarations for the controls in the .aspx page
» the event handlers and other custom code
After Page generation…
n
Generated class…
compiled into an assembly
assembly loaded into the application
domain
n
Page class…
instantiated
executed
renders output to the browser…
Effect of Page modifications
n
IF a page changes & this would affect
the generated class… e.g.
adding controls
modifying code
n
THEN
compiled class code is invalidated
a new class is generated
Using C# classes with “eventdriven” controls
EXAMPLE (an “add to cart” button):
protected void DataList1_ItemCommand (objectsource,
DataListCommandEventArgs e)
{ if (e.CommandName == "AddToCart")
{ // Add code here to add the item to the shopping cart.
// Use the value of e.Item.ItemIndex to retrieve the data
// item in the control.
}
}
The “global.asax”
n
An optional way of including otherwise unlisted
events, objects, with application aspx scripts
 normally placed in root folder
n
.NET is configured so that any direct URL
request for global.asax is automatically rejected
» external users therefore cannot download or view the code
in it
» makes the settings very secure…
n
Parsed & compiled at run time
 dynamically generated .NET class (HttpApplication)
Use of Multiple
Scripting languages in a Page
n
General rule… DON’T!
 use of multiple compilers wastes system resources
n
If you must:
 HTML <SCRIPT> tag and its LANGUAGE added
within the .aspx code
» usually placed after the <HEAD></HEAD> tags
» RUNAT attributes (where code is executed) optional
 Syntax:
» <SCRIPT LANGUAGE=ScriptingLanguage RUNAT=Server>
n
script…
» </SCRIPT>
» similar to embedded client code
Keep it simple!
n
Multiple languages not even
recommended for different pages in the
same application!!!
makes the ASP.net engine load multiple
scripting engines
needs more processing
» which piece of script should be sent to which
scripting engine!
reduces the responsiveness and scalability of
the application
Potential Scripting Problems in a
RAD Environment…
n
Correct scripting language always needs to
be defined at the start of the page
 dynamic “new” pages provide a range of options
n
Inserted controls:
 can be inserted in the wrong place…
 can themselves become muddled (start again?)
 may already contain HTML code or (if database)
SQL code
Potential Problems of Scripting in
the RAD Environment…
(continued)
Embedded server script code may depend on
a link to a database
 link needs to be functioning correctly…
 database field types must correspond with script
equivalents
If the database tables/fields change
significantly, the script must accommodate the
field changes…
 roll out changes carefully
 may be easier to recreate control or whole page…
Use of Comments in aspx files
Apostrophe for VBScript
 ‘This is a VBScript comment
Three slashes for C#
 /// <summary>
 /// The “myClass” class represents an
arbitrary class
 /// </summary>
An excellent C# resource:
 http://www.softsteel.co.uk/tutorials/cSharp/cont
ents.html
More about Objects
n
In general terms… chunks of server script
code that will together provide web
functionality
 using RAD tools, added to a dynamic HTML page
“at the click of a mouse button”
 available in a number of languages
» asp, asp.net
» php, cold fusion
n
Several objects usually used together to
create a useful web application e.g.
 data capture form and sending data to database
 presentation of data from a database
“Components” and “Objects”
n
Very similar and easily confused…
Object
» part of the library of files available to the whole
application
Component
» a number of objects
» a mini-application in its own right
» any instance of a component must be explicitly
created before it can be used
ASP. Net Components
n
Early IIS ASP environment was mostly
COM components!
Problem having a webserver containing a
lot of source code!
n
Microsoft still provide a library of COM
components
Many old asp components in VB that have
been rewritten and converted into C#
assemblies
Using COM in aspx files
n
COM (later COM+) components…
 popular with developers…
 provided consistent, reusable functionality
 easy to maintain and share
 could be modified or enhanced without affecting the rest
of the application
n
Microsoft therefore reluctant to give up a good
thing and encouraged using COM+ with .net
 http://msdn2.microsoft.com/en-us/library/bb735856.aspx
Evolution of
“VBScript Request” object

“Request” was the only way asp scripts
could retrieve the input data entered by the
user in the input fields in the page
 used to access information passed from the
browser when requesting a Web page
 handles HTML form fields, client certificate
information, and cookies
ASP.net and directing
user input

The “HttpRequest” class includes…
 event handling controls
 web controls

Web controls can also be pre-written and
pre-compiled

Really useful in designing pages to
handle user input