Download cos_381_day_26

Document related concepts

URL redirection wikipedia , lookup

Transcript
COS 381
Day 26
Agenda




Capstone projects are DUE May 10 at 1PM
I will be covering ASP.NET from the handouts
There will be one lecture on Database access
Quiz 4 on May 4


ASP.NET and Database Access
Assignment 7 (final one) is posted and is Due
on May 3 at Midnight


Create a complex ASP.NET page
http://perleybrook.umfk.maine.edu/samples/a
ssign7/UMFKorder.aspx
ASP.NET

Where ASP used interpreted
languages ASP.NET uses complied
languages



ASP example (code)
Same in ASP.NET (code)
ASP.NET uses many languages


We will use C#
Another example (code)
NEW Features of ASP.NET

The following are not part of ASP or
any other traditional Web
programming languages






Server Controls
Validation Controls
List Controls and Rich Controls
HTML Server Controls
User and Custom Controls
Web Services Integration
Server Controls

Programmable; prepackaged server
code program code written to
perform dynamic functions




<asp:tagname …>
Converted to HTML by server
Look the same as standard from
elements but can be dynamically
control by server side code at run-time
Example server controls (code)
Validation Controls

Used to validate data users enter
into from elements




Make a field required
Compare against range of inputs
Compare two values
Validation controls process data
BEFORE a form is submitted
List Controls and Rich Controls

List controls are used to iterate,
process and display dynamic data



Database query
Program array
Rich controls are complex
components


Calendar
Ad Rotator
HTML Server Controls

Same as HTML elements except


runat=“server”
Example (code)
User and Custom Controls

User Controls



Mini-ASP.NET pages
Included in other ASP.NET pages
Custom Controls

Written entirely in another language
like


C++, C# VB
Precompiled and added to ASP.NET
Web Service Integration



Small Programs to which you can
call functions and get results
Transfer data using XML over HTTP
AN Example


US Postal Service has web service to
output all ZIP Codes
In order to include ZIP Codes in your
ASP.NET application you simply poll the
USPS Web Service
Web Service
Web Service
HTTP Request
Client PC
SOAPCapable
Browser
HTTP Response
SOAP Message
Using XML Syntax
Web
Service
Webserver
-Interface
Properties
Methods
Similarities to ASP

ASP pages coexist with ASP.NET


Cannot share session information
Still have Response and Request
objects

All info requested with a web page
Form data
 Query Strings


All info returned with a webpage
Components of .NET

3 major parts

Common language runtime (CLR)




A virtual machine that runs all .NET programs
Run-time language is Microsoft Intermediate
language (MSIL)
Uses JIT Compilation
Common type system (CTS)
 Standard set of types




Value types (int, char…)
Reference types for objects
Common naming convention (CLS)
Class Library


Predefined code packaged as classes
All .NET classes or part of System namespace
Class Libraries

Namespaces



Hierarchical naming convention
Matches class inheritance tree
Common Namespaces in .NET






System root namspace
System.data
System.xml
System.net
System.security
System.web
Writing C# code in .NET

Object oriented

Class


Object


Public int Gasoline { get {return Gas; } set {Gas=value;}}
Overloading


private void Drive() {}
Property


private int Gas;
Method


Car c = new Car():
Member variable


public class Car { }
Method with different parameter list
Inheritance

The power of OOP
Getting Started with .NET

Installing .NET

Need .NET SDK
Loaded with Visual Studio .NET
 Available under MSDNAA


Need IIS


Microsoft Data Access Components
(MDAC)


Personal edition or Server
For data base access
Service packs and updates
.NET Framework SDK

Assemblies



Metadata



ASP.NET pages
Executed in CLR
Data about data
Provides location of class definitions. How to
load classes and any defines attributes
Global Assembly Cache

Centralized locations for assemblies
Working with Dreamweaver

Chapter 3 of text provides a good
overview

