Top Banner
Introduction to Web Application Development with .Net and Web Service ISYS 350
26

Introduction to Web Application Development with.Net and Web Service ISYS 350.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Introduction to Web Application Development with .Net and Web

Service

ISYS 350

Page 2: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Web Server

• Web Server:– Built-in Web Server

• VS 08 uses the built-in web server for debugging.• Localhost website for testing

• Default home page– Default.aspx, default.asp, default.htm

Page 3: Introduction to Web Application Development with.Net and Web Service ISYS 350.

HTML Form vs .Net Web Form

• HTML form:Created using HTML tags– Example: Fullname.htm

• <form name = "formControl" method="POST">

• <p>first Name:<input type="text" name="T1" size="20"></p> <p>Last Name:<input type="text" name="T2" size="20"></p> <p>Full Name: <input type="text" name="T3" size="20"></p> <p><input type="button" value="Calculate" name="B1" onClick="calcJS()"></p>

• <p><input type="reset" value="Reset" name="B2"></p> </form>

• .Net web form: Created using ASP.Net controls

Page 4: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Client-Side vs Server-Side Script

• Client-side: Script is executing on client-side– JavaScript

• Server-side: Script is executing on server-side and produce HTML code as output.– ASP.Net– Java Servlets and JSP– PHP– Etc.

Page 5: Introduction to Web Application Development with.Net and Web Service ISYS 350.

JavaScript Examplefunction calcJS(){var fName, lName;fName=document.formControl.T1.value;lName=document.formControl.T2.value;document.formControl.T3.value=fName + " " + lName;}--></script></head><body><form name = "formControl" method="POST"> <p>first Name:<input type="text" name="T1" size="20"></p> <p>Last Name:<input type="text" name="T2" size="20"></p> <p>Full Name: <input type="text" name="T3" size="20"></p> <p><input type="button" value="Calculate" name="B1" onClick="calcJS()"></p> <p><input type="reset" value="Reset" name="B2"></p></form>

Page 6: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Benefits of Server-Side Technology

• Browser compatibility: Every browser reads HTML.

• Protection of source code.• Controls are server-side objects with properties,

methods and events.

Page 7: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Web Project

• File/New Website/ ASP.Net Website• Website folder• Web form:

– default.aspx• Design view and source view

• Add a new page:– Website/Add New Item/Web form

• To set start up page: – Point to the web page in the Solution Explorer and right

click to choose Set As Start Page.

Page 8: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Create a web page to add two numbers

• Add controls:– Format/Set position/Absolute

Page 9: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Monthly Payment of a Loan

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim term, intRate, payment As Double intRate = ListBox1.SelectedValue term = RadioButtonList1.SelectedValue payment = Pmt(intRate / 12, term * 12, -CDbl(TextBox1.Text)) TextBox2.Text = payment End Sub

Page 10: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Data Grid

• Creating bound DataGrid by dragging a table from the Server Explorer

Page 11: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Working with Multiple Pages

• Redirect or transfer to another page:– Server.Transfer(“page name”)– Response.Redirect(“page name”)

Page 12: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Working with ADO.Net(DataReader can be used as a data source)

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer;" Dim objComm As New OleDbCommand(strSQL, objConn)

objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() GridView1.DataSource = objDataReader GridView1.DataBind()

Page 13: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Select a Rating from RadiobuttonList and Display Customers with that Rating

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer where rating = '" + RadioButtonList1.SelectedValue + "'" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() GridView1.DataSource = objDataReader GridView1.DataBind()

Page 14: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Postback

• Postback is the process by which the browser posts information back to the server telling the server to handle the event, the server does so and sends the resulting HTML back to the browser.

• Button control triggers the postback when clicked; other controls need to set the AutoPostBack property to true.

Page 15: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Working with Database Class

• Add a class:– Website/Add New Item/Class– Classes with be placed in a App_Code folder

• Adding an existing item– Example: Adding the Customer class created

earlier.

Page 16: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Working with an Assembly

• Website/Add refernce/Browse for the assembly

Page 17: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Web Service

• XML Web Service

• Web services are classes that are stored on the web which can instantiate and use in both Windows and Web applications.

Page 18: Introduction to Web Application Development with.Net and Web Service ISYS 350.

To Add a New Web Service:

• Website/Add New Item/Web Service– Web Service has an extension: ASMX– The CodeBehind File is stored in the App_Code

folder.

• Web Services are defined as Web Method:– <WebMethod()> programmed as a function

with return value:– <WebMethod()> Public Function GetCname(ByVal

CID As String) As String

Page 19: Introduction to Web Application Development with.Net and Web Service ISYS 350.

A Web Service Example

Public Class MyWebService Inherits System.Web.Services.WebService <WebMethod()> Public Function GetCname(ByVal CID As String) As String Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer where CID = '" & CID & "';" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() If objDataReader.Read() Then Return objDataReader("Cname") Else Return ("Not exist") End If End FunctionEnd Class

Page 20: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Web Service Description Language (WSDL)

• A WSDL file is an XML document containing a complete description of the web service. It shows a web service’s name, methods, and parameter types.

• Help page: After entering web service’s URL, a help page is displayed. You can click the Service Description link to see the WSDL file.

Page 21: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Consuming Web Services from a Web Application

• Add a web reference to the web service:– Website/Add Web Reference

• Within this soulution

• Local computer

• Internet

• Imports the web service

• Declare a web service class variable.– Dim UseWs As New DBWebService

Page 22: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Example

Imports localhost.Service1Partial Class Default3 Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim WS As New localhost.Service1 TextBox2.Text = WS.GetCname(TextBox1.Text) End SubEnd Class

Page 23: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Web Service that Returns an Object(Customer Object)

Public Function GetCustomer(ByVal CID As String) As Customer Dim myCust As New Customer myCust.getData(CID) If myCust.RecExist Then Return myCust Else myCust.cid = CID myCust.CName = "NA" myCust.City = "NA" myCust.Rating = "NA" Return myCust End If End Function

Page 24: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Using the Service

Dim WS As New WebService2 Dim myCust As New Customer myCust = WS.GetCustomer(TextBox1.Text) TextBox2.Text = myCust.CName TextBox3.Text = myCust.City TextBox4.Text = myCust.Rating

Page 25: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Reference a Web Service on Internet

• Mortgage calculator

• http://www.webservicex.net/mortgage.asmx

• This web service return an object of MortgageResults type with more than one properties.

Page 26: Introduction to Web Application Development with.Net and Web Service ISYS 350.

Example

Dim WS As New Mortgage.Mortgage Dim WSResult As MortgageResults WSResult = WS.GetMortgagePayment(10, 0.05, 2000, 0, 0) TextBox2.Text = WSResult.TotalPayment.ToString