Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a...

47
Web Application Development Introduction

Transcript of Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a...

Page 1: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Web Application Development Introduction

Page 2: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Lesson: Creating Web Forms

What is a Web Form?

Creating a Web Form with Visual Studio .NET

Page 3: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

What Is a Web Form?

<%@ Page Language="vb" Codebehind="WebForm1.aspx.vb" SmartNavigation="true"%>

<html><body ms_positioning="GridLayout"> <form id="Form1" method="post" runat="server"> </form></body>

</html>

<%@ Page Language="vb" Codebehind="WebForm1.aspx.vb" SmartNavigation="true"%>

<html><body ms_positioning="GridLayout"> <form id="Form1" method="post" runat="server"> </form></body>

</html>

.aspx extension Page attributes

@ Page directive Body attributes Form attributes

Page 4: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Creating a Web Form with Visual Studio .NET

New ASP.NET Web Applications create a default Web Form: WebForm1.aspx

Create additional Web Forms from the Solution Explorer

Page 5: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Lesson: Using Server Controls

What is a Server Control?

Types of Server Controls

Saving View State

Demonstration: Converting HTML Controls to Server Controls

HTML Server Controls

Web Server Controls

Practice: Identifying the HTML Generated by Web Server Controls

Selecting the Appropriate Control

Demonstration: Adding Server Controls to a Web Form

Page 6: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

What is a Server Control?

Runat="server"

Events happen on the server

View state saved

Have built-in functionality

Common object model

All have Id and Text attributes

Create browser-specific HTML

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

Page 7: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Types of Server Controls

HTML server controls

Web server controls

Intrinsic controls

Validation controls

Rich controls

List-bound controls

Internet Explorer Web controls

Page 8: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Saving View State

Hidden ViewState control of name-value pairs stored in the Web Form

On by default, adjustable at Web Form and control level

<%@ Page EnableViewState="False" %>

<asp:ListBox id="ListName" EnableViewState="true" runat="server">

</asp:ListBox>

<%@ Page EnableViewState="False" %>

<asp:ListBox id="ListName" EnableViewState="true" runat="server">

</asp:ListBox>

<input type="hidden" name="__VIEWSTATE" value="dDwtMTA4MzE0MjEwNTs7Pg==" />

<input type="hidden" name="__VIEWSTATE" value="dDwtMTA4MzE0MjEwNTs7Pg==" />

Page 9: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

HTML Server Controls

Based on HTML elements

Exist within the System.Web.UI.HtmlControls namespace

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

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

Page 10: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Web Server Controls

Exist within the System.Web.UI.WebControls namespace

Control syntax

HTML that is generated by the control

<asp:TextBox id="TextBox1"runat="server">Text_to_Display</asp:TextBox>

<asp:TextBox id="TextBox1"runat="server">Text_to_Display</asp:TextBox>

<input name="TextBox1" type="text" value="Text_to_Display"Id="TextBox1"/>

<input name="TextBox1" type="text" value="Text_to_Display"Id="TextBox1"/>

Page 11: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

You need specific functionality such as a calendar or ad rotator

The control will interact with client and server script

You are writing a page that might be used by a variety of browsers

You are working with existing HTML pages and want to quickly add ASP.NET Web page functionality

You prefer a Visual Basic-like programming model

You prefer an HTML-like object model

Use Web Server Controls if:Use Web Server Controls if:Use HTML Server Controls if:Use HTML Server Controls if:

Bandwidth is not a problemBandwidth is limited

Selecting the Appropriate Control

Page 12: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Lesson: Using Code-Behind Pages

How to Implement Code

What are Code-Behind Pages?

Page 13: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

How to Implement Code

Three methods for adding code:

Put code in the same file as content (mixed)

Put code in a separate section of the content file (inline code)

Put code in a separate file (code-behind pages)

Code-behind pages are the Visual Studio .NET default

Page 14: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

What are Code-Behind Pages?

