50 bài tập ASP cho người mới bắt đầu

1. Ghi dòng text ra trang web

 

<html>

<body>

 <% response.write("Hello World!")%>

</body>

</html>

 

2. Định dạng text kết hợp với các thẻ html<html>

<body>

<% response.write("<h2>You can use HTML tags to format the text!</h2>") %>

<%

response.write("<p style='color:#0000ff'>This text is styled with the style attribute!</p>")

%>

</body>

</html>

 

doc11 trang | Chuyên mục: ASP | Chia sẻ: dkS00TYs | Lượt xem: 2035 | Lượt tải: 4download
Tóm tắt nội dung 50 bài tập ASP cho người mới bắt đầu, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
dim fname
fname=Request.Form("fname")
If fname"" Then
 Response.Write("Hello " & fname & "!")
 Response.Write("How are you today?")
End If
%>
26. Tương tác với người dùng dùng trên form sử dụng Option radio
<%
dim cars
cars=Request.Form("cars")
%>
Please select your favorite car:
<input type="radio" name="cars"
value="Volvo">Volvo
<input type="radio" name="cars"
value="Saab">Saab
<input type="radio" name="cars"
value="BMW">BMW
<%
if cars"" then
 Response.Write("Your favorite car is: " & cars & "")
end if
%>
27. Sử dụng cookies để đếm số lần truy cập vào website
<%
dim numvisits
response.cookies("NumVisits").Expires=date+365
numvisits=request.cookies("NumVisits")
if numvisits="" then
 response.cookies("NumVisits")=1
 response.write("Welcome! This is the first time you are visiting this Web page.")
else
 response.cookies("NumVisits")=numvisits+1
 response.write("You have visited this ")
 response.write("Web page " & numvisits)
 if numvisits=1 then
 response.write " time before!"
 else
 response.write " times before!"
 end if
end if
%>
28. Hướng người dùng đến các form khác nhau khi người dùng lựa chọn hướng sử dụng
<%
if Request.Form("select")"" then
 Response.Redirect(Request.Form("select"))
end if
%> 
<input type="radio" name="select"
value="demo_server.asp">
Server Example
<input type="radio" name="select"
value="demo_text.asp">
Text Example
29. Xử lý thông tin của form ngay tại form
Your name: 
<%
dim fname
fname=Request.QueryString("fname")
If fname"" Then
 Response.Write("Hello " & fname & "!")
 Response.Write("How are you today?")
End If
%>
30. Lấy thông tin từ máy chủ
You are browsing this site with:
Your IP address is:
The DNS lookup of the IP address is:
The method used to call the page:
The server's domain name:
The server's port:
The server's software:
31. Lấy thông tin từ máy chủ
All possible server variables:
<%
For Each Item in Request.ServerVariables
 Response.Write(Item & "")
Next
%>
32. Kiểm tra xem bạn đăng nhập trang web lần đầu tiên hay bao nhiêu?
<%
dim numvisits
response.cookies("NumVisits").Expires=date+365
numvisits=request.cookies("NumVisits")
if numvisits="" then
 response.cookies("NumVisits")=1
 response.write("Welcome! This is the first time you are visiting this Web page.")
else
 response.cookies("NumVisits")=numvisits+1
 response.write("You have visited this ")
 response.write("Web page " & numvisits)
 if numvisits=1 then
 response.write " time before!"
 else
 response.write " times before!"
 end if
end if
%>
33. Kiểm tra xem bạn gửi 1 thông tin đến máy chủ hết bao nhiêu bộ nhớ (đơn vị tính bytes)
Please type something:
<%
If Request.Form("txt")"" Then
 Response.Write("You submitted: ")
 Response.Write(Request.Form)
 Response.Write("")
 Response.Write("Total bytes: ")
 Response.Write(Request.Totalbytes)
End If
%>
34. Kiểm tra xem 1 file nào đó bị thay đổi lần cuối là khi nào?
<%
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set rs = fs.GetFile(Server.MapPath("demo_lastmodified.asp"))
modified = rs.DateLastModified
%>
This file was last modified on: <%response.write(modified)
Set rs = Nothing
Set fs = Nothing
%>
35. Mở 1 file text để đọc nội dung
<%
Set FS = Server.CreateObject("Scripting.FileSystemObject")
Set RS = FS.OpenTextFile(Server.MapPath("text") & "\TextFile.txt",1)
While not rs.AtEndOfStream
 Response.Write RS.ReadLine
 Response.Write("")
Wend
%>
36. Kiểm tra xem 1 file nào đó được mở ra bao nhiêu lần
<%
Set FS=Server.CreateObject("Scripting.FileSystemObject")
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 1, False)
fcount=RS.ReadLine
RS.Close
fcount=fcount+1
'This code is disabled due to the write access security on our server:
'Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 2, False)
'RS.Write fcount
'RS.Close
Set RS=Nothing
Set FS=Nothing
%>
This page has been visited times.
37. Kiểm tra xem 1 tên file nào đó kể cả đường dẫn có tồn tại hay không?
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
If (fs.FileExists("c:\winnt\cursors\3dgarro.cur"))=true Then
 Response.Write("File c:\winnt\cursors\3dgarro.cur exists.")
Else
 Response.Write("File c:\winnt\cursors\3dgarro.cur does not exist.")
End If
set fs=nothing
%>
38. Kiểm tra xem 1 thư mục nào đó có tồn tại thực sự hay không?
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
If fs.FolderExists("c:\temp") = true Then
 Response.Write("Folder c:\temp exists.")
Else
 Response.Write("Folder c:\temp does not exist.")
End If
set fs=nothing
%>
39. Kiểm tra xem 1 ổ đĩa có tồn tại hay không?
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.driveexists("c:") = true then
 Response.Write("Drive c: exists.")
