MWT_WWD_C2_S15_S8_20151231183444562

advertisement
Introduction to ASP.NET
What is ASP.NET and how is different from ASP
– ASP: server side technology for creating
dynamic web pages using scripting languages eg
vb script.
– ASP.NET: server side technology for creating
dynamic web pages using Fully Fledged
programming languages supported by .NET
What is .NET?
•
•
A Microsoft strategy and new technology for delivering software
services to the desktop and to the web
Components include:
– MS Intermediate Language; all code is complied into a more abstract,
–
–
–
–
•
•
•
trimmed version before execution. All .NET languages are compiled to
MSIL – the common language of .NET
The CLR- common language runtime; responsible for executing MSIL
code; interfaces to Windows and IIS
A rich set of libraries (Framework Class Libraries) available to all .NET
languages
The .NET languages such as C#, VB.NET etc that conform to CLR
ASP.NET is how the Framework is exposed to the web, using IIS to
manage simple pages of code so that they can be complied into full .NET
programs. These generate HTML for the browser.
Built on open protocols (XML, SOAP)
Future for development of MS & non-MS based systems.
Also heading towards the “Internet Operating System”
Common Language
Runtime Type System
Compilers use the runtime type system to produce type
compatible components
Components
C#
VB
C++
Common Type System
Compilers
Runtime Environment
.NET Execution Model
VB
Native
Code
VC
...
Script
IL
Common Language Runtime
Standard JIT
Compiler
Native
Code
Common Language Runtime
• Lightweight Just-in-time compiler:
– MSIL to Native machine language; Can be ported to numerous platforms
• The compiled code is transformed into an intermediate language
called the Microsoft Intermediate Language (MSIL or IL)
• An integer in Visual Basic .NET or an int in C# are converted to
the same .NET data type, which is Int32
• The IL that is created is the same for all languages
• The assembly is the compiled .NET program
• The assembly contains the IL along with additional information
called metadata
• Metadata contains information about the assembly
• Use the IL Disassembler (ildasm.exe) to view the IL within an
assembly
Framework Overview
VB
C++
C#
JScript
…
Common Language Specification
Win Forms
Data and XML
Base Class Library
Common Language Runtime
Visual Studio.NET
Web Forms
(ASP.NET)
Namespace
• The base class libraries are organized into logical
groupings of code called namespaces
• A namespace is a hierarchical way to identify
resources in .NET
• The System object is at the top of the namespace
hierarchy, and all objects inherit from it
– ASP.NET: System.Web namespace
– WebForms: System.Web.UI namespace
– HTML Server Controls:
System.Web.UI.Control.HTMLControl
– ASP.NET Server Controls:
System.Web.UI.Control.WebControl
Importing Namespaces
• Visual Studio .NET adds references to your projects’
commonly used namespaces by default
• You can import the namespaces into your page using
the @Import directive
• The following is the syntax for importing a .NET
namespace
<%@ Import NamespaceName %>
• Below is a sample of how you would import the
ASP.NET Page class
<%@ Imports System.Web.UI.Page %>
Some ASP.NET namespaces
System
Defines fundamental data types eg
system.string
System.Collections
Definitions and classes for creating
various collections
File reading & writing operations
System.IO
System.Web
Support browser/server
communication
System.Web.UI
Creates the Page object whenever
an .aspx page is requested
System.Web.UI.web Classes and definitions to create
controls
server controls
ASP.NET
• The latest version of ASP is known as ASP.NET
• Visual Studio .NET is a developer application used
to create ASP.NET Web applications
• There are two main types of Web resources created
with ASP.NET applications
– WebForms are ASP.NET pages within an ASP.NET
application
– Web Services are ASP.NET Web pages that contain
publicly exposed code so that other applications can
interact with them
– Web Services are identified with the file extension .asmx
WebForms
• The ASP.NET WebForm is separated into two
logical areas:
– The HTML template
– A collection of code behind the WebForm
• The HTML template
–
–
–
–
Contains the design layout, content, and the controls
Creates the user interface, or presentation layer
Instructs the browser how to format the Web page
Is created using a combination of HTML controls,
HTML Server controls, Mobile Controls, and
ASP.NET controls
Server Controls
• HTML Server controls are similar to the HTML
controls, except they are processed by the server
• Add runat = "server" to the HTML control to
transform it into an HTML Server control
• HTML control: <input type="text">
• HTML Server control:
<input type="text" runat="server"/>
<input type=”radio” runat=”server” value=”Yes”/> Yes
• Server-side programs can interact with the control
before it is rendered as a plain HTML control and
sent to the browser
ASP.NET Controls
• ASP.NET form controls will create the HTML code
• ASP.NET Server controls are organized as:
–
–
–
–
ASP.NET Form Controls
Data Validation Controls
User Controls
Mobile Controls
• ASP.NET controls are usually identified with the
prefix asp: followed by the name of the control
• ASP.NET button:
<asp:Button id="ShowBtn" runat="server"
Text="Show the message." />
HTML Server Vs
ASP.NET Server, Controls
• ASP.NET form controls can interact with clientside events such as when the user clicks on a
button
– When the event occurs, ASP.NET can trigger a script to
run on the server
• ASP.NET form controls also have different
properties than their HTML server control
counterparts
– HTML Server label control
• Message1.InnerHTML = "Product 1"
– ASP server label control
• Message2.Text = "Product 2"
User Controls
• User controls are external files that can be
included within another WebForm
• User controls allow you to reuse code across
multiple files
• For example, you can create a user control that
displays the a navigation bar
• You can use this control on the home page; they
are often used for creating self-contained code,
headers, menus, and footers
• User controls replace the functionality of ASP
server-side include pages
• They are identified with the file extension .asmx
Other ASP.NET Server Controls
• Data validation controls
– A series of controls that validate form data without extensive
JavaScript programming
• Mobile controls
– A series of controls that provide form functionality within
wireless and mobile devices
• Literal controls
– Page content that is not assigned to a specific HTML control
such as a combination of HTML tags and text to the browser
Server Controls within
Visual Studio .NET
• In Visual Studio
.NET most of the
ASP.NET Server
controls are located
on the Web Forms
tab in the toolbox
Server controls with Visual Studio.NET
The Code Behind
• Server programs are written in a separate file
known as the code behind the page
• By separating the programming logic and
presentation layer, the application becomes
easier to maintain
• Only Server controls can interact with the code
behind the page
– Written in any ASP.NET compatible language such
as Visual Basic .NET, C#, Perl, or Java
– Filename is the same as the WebForm filename
– Add a file extension that identifies the language
• Visual Basic .NET use .vb (mypage.aspx.vb)
• C# use .cs (mypage.aspx.cs)
Code Behind file
• The location of the code behind the page is determined
via a property that is set on the first line in the page
using the @Page directive
<%@ Page Language="vb" Codebehind="WebForm1.vb"
Inherits=“MyFirstApp.WebForm1"%>
• The @Page directive allows you to set the default
properties for the entire page such as the default
language
• The CodeBehind property identifies the path and
filename of the code behind file
• The Inherits property indicates that the code behind the
page inherits the page class
• This page class contains the compiled code for this
page
Compiling the Page Class
• The compiled code behind the page is the class
definition for the page
– A class is a named logical grouping of code
– The class definition contains the functions, methods, and
properties that belong to that class
• In Visual Studio .NET the process of compiling a
class is called building
– When you build the application, you compile the code
into an executable file
– Visual Studio .NET compiles the code behind the page
into an executable file and places the file in the bin
directory
Page Class Events
• The Page Class consists of a variety of methods,
functions, and properties that can be accessed
within the code behind the page
• The first time a page is requested by a client, a
series of page events occurs
• The first page event is the Page_Init event
which initializes the page control hierarchy
• The Page_Load event loads any server controls
into memory and occurs every time the page is
executed
Page class events
• Page_init
• Page_load
•
Server_Controls
•
Page_prerender
•
Page_Unload
How ASP.NET works
• When .NET is installed, IIS is configured to
look for files with the .aspx extension and to
use the ASP.NET module (aspnet_isapi.dll)
to handle them.
• ASP.NET parses the .aspx file and arranges
it in a predefined class definition and
generates an asp.net page object.
• The page object generates html that is sent
back to IIS and then the browser.
• NOTE: only .aspx files are parsed (if it is
pure html don’t save it as an aspx file as it
will slow down the server.
ASP.NET samples
• Page directives: <%@ page language = “VB”
debug="true" trace="true“ %>
• <script language = “VB” runat=“server”> VB.NET
code declarations ……….. </script>
• Message.aspx
<html>
<head> <title>Inserting ASP.NET code Example</title> </head>
<body>
Line1: First HTML Line<br />
Line2: Second HTML Line<br />
Line3: Third HTML Line<br />
</body>
</html>
• Note this has no asp code so better to use .html extension
• Message2.aspx
<script language="VB" runat="server">
Sub Page_Load()
Response.Write ("First ASP.NET Line<br />")
Response.Write ("Second ASP.NET Line<br />")
Response.Write ("Third ASP.NET Line<br />")
End Sub
</script>
<html> <head> <title>Inserting ASP.NET code Example</TITLE> </head>
<body>
Line1: First HTML Line<br />
Line2: Second HTML Line<br />
Line3: Third HTML Line<br />
</body>
</html>
• Message3.aspx
html>
<head><title>Inserting ASP.NET code Example</title></head>
<body>
Line1: First HTML Line<br />
Line2: Second HTML Line<br />
Line3: Third HTML Line<br />
<script language="VB" runat="server">
Sub Page_Load()
Response.Write ("First ASP.NET Line<br />")
Response.Write ("Second ASP.NET Line<br />")
Response.Write ("Third ASP.NET Line<br />")
End Sub
</script>
</body>
</html>
Render or inline code block –
interweave1.aspx
<html>
<head>
<title>Interweaving ASP.NET code and HTML Example</title>
</head>
<body>
Line1: First HTML Line<br />
<% Response.Write ("First ASP.NET Line<br />") %>
Line2: Second HTML Line<br />
<% Response.Write ("Second ASP.NET Line<br />") %>
Line3: Third HTML Line<br />
<% Response.Write ("Third ASP.NET Line<br />") %>
</body>
</html>
NOT RECOMMENDED.
Interweave2.aspx A Server control
script language="VB" runat="server">
Sub Page_Load()
Message.Text="The ASP.NET line"
End Sub
</script>
<html>
<head> <title>Inserting ASP.NET code Example</TITLE> </head>
<body>
First HTML Line<br/>
<asp:label id=Message runat="server"/> <br />
Second HTML Line<br/>
</body>
</html>
Download