Only issue is that it assumes that you
have a web server available on your
Desktop..we do in N-105. You may not
have on your home computer
Setting up Dreamweaver MX to
Create ASP.NET Applications



Open Dreamweaver MX.
Select Site > New Site… from the Menu bar to create a
new site.
On the Basic tab, enter the following information,
clicking Next at the bottom of each screen:


Name: MyASPNET
Yes, I want to use a server technology.






Select ASP.NET C# as the server technology.
Edit and then upload to remote testing server, storing
your files at H:\asp.
URL to browse to the root of your site
http://perleybrook.umfk.maine.edu/classes/cos381/”last
name”.
No, do not copy files when I am done editing.
Click Done to create an initial cache of your site.
Lets do this “Step by Step”
Web Form Controls

Learn by doing

Create new ASP.NET C# page
Insert new Form Element
Set to Run at Server
Add Label Control
Web Form


Save and test previous example
Go to “View Source”
Building Dynamic Pages



Update page based on external
conditions
runat=“server” means that a control
can be manipulated
Let’s modify our page to display an
user input


ADD asp:textbox
ADD asp:button
Then we add a Page Load Function
Change Function text
About Post Back

Form “post’ back to themselves



isPostBack is true when


No need to reload form
Makes the form interactive
Something control has caused the form
to be to be sent to the server and the
form is “reloaded”
Problem is that you allways return
to the same form page
Form vs Query Srting

In CGI programming we used the Query
String to collect data from a form and
pass it to our script



Uses method=“get”
Security/performance issue
In ASP.Net


We use the action=“post” and read our values
from the response object
 Response.Form[“txtFirstName”]
We use get when we want to load a different
page and pass data to the page

<a ref=“http://url/webform.aspx?data=stuff” >stuff</a>
Using Request.Form
Add Text
Examples ASP.NET

All examples from class are at


http://perleybrook.umfk.maine.edu/samples/A
SPdotNet/script.htm
Today’s Example (so far)

WebForm1 (code)
Moving between pages

Since Web Form Post Back ….how
do you go to another web Page?


If you don’t want to finish
processing the page you are on


Response.Redirect(“Url”);
Response.Redirect(“Url”, true);
The hidden form element
__VIEWSTATE gets lost
Session Variables

To move data between different Web
forms you can use Session Variables



To Set a Session Variable (original Web Form
 Session[“aVariable”] = “some data”;
To get a Session Variable (second Web Form)
 lblSomeLable.Text =
Session[“aVariable”].ToString();
 String str = Session[“aVariable”].ToString();
Example (code1) (Code2)
Working With Drop-Down Lists

To add a Drop Down use
Dreamweaver’s wizard
Drop Down List

Coding Drop Downs

In Body section
<asp:dropdownlist ID="dblSampleList"
runat="server">
<asp:listitem>List Item 1</asp:listitem>
</asp:dropdownlist>

To add a item dynamically
dblSampleList.Items.Add("List Item 2");
dblSampleList.Items.Add("List Item 3");

Example (code)
Binding Data

Used for binding data from a
Database or an array to a controls
at run time




Another way to add list items
Bind values to variables
Two Styles for Binding Data exist
Textbook Error

References to BindData() should be
change to DataBind()
Binding Data

First Style

<%# variable %>
In body of HTML
 Type in or use ASP.NET Bound Data Icon
in Dreamweaver
 Must call Page.DataBind(); in Page_load
function (note error in text book!)
 Will be replaced by value of variable


Second Style
Binding Data

Second Style

In Page_Load Function
someList.DataSource = someList;
 Page.DataBind();


Example (code)
Event Handling in ASP.NET

Events are messages sent out by
objects


Different objects – Different messages
In ASP.NET events occur when the
server detects an event

page-Level events


Occur when users visits a web page
Control events

Occur when a user manipulates an
ASP.NET control
Page Events

Web Form trigger 11 events when loaded (in
order)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
Initialization (Page_Init)
Load View State
Post Back Data Processing
Page Load (Page_Load)
Post Back Change Notification
Post Back Event Processing
Pre-rendering (Page_PreRender)
Same view State
Rendering
Disposing
Unload
Page Level Events


These events do not need to be registered
Most common Used Page Event is
Page_Load
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
if (!PostBack) DataBind();
}
</script>

