Sie sind auf Seite 1von 21

UNIT IV

Web Forms
Web Forms are the User Interface (UI) elements that give your Web applications their look and feel. Web Forms are similar to Windows Forms in that they provide properties, methods, and events for the controls that are placed onto them. However, these UI elements render themselves in the appropriate markup language required by the request, e.g. HTML. Microsoft Visual Studio .NET, you will also get the familiar drag-and-drop interface used to create your UI for your Web application. Web Forms are made up of two components: the visual portion (the ASPX file), and the code behind the form, which resides in a separate class file.

Short History of Data Access


VT Objects Data Access Objects (DAO/Jet) Open Database Connectivity (ODBC) OLE for Databases (OLE/DB) ActiveX Data Objects (ADO)

Why is ADO.NET Better?


Disconnected by Design Relational by Nature Integration with XML Framework Supports Real Database Schema

Managed Providers
ADO.NETs Version of Providers (ADO, OLE/DB) and Drivers (ODBC) Not the only way to access data in .NET (most notably the Xml classes) Made up of a number of managed classes that implement known interfaces

System.Data Namespace
Dataset DataTable DataRow DataColumn etc. Data Relation ForeignKeyConstraint

System.Data.Common Namespace
DataAdapter DataTableMapping etc. DbDataRecord

System.Data.SqlClient Namespace SqlConnection SqlCommand SqlDataReader etc.

System.Data.OleDb Namespace OleDbConnection OleDbCommand OleDbDataReader etc.

Your Provider

YourConnection YourCommand YourDataReader etc.

What are DataSets?


Disconnected set of Database Data? In-Memory Database (IMDB)? A complex, relational data structure with built-in support for XML serialization? All three?

The DataSet
DataSet

Tables
DataTable Rows DataRow DataTable Rows DataRow

DataRelation Constraint ForeignKeyConstraint

using System; using System.Data; using System.Data.SqlClient;


class HelloADONET { static void Main() { SqlConnection conn = new SqlConnection("..."); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = conn.CreateCommand(); da.SelectCommand.CommandText = "SELECT * FROM AUTHORS"; DataSet dataSet = new DataSet(); da.Fill(dataSet); } }

Typed DataSets
Strong Typing XSD Based Schema Simple to Setup relationships, constraints, etc. Not very much use if you have amorphous data

DataBinding in ASP.NET
Data Binding is binding controls to data from databases. With data binding we can bind a control to a particular column in a table from the database or we can bind the whole table to the data grid. Data binding provides simple, convenient, and powerful way to create a read/write link between the controls on a form and the data in their application. Working with Data Binding in ASP.NET is slightly different to working with it in VB .NET. In VB .NET, the BindingContext object handles that and it lets us set the record bound control display. In ASP.NET, there is no BindingContext object to handle that. In ASP.NET, we use a DataView to let the user select which record should be displayed in bound controls. We bind the controls using data view and use the RowFilter property of the data view to select the record we want the bound control to display.

DataBinding in ASP.NET (2)


using System.Data; using System.Web.UI; using System.Web.UI.WebControls;
protected ListBox ListBox1; DataSet dataSet = new DataSet(); // ... // DataBind ListBox1.DataSource = dataSet; ListBox1.DataMember = "Customers"; ListBox1.DataTextField = "CompanyName"; ListBox1.DataValueField = "CustomerID"; ListBox1.DataBind();

What Is a Web Form?


.aspx extension Page attributes

@ Page directive

Body attributes Form attributes

<%@ Page Language="vb" Codebehind="WebForm1.aspx.vb" SmartNavigation="true"%> <html> <body ms_positioning="GridLayout"> <form id="Form1" method="post" runat="server"> </form> </body> </html>

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

What is a Framework?
Framework is a group of components that work interactively with requests from other components or objects to generate a Framework consistent output. Presentation,
(Black Box)

Company business rules, standards, policies

Java C# C++ VB.Net

business rules, database queries, etc

Object (GUI, XML, data, authentication, etc)


Controlled Environment

Example
Framework
(Black Box)

Company business rules, standards, policies

Java C# C++ VB.Net

Drop Down List, Select * from Users, log file

Render: Drop Down List of Users on a Web page or Window Application, recording any errors to the log file

75% complexity Procedural Application 100% complexity

25% complexity

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

How to Use a Proxy to Call an XML Web Service


1. 1 2. 2 3. 3 4. 4 Create a Web reference for the XML Web Service Create an instance of the XML Web Service Call the Web methods of the XML Web Service 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

C# Code Example

XML Web Service Error Handling Service unavailable


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; }

SOAP exceptions from XML Web Services

Visual Basic .NET Code Example

XML Web Service Code


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

.asmx.vb page
Imports System Imports System.Web.Services Class Service1 <WebMethod()> Public Function function1() As type 'function_here End Function End Class

C# Code Example

Cost Savings of a Framework/Web Service


Current Object Orient programming cost savings over procedural programming is 20% to 30% at the INEEL. Other companies are achieving 40% to 60% Framework can make up the difference and add another 30% cost savings

Das könnte Ihnen auch gefallen