Active Server Pages
Active Server Pages
NET
Active Server Pages
Web Applications
A browser displays pages defined using HTML tags (Hypertext, Markup Language)
Web Applications
Web Applications
Dynamic Pages
The same page may be posted back to the server for processing.
Nevertheless, the page itself is stateless, i.e., it will not maintain the value
of a variable between each loading of the page.
Server-Side Code
URL parameters
HTTP request
(form data,
HTTP
header data)
HTTP response
HTML, XML
ASP page
(static HTML,
server-side
logic)
Built on .NET Framework: any .NET programming language can be used (C#,
Visual Basic)
Session management
WebTime.aspx Example
Creating an ASP.NET Web Application using VS
Select File > New Web Site... and choose ASP.NET Empty Web Site in the
Templates pane.
WebTime.aspx Example
When the project loads for the first time, the Web Forms Designer displays
the autogenerated ASPX file in Source mode.
Design mode indicates the XHTML element where the cursor is currently
located.
You can also view both the markup and the web-page design at the same time
by using Split mode
WebTime.aspx Example
2.
3.
4.
5.
6.
7.
WebTime.aspx Example
Changing the Title of the Page
We change the pages title from the default Untitled Page to A Simple Web Form
Example.
Open the ASPX file in Source mode and modify the text between the <title> tags.
Alternatively, you can modify the Web Forms Title property in the Properties window.
To view the Web Forms properties, select DOCUMENT from the drop-down list in the
Properties window.
To add controls to the page, you can drag and drop them from the Toolbox onto the
Web Form in Design mode.
Like the Web Form itself, each control is an object that has properties, methods and
events.
You can type text directly on a Web Form at the cursor location or insert XHTML
elements using menu commands.
Right click the ASPX file in the Solution Explorer and select Rename.
Enter the new file name WebTime.aspx and press Enter. Both the ASPX file
and the code-behind file are updated.
Renaming the Class in the Code-Behind File and Updating the ASPX File
Right click the class name in the partial classs declaration and select
Refactor > Rename to open the Rename dialog.
WebTime.aspx Example
Examining an ASPX File
The Inherits attribute specifies the class in the code-behind file from which
this ASP.NET class inherits.
WebTime.aspx Example
The document type declaration, which specifies the document element name
and the PUBLIC URI for the DTD that defines the XHTML vocabulary.
XHTML documents have the root element html and markup information about
the document in the head element.
Setting the runat attribute to "server" indicates that ASP.NET processes the
element and its nested elements and generates the corresponding XHTML.
The body contains the main content that the browser displays.
The form that contains our XHTML text and controls is set to execute on the
server, which generates equivalent XHTML.
WebTime.aspx Example
The asp: tag prefix indicates that the label is an ASP.NET web control, not an
XHTML element.
WebTime.aspx Example
This control is processed on the server so that the server can translate the
control into XHTML.
If this is not supported, the asp:Label element is written as text to the client.
The Page_Init method handles the pages Init event, which indicates that the
page is ready to be initialized.
WebTime.aspx Example
> Relationship Between an ASPX File and a Code Behind File
The code-behind file inherits from Page, which defines the general
functionality of a web page.
ASP.NET generates another partial class that defines the remainder of that
class, based on the markup in the ASPX file.
The first time the web page is requested, this class is compiled, and an
instance is created.
This instance represents our pageit creates the XHTML that is sent to the
client.
Once an instance of the web page has been created, multiple clients can use
it to access the pageno recompilation is necessary.
WebTime.aspx Example
> How the Code in an ASP.NET Web Page Executes
When an instance of the page is created, the PreInit event occurs first,
invoking method Page_PreInit, which can be used to set a pages theme.
The Init event occurs next, invoking method Page_Init, which is used to
initialize objects and other aspects of the page.
Next, the Load event occurs, and the Page_Load event handler executes.
The page then processes any events that are generated by the pages
controls.
Once a response has been generated and sent, an Unload event occurs,
which calls Page_Unload, which typically releases resources used by the
page.
Further reading: https://msdn.microsoft.com/en-us/library/ms178472.aspx
WebTime.aspx Example
To view the XHTML generated by ASP.NET, select View Source from the Page
menu in Internet Explorer (or View > Page Source if you are using Firefox).
Nonvisual form components, called hidden inputs, store data that the user
doesnt need to see.
Attribute method of the form element specifies the request method (usually
get or post). The action attribute identifies the resource that will be
requested when a form is submitted.
WebTime.aspx Example
When the form is processed on the server, the runat attribute is removed.
Only those elements marked in the ASPX file with runat=server are
modified or replaced in the generated XHTML.
WebTime.aspx Example
The positions of controls and other elements are relative to the Web Forms
upper-left corner. This type of layout is known as relative positioning.
You can select Debug > Start Without Debugging, which runs the application by
opening it in a browser window.
To debug your application, you can select Debug > Start Debugging. You cannot
debug a web application unless debugging is explicitly enabled by the web.config
file.
To view a specific ASPX file, you can right click either the Web Forms Designer or
the ASPX file name and select View In Browser.
Finally, you can run your application by opening a browser window and typing the
web pages URL in the Address field.
Event Handling
When the user interacts with a GUI component, the event drives the program
to perform a task.
2.
To create this click event handler, double click the Button on the Form.
3.
4.
Set the Text property of the Label control with the current time in this function.
HelloWorld Example
<%-- Hello World page that also displays the current time. --%>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HelloWorld.aspx.cs"
Inherits="HelloWorldPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Hello World Web Form</title>
</head>
<body>
Event Handling
An object reference named sender a reference to the object that generated the
event.
Clicking the Events icon (the lightning-bolt icon) in the Properties window,
displays all the events for the selected control.
Button code
Button
...
List code
List
...
Label code
Label
Browser
...
ASP.NET
Event handlers
ASP.NET Architecture
VB
C++
C#
JScript
Windows
Forms
Visual Studio.NET
User code executes on the web server in page or control event handlers
Server Controls
HTML Controls
Web Controls
Richer functionality
More consistent object model // the properties are changed in the consistent way loke
background
Server Controls
> HTML Controls
Table.bgcolor=red;
Derived from
System.Web.UI.HtmlControls.HtmlControl
Server Controls
> HTML Controls
Supported controls
<a>
<img>
<form>
<table>
<tr>
<td>
<th>
<select>
<textarea>
<button>
<input type=text>
<input type=file
<input type=submit>
<input type=button>
<input type=reset>
<input type=hidden>
Server Controls
> HTML Controls
Demo 1
HTMLControls1.aspx
Demo 2
HTMLControls2.aspx
Server Controls
> HTML Controls
Event code will read the values of other controls (e.g. text, check boxes, radio buttons,
select lists)
Server Controls
> Web Controls
Label1.BackColor = Color.Red;
Table.BackColor = Color.Blue;
Richer functionality
AutoPostBack is the method by which the page will be posted back to the server
automatically based on some events in the web controls
Server Controls
> Web Controls
Server Controls
> Web Controls
Web Controls provide extensive properties to control display and format, e.g.
Font
BackColor, ForeColor
Style, CssClass
Height, Width
Visible, Enabled
Server Controls
> Web Controls
Intrinsic controls
List controls
Rich controls
Validation controls
Server Controls
> Intrinsic Controls
Supported controls
<asp:button>
<asp:label>
<asp:imagebutton>
<asp:panel>
<asp:linkbutton>
<asp:table>
<asp:hyperlink>
<asp:textbox>
<asp:checkbox>
<asp:radiobutton>
<asp:image>
Server Controls
> Intrinsic Controls
Server Controls
> List Controls
Supported controls
<asp:dropdownlist>
<asp:repeater>
<asp:listbox>
<asp:datalist>
<asp:radiobuttonlist>
<asp:datagrid>
<asp:checkboxlist>
Server Controls
> CheckBoxList & RadioButtonList
Server Controls
> Intrinsic & Simple List Controls
Demo 1: WebControls1.aspx
Demo 2: WebControls2.aspx