26
CIS 451: Introduction to ASP.NET Dr. Ralph D. Westfall January, 2009

CIS 451: Introduction to ASP.NET

Embed Size (px)

Citation preview

Page 1: CIS 451: Introduction to ASP.NET

CIS 451:Introduction to ASP.NET

Dr. Ralph D. WestfallJanuary, 2009

Page 2: CIS 451: Introduction to ASP.NET

Web Development Problem

HTML designed to display static pages only interactive when user clicks links

can’t provide animation except animated GIFs

can’t create custom pages for specific types of customers

can’t use databases, spreadsheets, etc. no security capabilities in HTML itself

except hiding passwords <input type="password" etc.

using long directory/file names works somewhat, but it's not HTML

Page 3: CIS 451: Introduction to ASP.NET

Solution 1: Client Side Scripts JavaScript (dominant product) can do

animation, rollovers, message boxes calculations, validation of user inputs incorporate customized content into pages

e.g. based on day/time, user inputs, etc. provide limited access to user’s system

system date, browser, plug-in information set cookies (data for server for next visit)

Page 4: CIS 451: Introduction to ASP.NET

Client Side Scripts (continued)

what (client side) JavaScript can’t do connect with databases, and possibly

other applications on the server provide meaningful security for

access control (but JavaScript Source has some "hackish" scripts)

hiding proprietary code if it's in the HTML file or user has a tool to find it (e.g., Web Developer add-on for Firefox)

Page 5: CIS 451: Introduction to ASP.NET

Solution 2: Server Side Scripts use common gateway interface (CGI) offer database & security capabilities written in C, C++, Perl, etc.

relatively difficult to program & maintain use server resources relatively

inefficiently separate programs in addition to

HTML but this is probably an advantage

Page 6: CIS 451: Introduction to ASP.NET

Solution 3: ASP Active Server Pages released by Microsoft in 1996 works with Microsoft servers

Windows Server 2008 free Visual Web Developer Express 2008

IDE includes a lightweight personal web server for developing ASP.NET applications

so does Visual Studio

Page 7: CIS 451: Introduction to ASP.NET

Solution 3: ASP (continued) simple language can go in same files as HTML code provides many of the capabilities of

CGI uses server resources more efficiently

than CGI using ASP server extensions, can work

with non-Microsoft servers, but …

Page 8: CIS 451: Introduction to ASP.NET

ASP.NET major revision of ASP

can use different languages, not just VB.NET

VB.NET code looks more like Java than the VBScript used previously

.NET code is compiled into MSIL (Microsoft Intermediate Language)

MSIL is then interpreted into machine language that runs on the server

Page 9: CIS 451: Introduction to ASP.NET

ASP.NET: Many Languages HTML (of course!) Visual Basic.NET can also use C# and J# with VWDX .NET supports over 30 other

languages C++ and JScript (Microsoft's version of

JavaScript) work with Visual Studio.NET other (non Microsoft) languages can

also work with Visual Studio.NET

Page 10: CIS 451: Introduction to ASP.NET

Tools To Develop & Use ASP.NET

a tool to create code text editors: Notepad, TextPad, etc. HTML authoring tools: Dreamweaver,

FrontPage, etc. Visual Studio.NET, Web Matrix or

Visual Web Developer Express 2008 (free!)

IIS or VWDX on your computer or free 14-day ASP.NET hosting

web browser

Page 11: CIS 451: Introduction to ASP.NET

Creating an ASP.NET Page create an HTML document add ASP code in appropriate places save with extension of .aspx if don't have local server on computer,

upload to a remote ASP.NET server Cal Poly Intranet server won't run ASP

view in browser (file can be on local machine with server or remote server)

Page 12: CIS 451: Introduction to ASP.NET

Simple ASP.NET Page (not Visual Studio) create a document with HTML code add .NET code within HTML (inline)

at top, need <%@ Page Language="VB" %>

save with extension of .aspx upload to remote server (if don't

have Web Matrix or IIS) or paste into VS

view in browser

Page 13: CIS 451: Introduction to ASP.NET

