AspPart1

acceptableseashoreSecurity

Nov 5, 2013 (4 years and 9 days ago)

115 views

ASP.NET

Part 1

Instructor
: Charles Moen

CSCI/CINF 4230

2

ASP.NET


Microsoft technology for building dynamic,
interactive websites


Part of the
Microsoft .NET Framework


Server
-
side technology

that must be used on a
Microsoft server


It’s used on some of the biggest websites

-

Microsoft.com


-

Costco.com

-

Dell.com




-

Monster.com

-

MySpace.com


-

Match.com

ASP.NET (Walther, Wikipedia, Ding, Microsoft)


3

ASP.NET History


Named ASP+ while still in beta


ASP.NET 1.0 was released by Microsoft in January 2002


Its name is similar to an earlier Microsoft technology for
dynamically
-
created Web pages that is called ASP (for
Active Server Pages), but they are incompatible and are
very different


The early version is sometimes called “Classic ASP”


ASP.NET 3.5


Current version


Released in November 2007

ASP.NET (Wikipedia)


4

.NET Framework


A marketing term that Microsoft uses for a collection
of technologies


Languages, such as C# and VB


The .NET class library


ASP.NET


the engine for hosting dynamic web pages


ADO.NET


the technology for connecting to database


Visual Studio

ASP.NET (MacDonald)


5

.NET Framework


Framework Class Library


More than 13,000 classes


All the classes are divided into namespaces


The classes are stored in .dll files, which are called “
assemblies
,”
e.g. System.Web.dll


Common Language Runtime


Executes your application code


A compiler converts the source code into
MSIL

(Microsoft
Intermediate Language), which is platform
-
independent


The JITTER (Just
-
In
-
Time compiler) converts the MSIL code into
machine code when the application executes

ASP.NET (Walther)


6

Development Environments


IDE


Visual Web page development, drag
-
and
-
drop controls, code
completion, integrated debugging, automatic compiling, more...


Microsoft Visual Studio 2008



Microsoft Visual Web Developer 2008 Express Edition


Free


www.microsoft.com/express/vwd


Servers


IIS


ASP.NET Development Server run by the IDE


OS


Windows XP Professional


Windows Vista

ASP.NET (Wikipedia, Walther, Ding)


7

Server
-
side Processing

ASP.NET

(MacDonald)


IIS

is the
server

http://dcm.uhcl.edu/moen/welcome.
aspx
?Charles+Moen


1.
Server looks at the
extension

of the requested file

2.
If it is html


The server locates the file


Then returns it to the user agent inside an HTTP
response

3.
If it is
aspx


The server finds the program that can process it


Sends the request to the ASP.NET engine in
aspnet_isapi.dll


ASP.NET processes the page in the
aspx

file


The server gets the HTML from ASP.NET; returns it
to the user agent inside an HTTP response



ASP.NET

8

File Types

.aspx


ASP.NET web pages

.aspx.cs


Code
-
behind files that contain C# code


Separates application logic from the structure and presentation of the web page

.ascx


User controls that you’ve developed

.asmx


Web services


methods that can be called over the Internet

web.config


XML configuration file

Global.asax


Global application file


define global variables and event handlers



ASP.NET

(MacDonald)


9

.aspx

File

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>


<!DOCTYPE html PUBLIC "
-
//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1
-
transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">


<title>Example</title>

</head>

<body>


<form id="form1" runat="server">


<div>


<asp:Label ID="Label1" runat="server" Text="Enter your name:"></asp:Label>


<br />


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>


<br />


<asp:Button ID="Button1" runat="server" Text="Button" />


</div>


</form>

</body>

</html>

ASP.NET

(MacDonald)



A
.
aspx

page is referred to as a
Web Form


Most
.
aspx

pages have a form element


Different from an HTML form because it uses ASP.NET controls

10

Page Directive

ASP.NET

(MacDonald)



Always the first line


Tells ASP.NET basic information about how to compile the page