Else
 Response.Write("Drive c: does not exist.")
End If
Response.write("")
if fs.driveexists("g:") = true then
 Response.Write("Drive g: exists.")
Else
 Response.Write("Drive g: does not exist.")
End If
set fs=nothing
%>
40. Lấy ra tên thư mục của đường dẫn đã cho
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.GetParentFolderName("c:\winnt\cursors\3dgarro.cur")
Response.Write("The parent folder name of c:\winnt\cursors\3dgarro.cur is: " & p)
set fs=nothing
%>
41. Lấy ra phần mở rộng của 1 tên file
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Response.Write("The file extension of the file 3dgarro is: ")
Response.Write(fs.GetExtensionName("c:\winnt\cursors\3dgarro.cur"))
set fs=nothing
%>
Chủ đề về text
42. Mở và đọc 1 file Text
This is the text in the text file:
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
Response.Write(f.ReadAll)
f.Close
Set f=Nothing
Set fs=Nothing
%>
43.Lấy ra mấy ký tự từ 1 file text
This is the first five characters from the text file:
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
Response.Write(f.Read(5))
f.Close
Set f=Nothing
Set fs=Nothing
%>
44.Đọc ra 1 dòng của 1 file text
This is the first line of the text file:
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
Response.Write(f.ReadLine)
f.Close
Set f=Nothing
Set fs=Nothing
%>
45. Đọc tất cả các dòng của 1 file Text
This is all the lines in the text file:
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
do while f.AtEndOfStream = false
Response.Write(f.ReadLine)
Response.Write("")
loop
f.Close
Set f=Nothing
Set fs=Nothing
%>
46. Bỏ qua 1 phần nào đó của file Text
The first four characters in the text file are skipped:
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
f.Skip(4)
Response.Write(f.ReadAll)
f.Close
Set f=Nothing
Set fs=Nothing
%>
47. File được tạo ra lúc nào
<%
dim fs, f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.GetFile(Server.MapPath("testread.txt"))
Response.Write("The file testread.txt was created on: " & f.DateCreated)
set f=nothing
set fs=nothing
%>
Phần 2: ASP cơ sở dữ liệu
1. File modulieu.asp
<%
Set Conn = Server.CreateObject("ADODB.Connection")
DSNStatement = "DRIVER=Microsoft Access Driver (*.mdb);DBQ="
DSNStatement = DSNStatement & Server.MapPath("/t36/database/thanhvien.mdb")
Conn.Open DSNStatement
%>
2. Duyệt dữ liệu
<%
Select case weekday(date())
case 1
thu = "Chủ Nhật"
case 2
thu="Thứ Hai"
case 3
thu="Thứ Ba"
case 4
thu="Thứ Tư"
case 5
thu="Thứ Năm"
case 6
thu="Thứ Sáu"
case 7
thu="Thứ Bảy"
end Select
ngay=day(date())
thang=month(date())
nam=year(date())
Response.Write Thu & ", Ngày: " & cstr(ngay) & " Tháng " & cstr(thang) & " Nam " & cstr(Nam) & "" & ""
%>
<%
Dim Conn, RS 'Declares the Conn (Connection) and RS (Recordset) variables
Set RS = Server.CreateObject("ADODB.Recordset")
SQLStatement = "SELECT * FROM hoidap"
RS.Open SQLStatement, Conn, 3, 3
rs.movefirst
Response.Write "" & "" & "" & "Danh sách các câu hỏi thảo luận" & "" & "" & ""
kt=0
Response.Write ""
Do while not rs.eof
Response.write ""
if kt=1 then
Response.write ""
kt=0
else
Response.write ""
kt=1
end if
Response.write ""
Response.write "" & "Nguoi gui :" & "" & rs("FromUser") & ""
Response.write "Thời gian :" & rs("ngay") & " - " & rs("thoigian") & ""
Response.write "Chủ đề :" & rs("Loai") & ""
Response.write "Nội dung :" & rs("NoiDung") & ""
Response.write ""
Response.write ""
Response.write ""
rs.moveNext
Loop
Response.write ""
%>
 
3. Sửa dữ liệu
<%
Select case weekday(date())
case 1
thu = "Chủ Nhật"
case 2
thu="Thứ Hai"
case 3
thu="Thứ Ba"
case 4
thu="Thứ Tư"
case 5
thu="Thứ Năm"
case 6
thu="Thứ Sáu"
case 7
thu="Thứ Bảy"
end Select
ngay=day(date())
thang=month(date())
nam=year(date())

Response.Write Thu & ", Ngày: " & cstr(ngay) & " Tháng " & cstr(thang) & " Nam " & cstr(Nam) & "" & ""
%>
<%
bTenDangNhap=Request("TenDangNhap")
bhoten=request("hoten")
bmatkhau=request("matkhau")
bdiachi=request("diachi")
bdienthoai=request("dienthoai")
bemail=request("email")
Set RS = Server.CreateObject("ADODB.Recordset")
SQLStatement = "SELECT * FROM hoso WHERE TenDangNhap='"& bTenDangNhap &"' and MatKhau='"& bMatKhau &"'"
RS.Open SQLStatement, Conn, 3, 3
rs.MoveFirst
rs.Fields("TenDangNhap")=btenDangNhap
rs.Fields("hoten")=bhoten
rs.Fields("matkhau")=bmatkhau
rs.Fields("diachi")=bdiachi
rs.Fields("dienthoai")=bdienthoai
rs.Fields("email")=bemail
rs.Update
rs.Close
set rs=nothing
Response.Write "Ban da sua doi thong tin trong profile thanh cong"
%>

File đính kèm:

  • doc50 bài tập ASP cho người mới bắt đầu.doc
Tài liệu liên quan