Upload
isaac-atkinson
View
220
Download
0
Embed Size (px)
Citation preview
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
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
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
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"/>
Types of Server Controls
HTML server controls
Web server controls
Intrinsic controls
Validation controls
Rich controls
List-bound controls
Internet Explorer Web controls
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==" />
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" />
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"/>
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
Lesson: Using Code-Behind Pages
How to Implement Code
What are Code-Behind Pages?
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
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
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
What are Event Procedures?
Action in response to a user’s interaction with the controls on the page
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">
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
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
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";
Lesson: Using Page Events
Understanding the Page Event Life Cycle
Handling Page.IsPostback Events
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
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}
Lesson: Overview of Using XML Web Services
What is an XML Web Service?
Why use XML Web Services?
Finding an XML Web Service
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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));
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
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
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
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
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
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
Questions?