Language used for the code


The way that event handlers are connected


If you’re using code
-
behind, it specifies the code
-
behind file and the name of the class in the code
-
behind file

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>


<!DOCTYPE html PUBLIC "
-
//W
3
C//DTD XHTML
1.0
Transitional//EN"
"http://www.w
3
.org/TR/xhtml
1
/DTD/xhtml
1
-
transitional.dtd">


<html xmlns="http://www.w
3
.org/
1999
/xhtml">

<head runat="server">


<title>Example</title>

</head>

<body>


<form id="form
1
" runat="server">


<div>


<asp:Label ID="Label
1
" runat="server" Text="Enter your name:"></asp:Label>


<br />


<asp:TextBox ID="TextBox
1
" runat="server"></asp:TextBox>


<br />


<asp:Button ID="Button
1
" runat="server" Text="Button" />


</div>


</form>

</body>

</html>

11

DOCTYPE

ASP.NET

(MacDonald)



Follows the page directive


Defaults to transitional

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>


<!DOCTYPE html PUBLIC "
-
//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1
-
transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">


<title>Example</title>

</head>

<body>


<form id="form1" runat="server">


<div>


<asp:Label ID="Label1" runat="server" Text="Enter your name:"></asp:Label>


<br />


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>


<br />


<asp:Button ID="Button1" runat="server" Text="Button" />


</div>


</form>

</body>

</html>

12

HTML Elements

ASP.NET

(MacDonald)



HTML elements


html, head, body


Form element


Included by default


C
ontains

a nest
ed

div element by default

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>


<!DOCTYPE html PUBLIC "
-
//W
3
C//DTD XHTML
1.0
Transitional//EN"
"http://www.w
3
.org/TR/xhtml
1
/DTD/xhtml
1
-
transitional.dtd">


<html xmlns="http://www.w
3
.org/
1999
/xhtml">

<head runat="server">


<title>Example</title>

</head>

<body>


<form id="form
1
" runat="server">


<div>


<asp:Label ID="Label
1
" runat="server" Text="Enter your name:"></asp:Label>


<br />


<asp:TextBox ID="TextBox
1
" runat="server"></asp:TextBox>


<br />


<asp:Button ID="Button
1
" runat="server" Text="Button" />



</div>


</form>

</body>

</html>

13

ASP.NET Web Controls

ASP.NET

(MacDonald)



Ordinary XHTML elements are mixed with ASP.NET web controls


Indentified by the asp: namespace


When the page is processed in the ASP.NET engine, the ASP.NET web controls are rendered as
XHTML markup


Page sent to the client is always standard XHTML, and may include CSS and JavaScript

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>


<!DOCTYPE html PUBLIC "
-
//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1
-
transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">


<title>Example</title>

</head>

<body>


<form id="form1" runat="server">


<div>


<asp:Label ID="Label1" runat="server" Text="Enter your name:"></asp:Label>


<br />


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>


<br />


<asp:Button ID="Button1" runat="server" Text="Button" />



</div>


</form>

</body>

</html>

14

ASP.NET Controls


HTML server controls


Equivalent to standard HTML elements


Processed on the server side


Web controls


ASP.NET versions of standard controls


buttons, input fields, labels


Validation controls


required field, range, regular expression


Data controls


for working with data in a database


Navigation controls


menus, tree views, bread crumb trails


Rich controls


calendar, file upload, wizards, banner ads


Login controls

ASP.NET (Walther, MacDonald)


15

HTML Server Controls


Equivalent to standard HTML elements


Processed on the server side


Each control has a class that is defined in
System.Web.UI.HtmlControls (MacDonald, p.
138
)


Advantages


They retain their state


They fire server
-
side events, which your code can respond to


Easy to convert from an existing page


Add

runat="server"

ASP.NET (Walther, MacDonald)


16

Converting from HTML to ASP.NET

ASP.NET

(MacDonald)





