Tổng quan về các .NET- Based Languages3 Hỗ trợ đa ngôn ngữ trợ đa ngôn ngữ lập trình Hiện tại hỗ trợ khoảng 20 NNLT Microsoft cung cấp các ngôn ngữ: Visual Basic .NET, C#, Visual J# .N
Trang 1Môn học: Lập trình ASP.NET
.NET-Based Languages
TRƯỜNG ĐẠI HỌC KHOA HỌC TỰ NHIÊN
KHOA CÔNG NGHỆ THÔNG TIN
BỘ MÔN CÔNG NGHỆ PHẦN MỀM
- -Nội dung
2
Trang 21 Tổng quan về các NET- Based Languages
3
Hỗ trợ đa ngôn ngữ
trợ đa ngôn ngữ lập trình
Hiện tại hỗ trợ khoảng 20 NNLT
Microsoft cung cấp các ngôn ngữ: Visual Basic NET,
C#, Visual J# NET, and JScript NET
Code modules có thể được tái sử dụng (resuable)
Lựa chọn ngôn ngữ thích hợp tương ứng với một tác
vụ cụ thể (ngôn ngữ phát triển web, ngôn ngữ phát
triển ứng dụng)
Hiệu năng tương đương cho tất cả ngôn ngữ
4
Trang 3The Common Language Runtime
5
CIL : Common Intermediate Language
CLR : Common Language Runtime
The Common Language Runtime
Garbage collection
Nhiều version của một DLL file có thể chạy đồng thời
Ứng dụng có thể lựa chọn version của DLL file để
chạy chương trình
6
Trang 4Các thành phần trong CLR
.NET Framework Class Library Support
Thread Support COM Marshaler
Type Checker Exception Manager
MSIL to Native
Compilers
Code Manager
Garbage Collector Security Engine Debug Engine
Class Loader
Runtime Compilation and Execution
Native code
C# code Visual Basic NET code
Which language?
Visual Basic NET compiler C# compiler
MSIL JIT
compiler default.aspx
Runtime
8
Trang 5Khái niệm Namespaces
Gom theo mối liên quan logical, không theo physical
Thể hiện kiến trúc phân tầng
tên class
9
Imports System.Data.SqlClient
using System.Data.SqlClient;
Sử dụng Namespaces
10
Dim listBox1 As New System.Web.UI.WebControls.ListBox()
listBox1.Items.Add("First Item")
Imports System.Web.UI.WebControls
Dim listBox1 As New ListBox() listBox1.Items.Add("First Item")
using System.Web.UI.WebControls;
ListBox listBox1 = new ListBox();
listBox1.Items.Add("First Item");
System.Web.UI.WebControls.ListBox listBox1 = new System.Web.UI.WebControls.ListBox();
listBox1.Items.Add("First Item");
Trang 62 So sánh các NET- Based Languages
C#
…
11
Visual Basic NET
Visual Basic
12
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 0 Dim x As Double = TextBox1.Text For i = 0 To 4
x *= 2 Label1.Text = Label1.Text & x & ","
Next End Sub
Trang 713
private void Button1_Click(object sender,
System.EventArgs e) {
int i = 0;
double x = Convert.ToDouble(TextBox1.Text);
for (i=0; i<=4; i++)
{
x *= 2;
Label1.Text = Label1.Text + x + ",";
}
}
3 Web Forms
cácWeb pages
Thành phần thể hiện presentation/ markup (HTML)
Thành phần lập trình (các đoạn code C#,VB.NET…), gọi
là programming logic.
14
Trang 8Programming logic
Trong cùng các file web page
Trong cùng file HelloWorld.aspx
Code declaration block
Trong một file class riêng
Code-behind file
Chứa hai file:
HelloWorld.aspx Web Form
HelloWorld.aspx.cs Code-behind file
15
16
Code declaration block
Code Declaration Block Page Directive
Web Form
ASP.NET Control
Trang 9Page directive
Web pages cho môi trường ASP.NET
Xác định ngôn ngữ lập trình được sử dụng là C#
FIT - HCMUS
17
Thành phần <script>
trong sẽ được thực thi ở server
18
Trang 10Thành phần <form>
form này
19
ASP.NET control
<asp: Label>
ID: ‘ labDate ’
runat = ‘ server ’ xác định server control
Tương đương với đoạn code
Label labDate = new Label ();
20
Trang 11Programming logic
Trong cùng các file web page
Trong cùng file HelloWorld.aspx
Code declaration block
Trong một file class riêng
Code-behind file
Chứa hai file:
HelloWorld.aspx Web Form
HelloWorld.aspx.cs Code-behind file
21
Code- behind file
22
HelloWorld.aspx HelloWorld.aspx.cs
Trang 12Code- behind file
thành phần giao diện
23
4 Tạo Component sử dụng VS.NET
24
Trang 13Khái niệm Classes và Components
diện (interfaces)
Components được complied thành các DLL files
giữa các ứng dụng
25
Component
Web application
Windows application
Windows application
Web application
Tạo Classes
26
Public Class Shipping
Function ShippingCost _
(ByVal sngPrice As Single) As Single
'…
Return (sngShipping)
End Function
End Class
}
public class Shipping
{
public Single ShippingCost (Single sngPrice)
{
//…
return sngShipping;
}
}
Trang 14Sử dụng Components
27
sngShipping = x.ShippingCost(sngPrice);
Dim x As New CompanyA.Shipping
Namespace CompanyA
Class Shipping
Function
ShippingCost (5)
End Class
End Namespace
component.d
ll
sngShipping = _ x.ShippingCost(sngPrice)
CompanyA.Shipping x = new CompanyA.Shipping();
namespace CompanyA {
class Shipping {
public void ShippingCost (5) { } }
}
component.d ll
Demo: Tạo Class sử dụng VS.NET
28
Trang 153 Lab Web Application
29
Medical
Medical.aspx
Benefits Home Page
Default.aspx
Life Insurance
Life.aspx
Retirement
Retirement.aspx
Dentists
Dental.aspx
Dentists
Doctors
Doctors.aspx
Doctors
Logon Page
Login.aspx
Registration
Register.aspx
Coho
Winery
Prospectus
Prospectus.aspx
XML Web Service
dentalService1.asmx
Page Header
Header.ascx ASPState
tempdb
Lab Web
Application
User Control
namedate.ascx
Class1.vb or Class1.cs
Menu Component
Class1.vb or Class1.cs
XML Files
Web.
config