Separation of code from content

Developers and UI designers can work independently

Form1.aspxForm1.aspx Form1.aspxForm1.aspx Form1.aspx.vbForm1.aspx.vbor Form1.aspx.csor Form1.aspx.cs

<tags><tags> codecode

codecode

Separate filesSingle file

Page 15: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Lesson: Adding Event Procedures to Web Server Controls

What are Event Procedures?

Client-Side Event Procedures

Server-Side Event Procedures

Creating Event Procedures

Interacting with Controls in Event Procedures

Page 16: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

What are Event Procedures?

Action in response to a user’s interaction with the controls on the page

Page 17: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Client-Side Event Procedures

Internet .HTM Pages

Typically, used only with HTML controls only

Interpreted by the browser and run on the client

Does not have access to server resources Uses <SCRIPT language="language">

Page 18: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Server-Side Event Procedures

Used with both Web and HTML server controls

Code is compiled and run on the server

Have access to server resources

Use <SCRIPT language="vb" runat="server"> or <SCRIPT language=“cs" runat="server">

Internet.ASPX Pages

Page 19: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Creating Event Procedures

Visual Studio .NET declares variables and creates an event procedure template

Using the Handles keyword adds many event procedures to one event

protected System.Web.UI.WebControls.Button cmd1;private void InitializeComponent(){ this.cmd1.Click += new System.EventHandler(this.cmd1_Click); this.Load += new System.EventHandler(this.Page_Load);} private void cmd1_Click(object s, System.EventArgs e)

protected System.Web.UI.WebControls.Button cmd1;private void InitializeComponent(){ this.cmd1.Click += new System.EventHandler(this.cmd1_Click); this.Load += new System.EventHandler(this.Page_Load);} private void cmd1_Click(object s, System.EventArgs e)

Protected WithEvents cmd1 As System.Web.UI.WebControls.ButtonPrivate Sub cmd1_Click(ByVal s As System.Object, _

ByVal e As System.EventArgs) Handles cmd1.Click

Protected WithEvents cmd1 As System.Web.UI.WebControls.ButtonPrivate Sub cmd1_Click(ByVal s As System.Object, _

ByVal e As System.EventArgs) Handles cmd1.Click

Page 20: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Interacting with Controls in Event Procedures

Read the properties of Web server controls

Output responses to other Web server controls

lblGreeting.Text = "new text"lblGreeting.Text = "new text"

strGreeting = "Hello " & txtName.TextstrGreeting = "Hello " & txtName.Text

strGreeting = "Hello " + txtName.Text;strGreeting = "Hello " + txtName.Text;

lblGreeting.Text = "new text";lblGreeting.Text = "new text";

Page 21: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Lesson: Using Page Events

Understanding the Page Event Life Cycle

Handling Page.IsPostback Events

Page 22: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Understanding the Page Event Life Cycle

Page_LoadPage_Load

Page_UnloadPage_Unload

Textbox1_ChangedTextbox1_Changed

Button1_ClickButton1_Click

Page is disposed

Page_InitPage_Init

Control eventsControl events

Change EventsChange Events

Action EventsAction Events

Page 23: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Handling Page.IsPostback Events

Page_Load fires on every request

Use Page.IsPostBack to execute conditional logic

Page.IsPostBack prevents reloading for each postback