<!DOCTYPE html PUBLIC "
-
//W
3
C//DTD XHTML
1.0
Transitional//EN"
"http://www.w
3
.org/TR/xhtml
1
/DTD/xhtml
1
-
transitional.dtd">


<html xmlns="http://www.w
3
.org/
1999
/xhtml">

<head>


<title>Currency Converter</title>

</head>

<body>


<form method="post" action="#">


<div>


Convert: &nbsp;


<input type="text" id="US" />


&nbsp; U.S. dollars to Euros.




<br /><br />


<input type="submit" value="OK" id="Convert" />



<br /><br />


<div style="font
-
weight:bold;" id="Result"></div>


</div>


</form>

</body>

</html>

17

Converting from HTML to ASP.NET

ASP.NET

(MacDonald)


<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="CurrencyConverter.aspx.cs" Inherits="CurrencyConverter" %>


<!DOCTYPE html PUBLIC "
-
//W
3
C//DTD XHTML
1.0
Transitional//EN"
"http://www.w
3
.org/TR/xhtml
1
/DTD/xhtml
1
-
transitional.dtd">


<html xmlns="http://www.w
3
.org/
1999
/xhtml">

<head>


<title>Currency Converter</title>

</head>

<body>


<form
runat="server"
>


<div>


Convert: &nbsp;


<input type="text" id="US"
runat="server"

/>


&nbsp; U.S. dollars to &nbsp;




<br /><br />


<input type="submit" value="OK" id="Convert"
runat="server"

/>



<br /><br />


<div style="font
-
weight:bold;" id="Result"
runat="server"
></div>


</div>


</form>

</body>

</html>

Add the page
directive

Add
runat=server

for every
element that we
want to use on
the server side

Now the controls
retain their state

18

Converting from HTML to ASP.NET

ASP.NET

(MacDonald)


<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="CurrencyConverter.aspx.cs" Inherits="CurrencyConverter" %>


<!DOCTYPE html PUBLIC "
-
//W
3
C//DTD XHTML
1.0
Transitional//EN"
"http://www.w
3
.org/TR/xhtml
1
/DTD/xhtml
1
-
transitional.dtd">


<html xmlns="http://www.w
3
.org/
1999
/xhtml">

<head>


<title>Currency Converter</title>

</head>

<body>


<form runat="server">


<div>


Convert: &nbsp;


<input type="text" id="US" runat="server" />


&nbsp; U.S. dollars to &nbsp;




<br /><br />


<input type="submit" value="OK" id="Convert" runat="server" />


OnServerClick="Convert_ServerClick"

/>


<br /><br />


<div style="font
-
weight:bold;" id="Result" runat="server"></div>


</div>


</form>

</body>

</html>

OnServerClick

identifies a click
that is processed
on the server side

To add
functionality, we
need to add an
event handler

HTML controls
can respond to
two events on the
server side

ServerClick

ServerChange


19

Converting from HTML to ASP.NET

ASP.NET

(MacDonald)


using System;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


public partial class CurrencyConverter : System.Web.UI.Page

{


protected void
Convert_ServerClick
(object sender, EventArgs e)


{


decimal USAmount = Decimal.Parse(US.Value);


decimal EuroAmount = USAmount *
0.85
M;


Result.InnerText = USAmount.ToString() + " U.S. dollars = ";


Result.InnerText += EuroAmount.ToString() + " Euros.";


}

}

Using statements give access to the
namespaces for the .NET classes

The event handler is defined in the code
-
behind file.

It is connected to the event by the OnServerClick attribute in the tag for the submit
button.

Every page is a class that inherits from
System.Web.UI.Page
, and the event handlers
are always methods of this class

All event handlers
have these two
arguments

20

C# Variables


Variable names are
case
-
sensitive


Common data types

(see MacDonald, p.
23
)

C#


byte


int


double


decimal


char


string


bool


Object

.NET type name


Byte


Int
32


Double


Decimal


Char


String


Boolean


Object

