Title: Targeted Applications
1Targeted Applications
- NET Framework to develop the following types of
applications and services - Console applications.
- Windows applications (Windows Forms).
- Web applications applications (ASP.NET).
- Mobile web applications (Mobile Web Form
ASP.NET) - XML Web services (can be consumed by Window
applications and Web applications). - Windows services.
2ASP.NET Features
- Multiple Language Support
- Run ASP and ASPX side by side
- Simplified programming model
- Server Controls
- Event based programming (VB for the web)
- Increased Performance
- Compiled code
- Caching Cache output
- Web Services
- Improved Security
- Greater Scalability
- Cookie-less Sessions
- Classes and Namespaces
- Simplified Configuration and Deployment
3Hello.aspx
- lthtmlgt
- ltscript language"VB" runat"server"gt
- Sub EnterBtn_Click(Src As Object, e As
EventArgs) - Message.Text "Hi " Name.Text ",
welcome to ASP.NET!" - End Sub
- lt/scriptgt
- ltbodygt
- ltform runat"server"gt
- Enter your name ltasptextbox id"Name"
runatserver/gt - ltaspbutton text"Enter" Onclick"EnterBtn_Cli
ck" runat"server"/gt - ltpgt
- ltasplabel id"Message" runatserver/gt
- lt/formgt
- lt/bodygt
- lt/htmlgt
Design time
Run time
4Web Server Controls
ltaspbutton text"Enter" Onclick"EnterBtn_Click"
runat"server" /gt
- lthtmlgt
- ltscript language"VB" runat"server"gt
- Sub EnterBtn_Click(Src As Object, e As
EventArgs) - Message.Text "Hi " Name.Text _
- ", welcome to ASP.NET!"
- End Sub
- lt/scriptgt
- ltbodygt
- ltform runat"server"gt
- Enter your name
- ltasptextbox id"Name" runatserver/gt
- ltaspbutton text"Enter" Onclick"EnterBtn_Click"
runat"server"/gt - ltasplabel id"Message" runatserver/gt
- lt/formgtlt/bodygtlt/htmlgt
Use id attribute to assign a unique identifier to
each control in order to refer to it in your
program.
5ASP.NET Pages Part Declarative, Part Code
- Combines declarative tags (HTML, ASPX directives,
server controls tags, and static text) with code
in a single file or in separate files. - Unlike ASP, good separation provided between code
and tags
?
single file
separate files (code-behind)
code
code
lttagsgt
lttagsgt
Form1.aspx
Form1.aspx
Form1.aspx.vb
6Web.config
- lt?xml version"1.0" encoding"utf-8" ?gt
- ltconfigurationgt
- ltsystem.webgt
- ltcompilation defaultLanguage"vb"
debug"true" /gt - lt!-- CUSTOM ERROR MESSAGES
- Set customErrors mode"On" or
"RemoteOnly" to enable custom error messages,
"Off" to disable. - Add lterrorgt tags for each of the errors
you want to handle. - --gt
- ltcustomErrors mode"Off"/gt
- ltauthentication mode"Windows" /gt
- ltauthorizationgt
- ltallow users"" /gt lt!-- Allow all users
--gt - lt/authorizationgt
- lttrace enabled"false" requestLimit"10"
pageOutput"false" traceMode"SortByTime"
localOnly"true" /gt - ltsessionState
- mode"InProc"
- stateConnectionString"tcpip127.0.0.1
42424" - sqlConnectionString"data
source127.0.0.1user idsapassword" - cookieless"false"
A Web.config file is required in your web
application folder, a subfolder under the
application folder does not require a Web.config
file.
7Runtime Compilation
8ASP.NET Execution Model First Request
Client Server
9Page/Control Event Execution
PostBack
First Request
Page DLL is loaded, control hierarchy initialized
They may be triggered on PostBack
Page_Load
Textbox1_Changed
1. Change Events
Button1_Click
2. Action Events
Control hierarchy (Dynamically generated HTML
page) is rendered
Page_Unload
Page is disposed
10Postback
- ltform id"Form1" method"post" runat"server"gt
- Enter your nameltbrgt
- ltasptextbox id"TextBox1" runat"server"gtlt/aspte
xtboxgt - ltPgtltaspbutton id"Button1" runat"server"
Text"Submit"gtlt/aspbuttongtlt/Pgt - ltPgtltasplabel id"Label1" runat"server"gtlt/asplab
elgtlt/Pgt - ltPgt ltaspLabel id"Label2" runat"server"gtlt/aspLa
belgt lt/Pgt - lt/formgt
Postback
When an ASP.NET page's ltformgt tag does not have
the action attribute, the "Submit" button, submit
the form variables (controls' data) to the same
page to be processed by its server-side
event-handling methods.
11Handling Postback Forms
- ViewState Control maintains the state of a page
during Postback - Page_Load fires on every request
- Test Page.IsPostBack to execute conditional
logic
Sub Page_Load(s As Object, e As EventArgs) If
Not Page.IsPostBack Then ' executes only on
initial page load End If ' The rest of code
executes on ' Post-Back every request End Sub
12Effect of Post Back
Design View
Hello2.aspx
lt_at_ Page Language"vb" AutoEventWireup"false"
Codebehind"Hello2.aspx.vb" Inherits"WebApplicati
on1.Hello2" gt .
13Hello2.aspx
- lt_at_ Page Language"vb" AutoEventWireup"false"
- Codebehind"Hello2.aspx.vb" Inherits"WebApplicat
ion1.Hello2"gt - lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt - ltHTMLgt
- ltHEADgt
- lttitlegtHello2lt/titlegt
- ltmeta content"Microsoft Visual Studio.NET 7.0"
name"GENERATOR"gt - ltmeta content"Visual Basic 7.0"
name"CODE_LANGUAGE"gt - ltmeta content"JavaScript" name"vs_defaultClien
tScript"gt - ltmeta content"http//schemas.microsoft.com/inte
llisense/ie5" name"vs_targetSchema"gt - lt/HEADgt
- ltbodygt
- ltform id"Form1" method"post" runat"server"gt
- Enter your nameltbrgt
- ltasptextbox id"TextBox1" runat"server"gtlt/aspte
xtboxgt - ltPgtltaspbutton id"Button1" runat"server"
Text"Submit"gtlt/aspbuttongtlt/Pgt - ltPgtltasplabel id"Label1" runat"server"gtlt/asplab
elgtlt/Pgt - ltPgt ltaspLabel id"Label2" runat"server"gtlt/aspLa
belgt lt/Pgt - lt/formgt
14Hello2.aspx.vb
- Public Class Hello2
- Inherits System.Web.UI.Page
- Protected WithEvents Button1 As
System.Web.UI.WebControls.Button - Protected WithEvents TextBox1 As
System.Web.UI.WebControls.TextBox - Protected WithEvents Label2 As
System.Web.UI.WebControls.Label - Protected WithEvents Label1 As
System.Web.UI.WebControls.Label -
- Private Sub Page_Load(ByVal sender As
System.Object, _ - ByVal e As System.EventArgs) Handles
MyBase.Load - ' Put user code to initialize the page
here - If Not Page.IsPostBack Then
- Label2.Text "First Time"
- Else
- Label2.Text "Post back"
- End If
- End Sub
- Private Sub Button1_Click(ByVal sender As
System.Object, _ - ByVal e As System.EventArgs) Handles
Button1.Click - Label1.Text TextBox1.Text
15Control Event Processing
- Events are
- Triggered on the client by the user
- Handled in server code
- Requires a postback to the same page
- ViewState of controls saves page and control
properties between round trips, therefore helps
restore control to its previous state. - Implemented as a hidden form field
- Disable via setting the EnableViewState attribute
- EnableViewStatefalse
- Data Binding resets control state
16The Files in a Web Application
http//localhost/hello/HelloForm3.aspx
Request
Response
17WebForm1.aspx
18Converting String to Number and Formatting
- ltHTMLgtltHEADgtlttitlegtWebForm1lt/titlegt
- ltscript language"vb" runat"server"gt
- Sub EnterBtn_Click(Src As Object, e As EventArgs)
- Dim DollarInput as String
- Dim DollarConverted as Double
- Try
- DollarInput Request.Params.Get("amount")
- DollarConverted CType(DollarInput,
Double) - Response.Write("The amount you entered
is" DollarConverted.ToString("C")) - Catch ex as Exception
- Response.Write("You did not enter a
proper dollar amount!") - End Try
- End Sub
- lt/scriptgt
- lt/HEADgt
- ltbodygt
- ltform id"Form1" method"post"
runat"server" gt - ltPgtEnter the amount ltINPUT
type"text" name"amount" size"20"gtlt/Pgt - ltINPUT type"submit" value"Submit"