Example (code)
Control Events

Must have registered Handlers


Use Delegates which listen for events
and then delegating responsibility for
the event to the assigned handler
Generally event handlers (like
Page_Load) have two parameters

Object src


The object which created the event
EventArgs E

Specific information about the event
Creating a handler for a ASP.NET
Control


ASP.NET Control must be in a Web
Form that is set to runat=‘server”
Use Dreamweaver Wizard
Control Events

The Dreamweaver wizard will register the
event but you still need to write the
handler code

Between <script> tags in Head
void btnTest_OnClick( Object Src, EventsArgs
E)
{
Some C# code
}
Example
(Code)
Adding Controls and Events
Programmatically

Between <script> tags in head






Button btnTest = new Button();
btnTest.Text = “daButton”;
btnTest.Click += new
EventHandler(this.btnTest_OnClick);
samePanel.Controls.Add(“btnTest”);
Make sure you write the
btnTest_Onclick functions
Example (code)
Complex Controls


Have lots of functionality;
sometimes called rich controls
We will look at two complex controls



Ad Rotator
File upload
You can create your own Complex
Controls (chap 10 & 11 in text)
AD Rotator

Places Different images in your Web
Form every time someone loads the
page. Images are licked to a web
site

3 Parts
Images
 The ASP.NET Ad Rotator Control
 A XML configuration file


Example (Code)
XML config File

<?xml version="1.0" encoding="iso-8859-1" ?>
<Advertisements>
<Ad>
<ImageUrl>*.jpg</ImageUrl>
<NavigateUrl>URL</NavigateUrl>
<AlternateText>text</AlternateText>
<Impressions>50</Impressions>
<Keyword>Macromedia</Keyword>
</Ad>
<Ad>
<ImageUrl>*.jpg</ImageUrl>
<NavigateUrl>UrL</NavigateUrl>
<AlternateText>Text</AlternateText>
<Impressions>30</Impressions>
<Keyword>Microsoft</Keyword>
</Ad>
</Advertisements>
example
Adding The Ad Rotator Control

Use Dreamweaver wizard


More tags icon
Under ASP.NET tags use asp:adrotatoor
Web Form Validation

Input fields need to be checked



Check for errors before using the data in a
program or putting into a database
We can check for
 Existence
 Format
 Range of vales
 Data type
ASP.NET includes validation controls that
automatically provide form field validation
with little or no extra coding
Preprogrammed Validators

Does both client side and server side
validation of data


DHTML
 CSS
 JavaScript
ASP.NET includes 5 preprogrammed
Validators





Required field
Range
Compare
Regular expression
Custom
Adding a Required field validator

Must use a Web Form that
runat=‘server”
Adding a Validation summary

This is a place for all the validation
errors to appear
Example (code)
Value Based Validators

Required Text Validators only require that
some text is entered but you may want to
check to actual text entered

Compare against
 Value
(compare validator)
 Type
(compare validator)
 Another control (compare validator)
 A regular expression (regular expression
validator)
 A range of values (range validator)
 Other conditions (custom validator)
Adding value Validators
Regular Expression

Same as JavaScript and Perl










\w is alpha char
\d is decimal char
* zero or more
+ one or more
? Zero or one
. Matches any character except newline
[] range of characters
[A-Z] upper case
\* matches *
\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)

Matches e-mails
Validations examples

Compare, Range & Regular
expression


Custom validator


Example (code)
Example (code)
Assignment #7

http://perleybrook.umfk.maine.edu/samples/assign7/UMFKorder.aspx