Private Sub Page_Load(ByVal s As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then 'executes only on initial page load End If 'this code executes on every requestEnd Sub

Private Sub Page_Load(ByVal s As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then 'executes only on initial page load End If 'this code executes on every requestEnd Sub

private void Page_Load(object sender, System.EventArgs e){ if (!Page.IsPostBack)

{ // executes only on initial page load}

//this code executes on every request}

private void Page_Load(object sender, System.EventArgs e){ if (!Page.IsPostBack)

{ // executes only on initial page load}

//this code executes on every request}

Page 24: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Lesson: Overview of Using XML Web Services

What is an XML Web Service?

Why use XML Web Services?

Finding an XML Web Service

Page 25: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

What is an XML Web Service?

Programmable logic accessible by standard Web protocols

Allows applications to send and receive information across the Internet

Language, protocol, and platform independent

Stateless architecture

Can be asynchronous

Based on an evolving W3C standard

Page 26: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Why Use XML Web Services?

InternetInternet

WeatherXML Web Service

WeatherXML Web Service

Exchange RateXML Web ServiceExchange Rate

XML Web Service

Pick your destination:

The weatherForecast calls for:

The exchange rate is:We can fly you there for only:

RainRain

Redmond

$1.56

$1,999.98Airfare

DatabaseAirfare

Database

AirfareXML Web Service

AirfareXML Web Service

Northwind Traders Travel SiteNorthwind Traders Travel Site

Page 27: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Publish XML Web service URL and description

Finding an XML Web Service

.disco

.wsdl

Web ServiceProxyProxy

WebForm

UDDI

11112222

3333

44445555 6666

1111

222233334444

5555

Discover XML Web service

Locate XML Web service URL

Read .wsdl description

Bind XML Web Service to Proxy

Call XML Web Service from the Web Formby Proxy

6666

Page 28: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Lesson: Using a Proxy to Call an XML Web Service

Using Proxies to Call XML Web Services

How to Use a Proxy to Call an XML Web Service

XML Web Service Error Handling

Page 29: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Using Proxies to Call XML Web Services

Appear the same as the original class, but do not contain application logic

Use SOAP to interact with the XML Web Service

Created from the ServiceName.asmx.wsdl file

Add members to manage interactions with the XML Web service and support asynchronous calls

InternetInternet XML WebService

XML WebService

ProxyProxy

WebForm

SOAP

Page 30: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

How to Use a Proxy to Call an XML Web Service

1. Create a Web reference for the XML Web Service

2. Create an instance of the XML Web Service

3. Call the Web methods of the XML Web Service

4. Build the ASP.NET Web Application

Sub Button1_Click(s As Object, e As EventArgs)...Dim ProxyGetStocks As New _ GetStocks.localhost.Service1()lblResults.Text = _

ProxyGetStocks.GetRating("Contoso")End Sub

Sub Button1_Click(s As Object, e As EventArgs)...Dim ProxyGetStocks As New _ GetStocks.localhost.Service1()lblResults.Text = _

ProxyGetStocks.GetRating("Contoso")End Sub

1111

2222

3333

4444

C# Code Example

Page 31: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

XML Web Service Error Handling

Service unavailable

SOAP exceptions from XML Web Services

GetStocks.StockWebRef.Service1 ProxyGetStocks = new GetStocks.StockWebRef.Service1();

ProxyGetStocks.Timeout = 10000;try{ lblMessage.Text = ProxyGetStocks.GetRating(TextBox1.Text);}catch (Exception err){ lblMessage.Text = err.Message;}

GetStocks.StockWebRef.Service1 ProxyGetStocks = new GetStocks.StockWebRef.Service1();

ProxyGetStocks.Timeout = 10000;try{ lblMessage.Text = ProxyGetStocks.GetRating(TextBox1.Text);}catch (Exception err){ lblMessage.Text = err.Message;}

Visual Basic .NET Code Example

Page 32: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Lesson: Creating an XML Web Service

How to Create an XML Web Service

XML Web Service Code

Instructor-Led Practice: Creating an XML Web Service

Page 33: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

How to Create an XML Web Service

1. Create a new XML Web Service project in Visual Studio .NET

2. Declare the WebMethod functions

3. Build the XML Web Service project

4. Test witha browser

1111

2222

3333

4444

Page 34: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

XML Web Service Code

.asmx page

.asmx.vb page

<%@ WebService Language="vb" Codebehind="Service1.asmx.vb" Class="XMLWebServiceName.Service1" %>

<%@ WebService Language="vb" Codebehind="Service1.asmx.vb" Class="XMLWebServiceName.Service1" %>

Imports SystemImports System.Web.Services

Class Service1<WebMethod()> Public Function function1() As type

'function_hereEnd FunctionEnd Class

Imports SystemImports System.Web.Services

Class Service1<WebMethod()> Public Function function1() As type

'function_hereEnd FunctionEnd Class

C# Code Example

Page 35: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Lesson: Configuring an ASP.NET Web Application

Overview of Configuration Methods

Configuring a Web Server Using Machine.config

Configuring an Application Using Web.config

Understanding Configuration Inheritance

Storing and Retrieving Data in Web.config

Using Dynamic Properties

Page 36: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Overview of Configuration Methods

Machine.config file

Machine-level settings

Web.config files

Application and directory-level settings

Both Machine.config and Web.config files are:

Well-formed XML

camelCase

Extendable

Page 37: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Configuring a Web Server Using Machine.config

Settings in the Machine.config file affect all Web applications on the server

Only one Machine.config file per Web server

Most settings can be overridden at the application level using Web.config files

Page 38: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Configuring an Application Using Web.config

One or more Web.config files per Web application

All configuration information for the application is contained in the Web.config files

Contains a section for each major category of ASP.NET functionality

Security

Mode

General application settings

Tracing

Page 39: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Understanding Configuration Inheritance

Application-level Web.config file inherits settings from Machine.config file

Settings in Web.config file that conflict override inherited settings

Individual directories may have Web.config files that inherit from—and can override—application-level settings

Machine.config

Web.config

Web.config

CONFIGCONFIG

VirtualDirVirtualDir

SubDirSubDir

Page 40: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Storing and Retrieving Data in Web.config

Storing application settings in a Web.config file

Retrieving application settings from a Web.config file

<configuration> <appSettings> <add key="pubs" value="server=localhost; integrated security=true; database=pubs"/> </appSettings></configuration>

<configuration> <appSettings> <add key="pubs" value="server=localhost; integrated security=true; database=pubs"/> </appSettings></configuration>

Dim strPubs As String = _ConfigurationSettings.AppSettings("pubs")

Dim strPubs As String = _ConfigurationSettings.AppSettings("pubs")

AppSettingsReader App = new AppSettingsReader();

string strPubs = (string)App.GetValue("pubs", typeof(string));

AppSettingsReader App = new AppSettingsReader();

string strPubs = (string)App.GetValue("pubs", typeof(string));

Page 41: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Using Dynamic Properties

Store property values in Web.config files rather than in the application's compiled code

Allows easy updates without recompiling the application

Enable and configure through object properties

Page 42: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Lesson: Deploying an ASP.NET Web Application

Web Application Deployment

Preparing a Web Application for Deployment

Practice: Selecting Necessary Files

Sharing Assemblies in the Global Assembly Cache

Updating Your Web Application

Page 43: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Web Application Deployment

Copy files locally or FTP files remotely

Configure the target folder as a virtual directory in IIS

Copy all necessary files, including the \bin directory and content

No need to register components

Page 44: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Preparing a Web Application for Deployment

1. Build the Web application

2. Do not select unnecessary files

Visual Studio .NET solution files (.vbproj, .vbproj.webinfo, .csproj, .csproj.webinfo, etc.)

Resource (.resx) files

Code-behind pages (.vb, .cs)

3. Copy or FTP necessary files to the production directory

Page 45: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Sharing Assemblies in the Global Assembly Cache

The global assembly cache provides storage for assemblies you need to share

Machine-wide cache for code

Because DLL files are not registered, they are not easily shared between Web applications

Page 46: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Updating Your Web Application

Copy or FTP files to update the Web application

Do not need to stop and restart IIS

.dll files can be updated while the site is still running

Page 47: Web Application Development Introduction. Lesson: Creating Web Forms What is a Web Form? Creating a Web Form with Visual Studio.NET.

Questions?