ASP.NET

(MacDonald)


int error =
404
;

System.String greeting = "Hello";

21

Type Conversions


Special characters can be appended to numeric literals to identify
their data type

M (decimal)

D (double)

F (float)

L (long)

ASP.NET

(MacDonald)


decimal price =
2.98
M;

2.98
would normally be interpreted
as a double


A number can be converted to a string by the
ToString

method

string bargain = "$" + price.ToString();


A string can be converted to a number, but it should be done in a try
block

string strCount = "10";

int count = Int32.Parse(strCount);

22

Converting from HTML to ASP.NET

ASP.NET

(MacDonald)


using System;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


public partial class CurrencyConverter : System.Web.UI.Page

{


protected void
Convert_ServerClick
(object sender, EventArgs e)


{


decimal USAmount;


bool success =
Decimal.TryParse(US.Value, out amount)
;


if (success)


{


decimal EuroAmount = USAmount *
0.85
M;


Result.InnerText = USAmount.ToString() + " U.S. dollars = ";


Result.InnerText += EuroAmount.ToString() + " Euros.";


}


else


{


Result.InnerText = "Please enter only a number.";


}


}

}

Testing whether
the user entered a
number

23

Converting from HTML to ASP.NET

ASP.NET

(MacDonald)


<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="CurrencyConverter.aspx.cs" Inherits="CurrencyConverter" %>


<!DOCTYPE html PUBLIC "
-
//W
3
C//DTD XHTML
1.0
Transitional//EN"
"http://www.w
3
.org/TR/xhtml
1
/DTD/xhtml
1
-
transitional.dtd">


<html xmlns="http://www.w
3
.org/
1999
/xhtml">

<head>


<title>Currency Converter</title>

</head>

<body>


<form runat="server">


<div>


Convert: &nbsp;


<input type="text" id="US" runat="server" />


&nbsp; U.S. dollars to &nbsp;


<select id="Currency" runat="server" />


<br /><br />


<input type="submit" value="OK" id="Convert" runat="server"


OnServerClick="Convert_ServerClick" />


<br /><br />


<div style="font
-
weight:bold;" id="Result" runat="server"></div>


</div>


</form>

</body>

</html>

Add a select
control for different
types of currency

24

Converting from HTML to ASP.NET

ASP.NET

(MacDonald)


public partial class CurrencyConverter : System.Web.UI.Page

{


protected void Convert_ServerClick(object sender, EventArgs e)


{


decimal USAmount;


bool success = Decimal.TryParse(US.Value, out amount);


if (success)


{


ListItem item = Currency.Items[Currency.SelectedIndex];



decimal newAmount = USAmount * Decimal.Parse(item.Value);


Result.InnerText = USAmount.ToString() + " U.S. dollars = ";


Result.InnerText += newAmount.ToString() + " " + item.Text;


}


else


{


Result.InnerText = "Please enter only a number.";


}


}



protected void
Page_Load
(object sender, EventArgs e)


{


if (this.IsPostBack == false)


{


Currency.Items.Add(new ListItem("Euros", "
0.85
"));


Currency.Items.Add(new ListItem("Japanese Yen", "
110.33
"));


Currency.Items.Add(new ListItem("Canadian Dollars", "
1.2
"));


}


}

}

Get the value of
the selected option

Initialize the select
control, but only
on the first page
load event

25

References

Ding, Wei, “ASP.NET” UHCL lecture slides,
2008
.

MacDonald
,
Matthew
,
Beginning ASP.NET
3.5
in C#
2008
: From Novice to Professional,
Second Edition
. Apress,
2007
.

Walther, Stephen.
ASP.NET
3.5
Unleashed
. SAMS,
2008
.

Wikipedia. “Active Server Pages”. [Online].
Available:http://en.wikipedia.org/wiki/Active_Server_Pages

Wikipedia. “ASP.NET”. [Online]. Available:http://en.wikipedia.org/wiki/ASP.NET