B?n ??ng bao gi? xem x�t ng�n ng? C# m?t c�ch t�ch bi?t, n� lu�n ??ng h�nh v?i "B? khung .NET". C# l� m?t tr�nh bi�n d?ch h??ng .NET, ngh?a l� t?t c? c�c m� c?a C# lu�n lu�n ch?y tr�n tr�n m�i tr??ng .NET Framework.
?i?u ?� d?n ??n 2 h? qu? sau
- C?u tr�c v� c�c l?p lu?n C# ???c ph?n �nh c�c ph??ng ph�p lu?n c?a .NET ng?m b�n d??i.
- Trong nhi?u tr??ng h?p, c�c ??c tr?ng c?a C# th?m ch� ???c quy?t ??nh d?a v�o c�c ??c tr?ng c?a .NET, ho?c th? vi?n l?p c? s? c?a .NET.
Ch�nh b?i t?m quan tr?ng c?a .NET, n�n c�c b?n c?n ph?i bi?t s? qua v? .NET tr??c khi ?i v�o ng�n ng? C#.
Ch�ng ta s? t�m hi?u xem chuy?n g� s? x?y ra khi m� c?a c�c ng�n ng? h??ng .NET (bao g?m C#) ???c bi�n d?ch v� th?c thi. ?�y l� m?t l?nh v?c r?ng, ch�ng ta s? t�m hi?u k? h?n v? Microsoft Intermediate Language (MS-IL), ng�n ng? trung gian trong .NET m� c?a c�c ng�n ng? kh�c ??u ph?i ???c bi�n d?ch v? ng�n ng? n�y tr??c khi th?c thi. C? th? ch�ng ta s? t�m hi?u xem c�ch th?c m� MS-IL v?i ph?n d�ng chung Common Type System (CTS) v� Common Language Specification (CLS) c� th? cung c?p cho ch�ng ta s? t??ng ho?t gi?a c�c ng�n ng? h??ng .NET. Ch�ng ta c?ng s? tr�nh b�y c�c ng�n ng? bi?t .NET kh�c bao g?m VB v� C++.
Sau ?� ch�ng ta s? xem x�t c�c ??c tr?ng kh�c c?a .NET, bao g?m c�c assembly, c�c namespace, v� th? vi?n l?p c? b?n c?a .NET. Ch�ng ta s? li?t k� v?n t?t v? c�c lo?i ?ng d?ng m� ch�ng ta c� th? t?o ra trong C#.
M?i quan h? gi?a C# v� .NET
C# l� m?t ng�n ng? l?p tr�nh m?i, v� ???c bi?t ??n v?i hai l?i ch�o : N� ???c thi?t k? ri�ng ?? d�ng cho Microsoft's .NET Framework (M?t n?n kh� m?nh cho s? ph�t tri?n, tri?n khai, hi?n th?c v� ph�n ph?i c�c ?ng d?ng).
N� l� m?t ng�n ng? ho�n to�n h??ng ??i t??ng ???c thi?t k? d?a tr�n kinh nghi?m c?a c�c ng�n ng? h??ng ??i t??ng kh�c.
M?t ?i?u quan tr?ng c?n nh? C# l� m?t ng�n ng? ??c l?p. N� ???c thi?t k? ?? c� th? sinh ra m� ?�ch trong m�i tr??ng .NET, n� kh�ng ph?i l� m?t ph?n c?a .NET b?i v?y c� m?t v�i ??c tr?ng ???c h? tr? b?i .NET nh?ng C# kh�ng h? tr? v� b?n c?ng ??ng ng?c nhi�n khi c� nh?ng ??c tr?ng C# h? tr? m� .NET kh�ng h? tr?.
?? t?o ???c nh?ng ?ng d?ng mang t�nh hi?u qu? cao, ch�ng ta s? xem qua v? ho?t ??ng c?a .NET
?o?n ASP n�y s? gi�p b?n g?i v� nh?n E-mail b?ng Microsoft's Collaboration Data Object. L?u ?o?n m� tr�n th�nh 1 file d?ng .asp . B?n s? ph?i thay ??i th�ng tin nh?: domain name server, ??a ch? E-mail v� Password c?a b?n ? c�c d�ng m� sau: Thay v� g?i b?ng textbody, b?n c� th? vi?t n?i dung E-mal nh? HTML v� thay ??i d�ng m� : N?u b?n mu?n c� nhi?u h? th?ng E-mail h?n tr�n Web c?a b?n th� b?n ph?i l?u ph?n n�y trong c�ng m?t trang ?? ??m b?o n� kh�ng b? l?i: Name redefined: B�y gi?, vi?c b?n ph?i l�m ti?p theo l� ch�p nh?ng ?o?n m� v�o file ASP:
cdoSMTPServer ="mail.yourdomain.com".
cdoSendUserName = "admin@yourdomain.com"
cdoSendPassword = "yourpassword"
<%
Set objMessage = Server.CreateObject("CDO.Message")
Set objMessage.Configuration = objConfig
With objMessage
.To = "Your Email"
.From = "Customer EMail"
.Subject = "Thank you for your registration"
.TextBody = "Hi, I hope you enjoy our service @ " & Now()
.Send
End With
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
%>
H�m UCASE() d�ng ?? chuy?n t?t c? c�c k� t? trong m?t chu?i th�nh ch? HOA.
H�m UCASE()
H�m UCASE() d�ng ?? chuy?n t?t c? c�c k� t? trong m?t chu?i th�nh ch? HOA (UPPERCASE). C�c k� t? kh�ng thu?c b?ng ch? c�i s? ???c gi? nguy�n v� cho ra k?t qu? t??ng t?. Ta c� th? vi?t UCASE() hay UCase()
V� D? 1:
<%
Response.Write("this is an example 1")
%>
K?t qu?:
this is an example 1
V� D? 2:
<%
Response.Write UCASE("this is an example 1")
%>
K?t qu?:
THIS IS AN EXAMPLE 1
V� D? 3:
<%
Response.Write UCASE("<b>this is an example 1</b>")
%>
K?t qu?:
THIS IS AN EXAMPLE 1
V� D? 4:
<%
Dim strExample
strExample = "this is an example 1"
Response.Write("" & UCASE(strExample) & "")
%>
K?t qu?:
THIS IS AN EXAMPLE 1
V� D? 5:
<%
Dim strExample
strExample = "this is an example 1"
Response.Write("<b>" & UCASE(strExample) & "</b>")
%>
K?t qu?:
THIS IS AN EXAMPLE 1
H�m LCASE()
H�m LCASE() d�ng ?? chuy?n t?t c? c�c k� t? trong m?t chu?i th�nh ch? th??ng(LOWERCASE). C�c k� t? kh�ng thu?c b?ng ch? c�i s? ???c gi? nguy�n v� cho ra k?t qu? t??ng t?. Ta c� th? vi?t LCASE() hay LCase()
V� D? 1:
<%
Response.Write LCASE("This Is An Example 2")
%>
K?t qu?:
this is an example 2
V� D? 2:
<%
Dim strExample
strExample = "THIS IS AN EXAMPLE 2"
Response.Write("<b>" & LCASE(strExample) & "</b>")
%>
K?t qu?:
this is an example 2
?o?n Code sau ?�y gi�p ch�ng ta th�m s? 0 v�o tr??c c�c ng�y(th�ng) 1 ??n 9 ?? chuy?n ch�ng th�nh 01-09.
S? d?ng h�m Day(Date) Month(Date) ?? l?y ng�y, th�ng v� h�m Len ?? so s�nh ?? d�i c?a chu?i ng�y th�ng.
?? l?y ng�y v� th�ng tr??c h?t ch�ng ta khai b�o 2 bi?n chu?i sau:
Dim strDay, strMonth
N?u chu?i gi� tr? c?a strDay = 1 (k� t?) t?c ng�y 1-9(ng�y n�y ch? 1 k� t? 1,2,3...) th� ch�ng ta th�m s? 0 v�o tr??c:
If Len(Day(Date) = 1 Then 'N?u chu?i Day(Date) ch? c� 1 character th�:
strDay = "0" & Day(Date) 'Th�m s? 0 v�o tr??c chu?i ?�
Else 'C�n l?i th�
strDay = Day(Date) 'strDay = gi� tr? c?a ng�y.
End If
L�m t??ng t? v?i h�m Month(Date)
?�y l� Source codes ??y ?? c?a h�m 3 h�m Day(Date), Month(Date) v� Year(Date)
<%
Dim strDay, strMonth, strYear
strDay = Day(Date)
strMonth = Month(Date)
If Len(Day(Date)) = 1 Then
strDay = "0" & Day(Date)
Else
strDay = Day(Date)
End If
If Len(Month(Date)) = 1 Then
strMonth = "0" & Month(Date)
Else
strMonth = Month(Date)
End If
strYear = Year(Date)
%>
L?y k?t qu?:
<% = strDay %>/<%= strMonth %>/<% = strYear %>
?? hi?n th? Ng�y th�ng n?m hi?n t?i ta c� th? d�ng h�m Date() v� Th?i gian v?i H�m Time(). C�n ?? hi?n th? c�ng l�c Ng�y th�ng n?m v� Th?i gian ta c� th? d�ng H�m Now(). D??i ?�y l� b?ng h??ng d?n s? d?ng hai H�m n�y:
Ng�ai ra b?ng c�ch d�ng c�c tham s? v� m?nh ?? ?i?u ki?n, ta c� th? bi?n h�a ?? hi?n th? Date v� Time theo � mu?n. V� D?: Vi?t K?t Qu? Hi?n Th?: <%= time()%> Time()Gi? tr�n Server) <%=date()%> " & Date() & " <%=now()%> 9/17/2006 11:25:16 PM <% Response.Write("Ng�y gi? hi?n t?i: "&now())%> Ng�y gi? hi?n t?i(Server): 9/17/2006 11:25:16 PM <%= FormatDateTime(Date, 0)%> 9/17/2006 <%= FormatDateTime(Date, 1)%> Sunday, September 17, 2006 <%= FormatDateTime(now, 3)%> 11:25:16 PM <%= FormatDateTime(now, 4)%> 23:25 <%response.write("Day of the week: "&WeekDay(Date))%> Day of the week: 1 <%response.write("Day of the month: "&Day(Date))%> Day of the month: 17 <%response.write("Current Year: "&Year(Date))%> Current Year: 2006 <%response.write("Current Year: "&Right(Year(Date),2))%> Current Year: 06 <%response.write("Today is: "&WeekDayName(WeekDay(Date)))%> Today is: Sunday <%response.write("Hour Part: "&(hour(now)))%> Hour part: 23 <%response.write("Minute Part: "&Minute(now()))%> Minute part: 25 <%response.write("Second Part: "&Second(now()))%> Second part: 16
?? b?t ??u c?p nh?t d? li?u v�o File CSDL login.mdb, ch�ng ta c?n ph?i li�n k?t ??n t?p tin n�y. Tr??c khi li�n k?t, ch�ng ta c?n x�c l?p m?t s? c?u h�nh ???ng d?n(Server.MapPath), cho ph�p s? d?ng Parent Path truy xu?t ??n th? m?c b?t k? ch?a t?p tin CSDL.
Cho r?ng b?n ?� c�i IIS v�o Local Computer c?a b?n.
Cho r?ng b?n ?� t?o th? m?c sau ?�y tr�n m�y Local Computer c?a b?n:
C:\inetpub\myweb\mydomain\database. Trong ?� mydomain l� th? m?c g?c ?? ch?a c�c file .asp v� th? m?c database d�ng ?? ch?a t?p tin CSDL. ? ?�y ch�ng ta kh�ng ?? c?p ??n vi?c t?o Virtual Directory v� X�c l?p File Default.asp cho trang Web. V?n ?? ? ch? ?�i khi b?n kh�ng ??t th? m?c database v�o th? m?c g?c m� tr? ch�ng ??n m?t ???ng d?n n�o ?� c� th? l�n v�i SubFolder ho?c xu?ng v�i subfolder, khi ?� b?n ph?i d�ng c�c khai b�o nh? /subfolder/database ho?c ../../subfolder/database. N?u b?n kh�ng Enable Parent Path trong IIS, m� s? d?ng c�c k� t? nh? tr�n khi khai b�o ???ng d?n Server.MapPath, qu� tr�nh li�n k?t CSDL c� th? s? g?p ph?i l?i.
?? kh?c ph?c l?i n�y, b?n th?c hi?n c�c b??c sau:
T? Windows Desktop:
-Start --> Control Panel
-Doubl Click Administrative Tools.
-Double Click Internet Information Services.
-Click Local Computer
-Click Websites
-Click Default Websites
-Ch?n Mydomain --> Right Click ch?n Properties
-C?a s? mydomain properties m? ra nh? h�nh d??i:
Domain Properties
Trong c?a s? n�y Click Configuration, trong c?a s? m?i Application Configuration, click ch?n Options nh? h�nh d??i:
Application Configuration-Option-Enable parent Paths
Click OK, OK v� Exit IIS. Xong, t?t c? c�c c�ng ?o?n cho Datafile ho�n t?t. Ch�ng ta b?t ??u li�n k?t CSDL.
T?p tin li�n k?t CSDL:
T?p tin n�y s? ???c s? d?ng trong t?t c? c�c b�i h?c, v� ch�ng ta l?u v?i t�n file dbconnection.asp
C�ch 1:
<% Dim strDBPath, strCon
strDBPath=Server.MapPath("database/login.mdb")
strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strDbPath
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open strCon
Err.Clear
On Error Resume Next
%>
C�ch 2:
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.connectionstring = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" _ & Server.Mappath("database/login.mdb") & ";User Id=Administrator;Password=something;"
Conn.Open
%>
C�ch 3:
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0; DBQ=" _& Server.Mappath("database/login.mdb") & "User ID=Administrator;Password=something;"
Conn.Open
%>
N?u b?n kh�ng Set User ID v� password th� x�a ph?n n�y.
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0; DBQ=" _
& Server.Mappath("database/login.mdb") & ";"
Conn.Open
%>
Ch?n 1 c�ch t?t nh?t cho b?n, v?i b�i th?c h�nh n�y b?n n�n ch?n c�ch 1. L?u t?p tin tr�n v?i t�n dbconnection.asp v� ch�ng ta s?n s�ng cho c�c b�i k? ti?p.
Ch�ng ta bi?t r?ng, c�c thao t�c c?p nh?t hay x�a d? li?u tr�n CSDL ch? c� th? th?c hi?n ???c b?ng VBScripting Language khi t?p tin ?� ?� ???c c?p m?t s? quy?n truy c?p nh?t ??nh. Th�ng th??ng ?� l� quy?n Vi?t(c?p nh?t-write) v�o CSDL.
Trong b�i tr??c ch�ng ta ?� t?o file CSDL v?i t�n l� Login.mdb, trong ?� ch�ng ta c� b?ng tblMembership v?i 3 tr??ng, 1 tr??ng LoginID gi? ID c?a m?i User v� 2 tr??ng Text: Username v� Userpassword, gi? t�n ??ng nh?p v� m?t kh?u.
Ch�ng ta bi?t r?ng, c�c thao t�c c?p nh?t hay x�a d? li?u tr�n CSDL ch? c� th? th?c hi?n ???c b?ng VBScripting Language khi t?p tin ?� ?� ???c c?p m?t s? quy?n truy c?p nh?t ??nh. Th�ng th??ng ?� l� quy?n Vi?t(c?p nh?t-write) v�o CSDL.
Do ?�, ?? c� th? thao t�c tr�n CSDL Login.mdb, ch�ng ta c?n thi?t l?p m?t s? quy?n truy c?p(read/write) cho File n�y. T? Windows Desktop, Click Start --> My Computer, trong h?p tho?i My Computer, click Th?c ??n Tools, Ch?n Folder Options, h?p tho?i Folder Options m? ra nh? h�nh d??i:
Thi?t l?p thu?c t�nh chi s? t�i nguy�n.
Click Tab View, d�ng chu?t k�o thanh cu?n (scroll bar) xu?ng v� Click b? ch?n Use Simple File Sharing (recommended), nh? h�nh tr�n, Click OK.
Tr? v? c?a s? My Computer, t�m ???ng d?n ??n File login.mdb m� b?n ?� l?u tr??c ?� nh? h�nh minh h?a d??i:
Thi?t l?p thu?c t�nh cho file login.mdb.
Right Click l�n File login.mdb v� ch?n Properties. C?a s? Properties m? ra, Click ch?n Tab Security nh? h�nh d??i:
C?p quy?n truy c?p cho File login.mdb.
? h?p tho?i Group or Usernames, Click ch?n Users , ? h?p tho?i Permissions for Users, Click ch?n nh? h�nh tr�n, Xong Click Apply, Click OK.
B�y gi? File login.mdb ?� s?n s�ng cho vi?c c?p nh?t d? li?u v�o ?�.
?o?n Code sau ?�y gi�p b?n hi?n th? ng�y th�ng n?m theo ti?ng Vi?t v� ??nh d?ng ch? theo CSS.
<% strThu = WeekDayName(WeekDay(Date))
If strThu = "Sunday" Then
Response.Write("<span style=""font-size:16;font-weight:bold; color:red;line-height:2em;"">Ch? Nh?t</span><br>")
End If
If strThu = "Monday" Then
Response.Write("<span style=""font-size:16;font-weight:bold;color: blue;line-height:2em"">Th? Hai</span><br>")
End If
If strThu = "Tuesday" Then
Response.Write("<span style=""font-size:16;font-weight:bold;color: blue;line-height:2em"">Th? Ba</span><br>")
End If
If strThu = "Wednesday" Then
Response.Write("<span style=""font-size:16;font-weight:bold;color: blue;line-height:2em"">Th? T?</span><br>")
End If
If strThu = "Thursday" Then
Response.Write("<span style=""font-size:16;font-weight:bold;color: blue;line-height:2em"">Th? N?m</span><br>")
End If
If strThu = "Friday" Then
Response.Write("<span style=""font-size:16;font-weight:bold;color: blue;line-height:2em"">Th? S�u</span><br>")
End If
If strThu = "Saturday" Then
Response.Write("<span style=""font-size:16;font-weight:bold;color: lime;line-height:2em"">Th? B?y</span><br>")
End If
Response.Write(" <b><font color=""#FFFFFF"">Ng�y " & day(date()) &" Th�ng "& month(date()) &"</font><br> <br><font size=""+1"" color=""red"">N?m "& year(date()) &" </font></b>")
%>
Sau ?�y l� c�c h�m ki?m tra bi?n
IsArray
IsArray tr? gi� tr? Boolean(True or False) khi bi?n l� m?t m?ng. C� ph�p c?a IsArray nh? sau:
IsArray(t�n bi?n)
??i s? t�n bi?n c� th? l� m?t bi?n b?t k?. IsArray tr? k?t qu? True (?�ng) n?u bi?n l� m?t m?ng(array), ng??c l?i IsArray cho k?t qu? False(sai. H�m IsArray ??c bi?t ti?n l?i v?i c�c bi?n th? (variants) ch?a m?ng.
IsDate
IsDate tr? gi� tr? Boolean khi bi?u th?c c� th? ???c chuy?n ??i sang d?ng Date(ng�y th�ng n?m). C� ph�p c?a H�m IsDate nh? sau:
IsDate(Bi?u th?c)
??i s? c?a bi?u th?c c� th? l� ng�y th�ng n?m b?t k? ho?c l� bi?u th?c chu?i m� VBScript h?p l? theo ng�y th�ng n?m ho?c th?i gian m� ng�n ng? n�y quy ??nh. IsDate tr? k?t qu? True n?u bi?u th?c l� Ng�y Th�ng N?m(Date) ho?c c� th? chuy?n ??i th�nh ng�y th�ng n?m h?p l?. N?u bi?u th?c kh�ng ph?i l� ng�y th�ng n?m ho?c ng�y th�ng n?m h?p l?, h�m s? tr? k?t qu? False. Trong Micrisoft, ng�y h?p l? l� January 01, 100 AD ??n december 31,9999.
M?t v�i v� d?:
<%
document.write(IsDate("April 22, 1947") & "
")
document.write(IsDate(#11/11/01#) & "
")
document.write(IsDate("#11/11/01#") & "<br>")
document.write(IsDate("Hello World!"))
%>
K?t qu?:
True
True
False
False
Hai bi?u th?c ??u ?�ng v� 2 bi?u th?c sau sai v� H�m IsDate nh?n th?y r?ng c�c gi� tr? trong bi?u th?c n�y kh�ng h?p l?.
IsEmpty
IsEmpty tr? gi� tr? Boolean khi m?t bi?n ???c kh?i t?o.(ch?a gi� tr? r?ng). H�m n�y r?t h?u �ch trong vi?c ki?m tra Login c?a User ho?c ki?m tra gi� tr? trong Request.QueryString.
C� ph�p c?a IsEmpty:
IsEmpty(Bi?u th?c)
??i s? c?a bi?u th?c c� th? l� m?t bi?u th?c b?t k?. Tuy nhi�n, v� IsEmpty x�c ??nh r?ng c�c bi?n c� nh�n ???c kh?i t?o hay kh�ng, n�n ??i s? c?a bi?u th?c th??ng l� m?t bi?n ??n. H�m IsEmpty tr? k?t qu? True n?u bi?n kh�ng kh?i t?o ho?c bi?n r?ng. Ng??c l?i h�m IsEmpty tr? k?t qu? False.
Xem V� d?:
<%
Dim LoginID, NoLogin
LoginID = Request.Form("loginid")
NoLogin = IsEmpty(LoginID)
If NoLogin = True Then
response.write "You must login to view this content!"
End If
%>
<%
Dim intPageNumber, intPageEmpty
intPageNumber = Request.Querystring("p")
intPageEmpty = IsEmpty(intPageNumber)
If intPageEmpty = True Then
Response.Write "You are in page number 1"
Else
Response.Write "You are in page number " & intPageNumber & ""
End If
%>
IsNull
IsNull tr? gi� tr? Boolean khi m?t bi?u th?c ch?a d? li?u kh�ng h?p l?. C� ph�p c?a IsNull nh? sau:
IsNull(bi?u th?c)
IsNumeric
IsNumeric tr? gi� tr? Boolean khi m?t bi?u th?c ???c ki?m ??nh l� m?t s?. C� ph�p c?a IsNumeric nh? sau:
IsNumeric(bi?u th?c)
V� d?:
<%
Dim intSongID
intSongID = Request.Querystring("songid")
If IsNumeric(intSongID) = False Then
Response.Write "The song you are looking for is not in our server."
Else
Response.Write "You are listening song number " & intSongID & ""
End If
%>
IsObject
IsObject tr? gi� tr? Boolean khi c�c tham chi?u c?a bi?u th?c l� m?t ??i t??ng t? ??ng OLE h?p l?. C� ph�p c?a IsObject nh? sau:
IsObject(Bi?u th?c)
??i s? c?a bi?u th?c c� th? l� m?t bi?u th?c b?t k?. IsObject tr? gi� tr? True n?u bi?u th?c l� bi?n c?a m?t ki?u ??i t??ng ph? ho?c m?t ??i t??ng do l?p tr�nh vi�n ??nh ngh?a, ng??c l?i n� tr? k?t qu? False.
V�i v� d?:
<%
dim x
set x=me
document.write(IsObject(x))
%>
K?t qu?:
True
<%
dim x
x="me"
document.write(IsObject(x))
%>
Output:
False
VarType
VarType tr? gi� tr? ch? ??nh ki?u ph? c?a m?t bi?n. C� ph�p nh? sau:
VarType(T�n bi?n)
??i s? T�n bi?n c� th? l� m?t bi?n b?t k?.
L?p tr�nh vi�n c� th? t?o h?ng ri�ng theo c� ph�p khai b�o. Ngo�i ra, VBScript c�n cung c?p m?t s? h?ng v?i t�n ?� x�c ??nh nh? : Nh? t�n g?i, h?ng gi? gi� tr? kh�ng ??i khi ch??ng tr�nh th?c thi. Trong Visual Basic ho?c VBScript, h?ng ???c d�ng ?? gi? c�c gi� tr? nh? t�n c?a th�ng hi?n t?i ho?c t�n c?a trang Web. Gi� tr? c?a h?ng lu�n c? ??nh. L?p tr�nh vi�n c� th? t?o h?ng ri�ng theo c� ph�p khai b�o. Ngo�i ra, VBScript c�n cung c?p m?t s? h?ng v?i t�n ?� x�c ??nh nh?: S? kh�c nhau gi? Null v� Empty ?�i khi d? nh?m l?n. Gi? th? bi?n l� m?t chi?c h?p hay th�ng ??ng gi� tr?. N?u bi?n ???c Set Empty th� c�i h?p n�y tr?ng. Ngh?a l� bi?n s?n s�ng ?? nh?n d? li?u nh?ng hi?n n� kh�ng c� d? di?u trong ?�. N?u Bi?n Set Null ngh?a l� h?p n�y ?ang ch?a d? li?u kh�ng h?p l?. ?? khai bao h?ng, s? d?ng t? kh�a Const. M?t v�i v� d?: B?i v� m?i m?t h?ng s? chia s? b? nh? m�y t�nh, do v?y n�n gi?m khai b�o h?ng c�ng �t c�ng t?t. Trong ng�n ng? l?p tr�nh, d? li?u ???c l?u tr? trong c�c bi?n. Do ?�, ta xem bi?n nh? m?t chi?c h?p ??ng d? li?u. M?t bi?n ???c d�ng ?? gi? m?t lo?i d? li?u, c� th? l� ch?, s? nguy�n, s? th?p ph�n, ng�y th�ng. Ta c� th? ??t t�n bi?n t�y �, mi?n sao ??ng tr�ng v?i c�c t? ?u ti�n c?a VBScript. (Reserved words) V� d?: Khai b�o bi?n v?i t�n sao cho d? nh? v� lo?i gi� tr? m� bi?n gi?. ? v� d? tr�n, bi?n strWebsiteName l� m?t chu?i n�n b?t ??u b?ng str vi?t t?t c?a string. Bi?n intPicturewidth cho bi?t bi?n n�y gi? gi� tr? l� m?t s? nguy�n n�n b?t ??u b?ng int: integer. ?? g�n gi� tr? r?ng cho m?t bi?n, khai b�o bi?n r?i g�n gi� tr? "" cho bi?n ?�. V� d?: Gi� tr? c?a bi?n do ch�ng ta g�n, ho?c l?y t? m?t Form trong trang, ho?c gi� tr? m?t Session, Cookies m� ch�ng ta t?o. V� d?: Ch�ng ta c� th? t?o m?t danh s�ch c�c bi?n to�n c?c (Global Variables) v� ??t trong m?t File r?i Include file n�y v�o trong c�c trang ASP. Ngo�i ra, trong m?t trang ch�ng ta c� th? khai b�o bi?n ri�ng cho trang ?�. C�c bi?n n�y ch? c� gi� tr? cho trang ?� m� th�i.
H?ng-Contstants:
SET objRS = Nothing
Khi ?� ta y�u c?u tr�nh VBScript r?ng h�y k?t th�c vi?c d�ng ??i t??ng objRS v� x�a ??i t??ng n�y kh?i b? nh? c?a h? th?ng. Tuy v?y, khi ch�ng ta c� v�i bi?n tham chi?u c�ng ??i t??ng, VBScript s? kh�ng x�a ??i t??ng cho ??n khi t?t c? c�c bi?n tham chi?u ??i t??ng ?� ???c Set = Nothing.
If IsNull(Session("AdminLogin")) = True Then
Th?c hi?n l?nh
End If
II-Khai b�o h?ng:
III-Bi?n v� lo?i d? li?u:
1-Bi?n ??n:
Dim strWebSiteName Dim intPictureWidth, intPictureHeight
Dim strMemberName, strMemberEmail, strMemberPassword, BoolMemberAcceptMail
Dim A
A =" "
Dim strWebsiteName, intPictureWidth
strWebsiteName = request.form("websitename")
intPictureWidth = request.form("picturewidth") ho?c
strWebsiteName = Request.Cookies("websitename")
intPictureWidth = Request.Cookies("picturewidth") ho?c
intPictureWidth = 120 strWebsiteName = "www.thuvien-it.net"
2-Bi?n m?ng-Array
<%
Dim membername(5),i
membername(0) = "David"
membername(1) = "Jimmy"
membername(2) = "Andy"
membername(3) = "Steve"
membername(4) = "Paul"
membername(5) = "Emily"
For i = 0 to 5
response.write(membername(i) & "<br>")
Next
%>
K?t qu?:
David
Jimmy
Andy
Steve
Paul
Emily
3-Option Explicit
Ph�t bi?u Option Explicit y�u c?u ch�ng ta khai b�o m?i bi?n m� ch�ng ta d�ng. V?i ph�t bi?u n�y, n?u m?t t�n bi?n ch?a ???c khai b�o m� g�n gi� tr? cho t�n ?� th� khi th?c thi, ch??ng tr�nh s? b�o l?i. V?i c�c ch??ng tr�nh ph?c t?p v?i qu� nhi?u bi?n, th� kh�ng n�n d�ng Ph�t bi?u n�y.
Sau ?�y l� c�ch vi?t Th? Ng�y Th�ng theo ?inh d?ng DD/MM/YY
<% Dim strThu
strThu = WeekDayName(WeekDay(Date))
If strThu = "Sunday" Then
Response.Write("<b>Ch? Nh?t, </b>")
End If
If strThu = "Monday" Then
Response.Write("<b>Th? Hai, </b>")
End If
If strThu = "Tuesday" Then
Response.Write("<b>Th? Ba, </b>")
End If
If strThu = "Wednesday" Then
Response.Write("<b>Th? T?, </b>")
End If
If strThu = "Thursday" Then
Response.Write("<b>Th? N?m, </b>")
End If
If strThu = "Friday" Then
Response.Write("<b>Th? S�u, </b>")
End If
If strThu = "Saturday" Then
Response.Write("<b>Th? b?y, </b>")
End If
Response.Write(" <b>Ng�y " & day(date()) &" Th�ng "& month(date()) &", N?m "& year(date()) &" </b>")
%>
T?o d�ng ch? ch?y k?t h?p gi?a Javascript v� CSS. Xem minh h?a d??i ?�y
Thanks for your help.
Ch�n ?o?n Codes sau v�o ph?n BODY:
<span id="bob" style="position:relative;top:0;left:10;"><font size=6 color=darkred>Thanks for your help.</font></span>
Copy v� d�n ?o?n codes tr�n v�o d??i d�ng tr�n.
L?u �: B?n c?n ph?i thay ??i t?a ?? Left, Top ?? d�ng ch? hi?n th? ?�ng trong b�i th?c h�nh.
M?t v� d? ??n gi?n thay ??i m�u ch? c?a th? <p>. V� d? n�y s? d?ng m?t danh s�ch m�u t? th�nh ph?n <select>. V� ng??i d�ng ch? vi?c Ch?n m�u t? m?t Drop down list.
L?a ch?n m?t m�u ?? thay ??i m�u c?a d�ng ch? tr�n:
| <script language="javascript"> function changeTDColor(){ var selectOpt = document.getElementById('color'); var td = document.getElementById('content'); td.style.color = selectOpt.options[selectOpt.selectedIndex].value; } </script> <div id="content">?�y l� m?t v� d? ??n gi?n c?a DHTML, s? d?ng getElementById Method ?? l?y d? li?u t? m?t th�nh ph?n c�ng v?i id duy nh?t.</div> L?a ch?n m?t m�u ?? thay ??i m�u c?a d�ng ch? tr�n: <select name="color" id="color" onchange="changeTDColor()"> <option value="">?en <option value="#bc0fc1">H?ng <option value="#FF0000">?? <option value="#800000">?? ??m <option value="#0000FF">Xanh <option value="navy">Xanh ??m <option value="#00ff00">L� C�y <option value="#008000">L� C�y ??m </select> |
D??i ?�y l� m?t k? thu?t minh h?a k?t h?p gi?a CSS v� Javascript ?? t?o c�c ?nh ??ng.
B?m V�O ?�Y ?? xem hi?u ?ng c?a b�i h??ng d?n n�y
?? l�m ???c nh? v?y, ch�p ?o?n Code sau d�n v�o ph?n <head></head>, Right Click v� Save 2 File ?nh tr�n v�o c�ng th? m?c v?i file HTML (ho?c thay ???ng d?n tuy?t ??i t?i h�nh ?nh.)<script language="javascript" type="text/javascript">
t=0; function moveittop(){ document.getElementById("electron").style.top =165*Math.cos(180*Math.PI/180)*Math.cos(t) - 40*Math.sin(45*Math.PI/180)*Math.sin(t) + 190; document.getElementById("earth").style.left =200*Math.sin(180*Math.PI/180)*Math.cos(t) + 40*Math.cos(45*Math.PI/180)*Math.sin(t) + 175; t = t + .1; setTimeout("moveittop(t)",100); } t=0; function moveitleft(){ document.getElementById("electron1").style.top =165*Math.cos(270*Math.PI/180)*Math.cos(t) - 40*Math.sin(45*Math.PI/180)*Math.sin(t) + 190; document.getElementById("earth1").style.left =174*Math.sin(270*Math.PI/180)*Math.cos(t) + 40*Math.cos(45*Math.PI/180)*Math.sin(t) + 180; t = t + .1; setTimeout("moveitleft(t)",100); } t=0; function moveit_45(){ document.getElementById("electron2").style.top =165*Math.cos(230*Math.PI/180)*Math.cos(t) - 30*Math.sin(45*Math.PI/180)*Math.sin(t) + 195; document.getElementById("earth2").style.left =165*Math.sin(230*Math.PI/180)*Math.cos(t) + 30*Math.cos(45*Math.PI/180)*Math.sin(t) + 180; t = t + .1; setTimeout("moveit_45(t)",100); } t=0; function moveit_135(){ document.getElementById("electron3").style.top =165*Math.cos(135*Math.PI/180)*Math.cos(t) - 30*Math.sin(315*Math.PI/180)*Math.sin(t) + 195; document.getElementById("earth3").style.left =165*Math.sin(135*Math.PI/180)*Math.cos(t) + 30*Math.cos(315*Math.PI/180)*Math.sin(t) + 170; t = t + .1; setTimeout("moveit_135(t)",100); } function moveit(){ moveittop(t); moveitleft(t); moveit_135(t); moveit_45(t); }
</script>
Th�m c�c d�ng codes sau ?�y v�o trong ph?n BODY:<form>
<input type="button" value="Move It" onclick="'moveit();'">
</form>
<div id="atomic" style="position:absolute; top:200; left:300;">
<img src="http://thuchanh.it2know.googlepages.com/atomback.gif" />
<div id="electron" style="position:absolute; top:25; left:175;">
<imgsrc="http://thuchanh.it2know.googlepages.com/electron.gif"></div>
<div id="electron1" style="position:absolute; top:190; left:15;">
<img src="http://thuchanh.it2know.googlepages.com/electron.gif" /></div>
<div id="electron2" style="position:absolute; top:90; left:50;">
<img src="http://thuchanh.it2know.googlepages.com/electron.gif" /></div>
<div id="electron3" style="position:absolute; top:90; left:270;">
<img src="http://thuchanh.it2know.googlepages.com/electron.gif" />
</div>
</div>
</div>
T�y theo m�n h�nh m� b?n c� th? ph?i thay ??i gi� tr? t?a ?? ?? c�c h?t electron n?m ngay tr�n ??nh c?a Atom. B?n c� th? c?n th�m v�o h�m Stop It ?? ng?ng s? chuy?n ??ng c?a h?t electron.
?? Test b�i n�y, b?n c?ng c� th? ch�p c? 2 ?o?n Code tr�n V�O ?�Y ?? th?y k?t qu?
M?t v� d? ??n gi?n s? d?ng ph??ng ph�p getElementById ?? thay ??i m�u c?a th? paragraph.
D�ng ch? n�y c� m�u b�nh th??ng, H�y b?m n�t "??i m�u" ?? th?y s? thay ??i v? m�u s?c
?o?n Codes sau ?�y d�ng Event Handler OnMouseOver, OnMouseOut ?? thay ??i m�u n?n c?a th�nh ph?n
B?n c� th? k?t h?p CSS ?? thay ??i c�c thu?c t�nh c?a th�nh ph?n , khi ?� c? Link v� n?n thay ??i c�ng l�c theo m�u b?n thi?t l?p.
??a chu?t l�n t?ng TD ?? th?y s? thay ??i
HomePage www.it2know.net
?�y l� m?t s? Event Handler th�ng d?ng v� c�ch d�ng ch�ng trong th�nh ph?n n�o c?a HTML.
EVEN HANDLER : Can be used in: onBlur select, text, textarea onChange select, text, textarea onClick button, checkbox, radio, link, reset, submit onFocus select, text, textarea onLoad window onMouseOver Link OnMouseOut Link OnSelect text, textarea OnSubmit Form OnUnload window
?o?n codes sau ?�y gi�p b?n l?y gi� tr? c?a bi?n s? d?ng ph??ng ph�p window.prompt<script language=javascript>
<!--
beginPhrase = "Xin ch�o ";
YourName = window.prompt("Vui l�ng nh?p T�n c?a b?n:"," ");
Welcome = ". Welcom to it2know.net.";
finalPhrase = beginPhrase + YourName + Welcome ;
window.document.write(finalPhrase);
//-->
</script>
Ch? vi?c ch�n ?o?n codes v�o n?i b?n c?n vi?t gi� tr? chu?i ra.
Ch�p ?o?n Code tr�n V�O ?�Y ?? xem k?t qu?
| <SCRIPT> <!-- function doBlink() { var blink = document.all.tags("BLINK") for (var i=0; i </SCRIPT> |
Copy v� d�n ?o?n codes v�o ph?n <head></head> c?a trang HTML, v� trong ph?n BODY, ?? c� d�ng ch? nh?p nh�y, ch? vi?c vi?t:
?o?n codes gi�p b?n t?o d�ng ch? ch?y ? thanh t�nh tr?ng (status bar) n?m b�n d??i tr�nh duy?t.
Copy v� d�n ?o?n codes v�o ph?n <Head></head> c?a trang HTML. ??t ?o?n codes cho th? <BODY> nh? b�n d??i:
<BODY bgcolor="white"onLoad="timerONE=window.setTimeout('scrollit_r2l(10)',100);">
D�ng ch? ch?y ???c l?u trong c�c bi?n n�y:
var m1 = " D�NG CH? TH? NH?T ";
var m2 = "D�NG CH? TH? HAI";
var m3 = "D�NG CH? TH? BA";
Ch? vi?c thay ??i c�c d�ng ?� b?ng nh?ng d�ng ch? c?a b?n.