hello.aspx<%@ Page Language="VB" %><html> <body> <!-- paste below </form> tag--

> <% Response.Write("<P>Hello, world!") %> <P>The time is: <% = Now() %> </body></html> <!-- View Source in browser -->

Page 14: CIS 451: Introduction to ASP.NET

ASP.NET Coding within HTML use <% at start, %> at end on same line (inline) <% = Time %> or on multiple lines (block)

<%

Response.Write "<P>Hello, world!"

Response.Write 'like VB.NET Debug.Write %>

Page 15: CIS 451: Introduction to ASP.NET

ASP.NET Output in HTML ASP inside a HTML tag

<P>The time is: <% = Time %> use <% = followed by a variable or a

function that returns data, then %> variable can come from server itself, or

from client machine, or can be defined in code elsewhere in same or another ASP file

<% Dim strCar as String="Geo" %> <P>The car is: <% = strCar %>

Page 16: CIS 451: Introduction to ASP.NET

Generating ASP Output - 2 ASP.NET code can write text and

also data from ASP.NET variables hit counter example <% nHits = nHits + 1 Response.Write "Hits = " & nHits

'text variable %>

Page 17: CIS 451: Introduction to ASP.NET

Generating ASP Output - 3 can mix text, HTML, and ASP.NET

variables in code this includes using ASP to write HTML

tags, in addition to writing variables and text

Page 18: CIS 451: Introduction to ASP.NET

fontsize.aspx <% Dim intI as Integer For intI = 1 to 7 %> <!-HTML inside ASP loop-> <font size = <% = intI %> > Size is <% = intI %> </font><br/> <% Next %> <!-note-> <!-what will you see from this?-> <!-fontcolor.aspx 'notes->

Page 19: CIS 451: Introduction to ASP.NET

Using ASP.NET with Forms use HTML or ASP page to create a form

be sure all form fields have id = "[ ]" and also name = "[ ]" 'same as for id=

use an ASP.NET page to process inputs action attribute in form has name of an

ASP.NET file, and use Input (Submit) button form field values accessible to ASP as Request.QueryString("[form field name]")

Page 20: CIS 451: Introduction to ASP.NET

form2.html

<html><body> <form method="get" action="resp2.aspx">

Last Name <input type="text"

name="custName" id="custName">

</body></html>

<!--need to refresh?-->

Page 21: CIS 451: Introduction to ASP.NET

Default.aspx <%@ Page Language="VB" %> <html><body> Input from form: <% Dim strCustName as String

strCustName = _ Request.QueryString("custName")

Response.Write("last name is " & _ strCustName)

%></body></html>

Page 22: CIS 451: Introduction to ASP.NET

Using ASP.NET with Forms - 2 if have more than just one text field, must

have a Submit button on form <input type=submit [etc.]>

after development would use <form method="post" [etc.]>

when using post, also need to change every Request.QueryString to Request.Form in the ASP page identified by the action

Page 23: CIS 451: Introduction to ASP.NET

Using Visual Studio.NET for ASP using Visual Studio.NET or Visual

Web Developer Express 2008 Create: Web Site… ASP.NET Web Site add a Label to Default.aspx designer

and expand it vertically and horizontally to occupy 25% of the screen

double click the screen (outside of the label) to start a Page_Load Sub

paste code on following page into Sub

Page 24: CIS 451: Introduction to ASP.NET

VB.NET Web CodeDim i As IntegerDim colr() As String = {"000000", "333300",

_ "666600", "999900", "cccc00", "ffff00"}Dim output As String = ""For i = 0 To 5 output &= "<h1><font color=#" & colr(i)

_ & ">" & colr(i) & "</font></h1>"NextLabel1.Text = output

Page 25: CIS 451: Introduction to ASP.NET

Run Code click Start (first time) modify the web.config…

>OK view code and then make changes

as necessary

Page 26: CIS 451: Introduction to ASP.NET

ASP.NET Code Samples CIS 451 ASP Code Samples W3 Schools online tutorials and

demonstration code