You are on page 1of 47

Crating Web Forms Applications

By
Ms. Madhura Kutre
Web Technologies
• HTTP / HTTPS (URL, GET/POST)
• Client-side:
– HTML / XHTML (Extensible HyperText Markup Language)
– JavaScript / VBScript (client-side scripting)
– Applets / ActiveX controls
• Server-side:
– PHP
– JSP (Java Server Pages)
– ASP (Active Server Pages)
– ASP.NET (next generation of ASP)
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 programming languages supported
by .NET
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
– 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 client-side
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"
Internet Technologies
WWW Architecture

PC/Mac/Unix
Client + Browser (IE, FireFox)

Request:
http://www.msn.com/default.html

HTTP
Network TCP/IP

Response:
<html>…</html>

Server Web Server


ASP Architecture

PC/Mac/Unix
Client + Browser (IE, FireFox)

Request:
http://www.msn.com/default.aspx

HTTP
Network TCP/IP

Response:
<html>…</html>

Server IIS
(Internet Information Server)
Server-Side Code
• What is server-side code?
– Software that runs on the server, not the client
– Receives input from
• URL parameters
• HTML form data
– Can access server-side databases, e-mail servers,
files, etc.
– Dynamically builds a custom HTML response
for a client(depends upon client request)
ASP.NET Overview and Features
ASP.NET provides services to allow the creation,
deployment, and execution of
Web Applications and Web Services
Web Applications are built using Web Forms
Web Forms are designed to make building
web-based applications as easy as building Visual Basic
applications
Built on .NET Framework: any .NET programming
language can be used (C#, Visual Basic)
Complete object model
Separation of code and UI
Maintains page state
Session management
Caching, Debugging, Extensibility
WebTime.aspx Example
Creating an ASP.NET Web Application using Visual Studio
Step 1: Creating the Web Application Project
• Select File > New Web Site... and choose ASP.NET Empty Web Site
in the Templates pane.
• Select File System from the drop-down list closest to Location.
• Set the Language to Visual C#, and click OK.
• Different files with different file name
extensions
– Standard ASP.NET files: .aspx or .ascx
– Web Services: .asmx
– Code (behind) files: .aspx.cs, .asmx.vb, ...
– Configuration: Web.Config
– Web applications: Global.asax and Global.asax.vb
ASP.NET File Types
ASP.NET applications can include many types of files.

Ends with .aspx


These are ASP.NET web pages (the .NET equivalent of the .asp file in an ASP
application). They contain the user interface and, optionally, the underlying
application code. Users request or navigate directly to one of these pages to
start your web application.

Ends with .ascx


These are ASP.NET user controls. User controls are similar to web pages,
except that the user can’t access these files directly. Instead, they must be
hosted inside an ASP.NET web page. User controls allow you to develop a
small piece of user interface and reuse it in as many web forms as you want
without repetitive code.

Ends with .asmx


These are ASP.NET web services—collections of methods that can be called
over the Internet. Web services work differently than web pages, but they still
share the same application resources, configuration settings, and memory.
web.config
This is the XML-based configuration file for your ASP.NET application. It
includes settings for customizing security, state management, memory
management, and much more.

Global.asax
This is the global application file. You can use this file to define global
variables (variables that can be accessed from any web page in the web
application) and react to global events (such as when a web application
first starts).

Ends with .cs


These are code-behind files that contain C# code. They allow you to separate
the application logic from the user interface of a web page.
WebTime.aspx Example
• Add n ASPX file (i.e., Web Form), default
named Default.aspx is created for each new
project.
• Visual Web Developer creates a code-behind
file named Default.aspx.cs.
• The View Designer button opens the Web Form in
Design mode.

• The Copy Web Site button allows you to copy the


project’s files to another location, such as a
remote web server.
• Finally, the ASP.NET Configuration button takes you
WebTime.aspx Example

• Let’s create our first ASP.NET page using Visual


Studio
1. Modify title of the page
2. Add a heading <h2>
3. Look at the page in Design and Split modes
4. Add a Label control from the Toolbox
5. Change ID of the Label control
6. Change some physical properties of the Label
control
7. Go to WebTime.aspx.cs file and add Page_Init
function to set Text property of the Label control
Visual Studio generates the markup shown when
you create the GUI. WebTime.aspx ( 1 of 2 )
1 <%-- WebTime.aspx --%> ASP.NET comments
2 <%-- A page that displays the current time in a Label. --%> begin with <%-- and
3 <%@ Page Language="C#" AutoEventWireup="true" terminate with --%>,
CodeFile="WebTime.aspx.cs" and can span multiple
4 Inherits="WebTime" EnableSessionState="False" %> lines.
5
6 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" The Page directive
7 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> specifies information
needed by ASP.NET
8
to process this file.
9 <html xmlns="http://www.w3.org/1999/xhtml">
10 <head runat="server"> The document type
11 <title>A Simple Web Form Example</title> declaration, which specifies
12 </head> the document element name
13 <body> and the PUBLIC URI for the
14 <form id="form1" runat="server"> DTD that defines the
15 <div> XHTML vocabulary.
16 <h2>Current time on the web server:</h2>
XHTML documents
The form that contains our The body contains have the root element
XHTML text and controls is set the main content html and markup
to execute on the server, which that the browser information about the
generates equivalent XHTML. displays. document in the
head element. 20
ASPX file that displays the web server’s time.
WebTime.aspx

17 <p> ( 2 of 2 )

18 <asp:Label ID="timeLabel" runat="server" BackColor="Black"


19 Font-Size="XX-Large" ForeColor="Yellow"
20 EnableViewState="False"></asp:Label>
21 </p> Markup for a label
The asp: tag prefix
22 </div> web control.
indicates that the
23 </form> label is an ASP.NET
web control, not an
24 </body>
XHTML element.
25 </html>

• In an ASPX file a directive is delimited by <%@ and %>.

ASPX file that displays the web server’s time. (Part 2 of 2. )


The code-behind file (WebTime.aspx.cs)

1 // WebTime.aspx.cs
2 // Code-behind file for a page that displays the current time.
3 using System;
4
5 public partial class WebTime : System.Web.UI.Page The Page_Init
6 { method handles the
7 // initializes the contents of the page page’s Init event,
8 protected void Page_Init( object sender, EventArgs e ) which indicates that
9 { the page is ready to
10 // display the server's current time in timeLabel be initialized.
11 timeLabel.Text = DateTime.Now.ToString( "hh:mm:ss" );
12 } // end method Page_Init Retrieve the current
13 } // end class WebTime time and formats it
as hh:mm:ss.

Code-behind file for a page that displays


the web server’s time. (Part 1 of 2.)
WebTime.aspx Example Run

• The Page_Init method handles the page’s 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.
• The code-behind file contains a partial class.
• 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 page—it 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 page—no recompilation is
necessary.
Features of .NET
• Rich Functionality out of the box:
It contains hundreds of classes that provide variety of
functionality ready to use in your applications.
• Easy development of web applications:
ASP.NET is a technology available on .NET platform
for developing dynamic and data driven web applications.
ASP.NET provides an event driven programming model with
complex user interface.
• OOPs Support:
.NET provides a fully object oriented environment.

• Multi-Language Support:
.Net Supports multiple languages. Such as Visual
Basic.Net, C#, Jscript.Net and managed C++.
• Multi-Device Support:

• Automatic memory management:


The garbage collector takes care of freeing unused
objects at appropriate intervals.

• Security:
Microsoft has taken great efforts to make .NET
platform safe and secure for enterprise applications.
Features such as type safety, code access security
and role based authentication make overall
application more robust and secure.
.NET Framework Architecture
VB C++ C# JScript …

Common Language Specification

Visual Studio.NET
ASP.NET: Web Services Windows
and Web Forms Forms

ADO.NET: Data and XML

Base Classes

Common Language Runtime


The Common Language Runtime (CLR):
• The CLR is described as the "execution engine" of .NET.

• It's this CLR that manages the execution of programs and provides
core services, such as code compilation, memory allocation,
thread management, and garbage collection.

• .NET applications are compiled to a common language known as


Microsoft Intermediate Language or “IL”.

• The CLR, then, handles the compiling the IL to machine


language, at which point the program is executed.

• This MSIL is turned into executable code using a JIT (Just In


Time) complier.
CLR: Execution Model
The .NET Class Framework:
• .NET Class Framework also referred as .NET base class
library. The .NET Class Framework consists of several
thousand type definitions, where each type exposes some
functionality.
• The class library consists of lots of prewritten code that all the
applications created in VB .NET and Visual Studio .NET will
use.
• The Microsoft .NET Framework class library provides a rich
library for creating applications that run on the platform (or
execution environment) known as the common language
runtime (CLR).
• Tthe CLR and the .NET Class Framework allow developers to
build the following kinds of applications:
• Web Services. Components that can be accessed over the
Internet very easily.
• Web Forms. HTML based applications (Web Sites).
• Windows Forms.
• Windows Console Applications. Compilers, utilities and tools
are typically implemented as console applications.
ADO.NET: Data and XML:
Definition: ADO.NET is a collection of classes,
interfaces, structures, and enumerated types that manage
data access from within the .NET Framework.

User Interface:
The next layer consists of the user and programming
interface that allows .NET to interact with the outside
world. The following are the types of interaction
interfaces that are supported by the .NET framework:
• Web Forms
• Windows Forms
• Web Services
Now let me tell you about Windows Forms and
ASP.NET. WinForms (Windows Forms) is simply the
name used to describe the creation of a standard Win32
kind of GUI applications.
Common Language Specification:
These are nothing but guidelines that all .NET languages
should follow.
• It defines a type subset of all .NET aware
languages
– To make language interoperability possible
– Required to be supported by all .NET aware
languages
– Such code is called CLS compliant
All the public methods of a class should be CLS compliant to
make language interoperability possible
Common Type System:
It is core part of CLR that takes care of cross language
integration.
It supports two general categories of types:
1. Value types
2. Reference Types

The common type system performs the following functions:


Establishes a framework that helps enable cross-
language integration, type safety, and high performance
code execution.
Provides an object-oriented model that supports the
complete implementation of many programming
languages.
Defines rules that languages must follow, which helps
ensure that objects written in different languages can
interact with each other.
.NET compliant Language:
The language which follows common language specification.
.NET framework contains multiple languages:
1. FORTRAN for .NET
2. Smalltalk for .NET
3. Perl for .NET
4. C
5. C++
6. J#
7. C#
8. Visual Basic
.NET 2.0
• .NET Framework 2.0 provides the core
architectural base for all subsequent versions of the
Framework. The following technologies are
included in the .NET Framework 2.0:
– Common language runtime (CLR) and base class
libraries.
– Support for generic types and methods.
– Compilers for C#, Visual Basic, C++, and J#.
– ADO.NET.
– ASP.NET.
– Windows Forms.
– Web services.
.NET 3.0 & 3.5

• The following technologies are introduced in the


.NET Framework 3.0:
– Windows Presentation Foundation (WPF).
– Windows Communications Foundation (WCF).
– Windows Workflow Foundation (WF).

• The following technologies are introduced in the


.NET Framework 3.5:
– Language Integrated Query (LINQ).
– New compilers for C#, Visual Basic, and C++.
– ASP.NET AJAX.
Some ASP.NET namespaces
System Defines fundamental data types eg
system.string
System.Collections Definitions and classes for creating
various collections
System.IO File reading & writing operations
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 Architecture
HTTP Request

HTTP Response

Client
Web Server

Internet Information
HTTP Request In HTTP Response Out
Server

.ASPX .HTML

DATABASE
ASP.NET

CLR

.NET Framework
ProcessRequest
ASP.NET Page Lifecycle
DeterminePostBackMode

PerformPreInit OnInitComplete OnLoadComplete

OnPreInit Load PreRender (recursive)

ApplyMasterPage
(recursive) LoadControlState CreateChildControls

LoadViewState SaveAllState

ProcessPostData SaveControlState

RaiseChangedEvents SaveViewState

RaisePostbackEvent Render (recursive)


State Management

Client side State Management


• View state
• Cookies
• Query String
Server side State Management
• Session State
• Application State
View State

This is the hidden field which will store information about the
controls in the encrypted format . This will help the server with
the information about the controls across the post back.
ViewState[“info"] = “any information";
Reading and Writing View State

// Write the price of an item to view state


ViewState["Price"] = price;

// Read the price back following a postback


decimal price = (decimal) ViewState["Price"];
COOKIES
Cookies can be defined as small amount of memory used by
Web Server on the client machine .
This is used to maintain personal information of the client.
That is user account , No Of visits ,Last visit Date so on.
Cookies are classified into 2 types
1)In Memory Cookies
2)Persistence Cookies
Creating a Cookie
HttpCookie cookie = new HttpCookie ("UserName", "Jeffpro");
Response.Cookies.Add (cookie);

Cookie name

Cookie value
Reading a Cookie
HttpCookie cookie = Request.Cookies["UserName"];
if (cookie != null) {
string username = cookie.Value; // "Jeffpro"
...
}

1)The persistent cookie information will be maintained within a text file.


C:/documents and settings/administartor/cookie/
Query Strings
•to pass information using a query string in the URL.
This approach is commonly found in search engines.
• For example, if you perform a search on the
Google website, you’ll be redirected to a new URL that
incorporates your search parameters.
•query string example-
http://www.contoso.com/listwidgets.aspx?
category=basic&price=100
In the URL path above, the query string starts with a
question mark (?) and includes two attribute/value
pairs, one called "category" and the other called "price."

You might also like