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

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

  • View
    217

  • Download
    2

Embed Size (px)

Citation preview

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

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

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.

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>

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.

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.

Create a web page to add two numbers

• Add controls:– Format/Set position/Absolute

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

Data Grid

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

Working with Multiple Pages

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

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()

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()

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.

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.

Working with an Assembly

• Website/Add refernce/Browse for the assembly

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.

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

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

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.

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

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

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

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

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.

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