Tải bản đầy đủ (.doc) (12 trang)

50 bài tập ASP cho người mới học

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (51.9 KB, 12 trang )

50 bài tập ASP cho người mới bắt đầu
Bộ bài tập ASP cơ bản, gồm 50 bài, từng bước từng bước, tương đối dễ học. Mong các bạn sưu
tầm tiếp và cập nhật thêm, để bộ sưu tập này nhiều lên và hữu ích hơn.
---------------------------
Băng qua việc cài đặt trình IIS trong WINDOWS, tạo thư mục ViduASP và soạn thảo các bài
tập vào đó, Share thư mục ViduASP thành WEB bằng cách chọn WebSharing. Cuối cùng có
thể chạy bằng địa chỉ trên Address của IE là http://localhost/viduAsp/tenfile.asp

Phần I: ASP cơ bản
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>


3. Định nghĩa biến trong Asp
<html>
<body>
<%
dim name
name="Donald Duck"
response.write("My name is: " & name)
%>
</body>
</html>

4. Định nghĩa một dãy
<html>
<body>
<%
Dim famname(6),i
famname(1) = "Jan Egil"
famname(2) = "Tove"
famname(3) = "Hege"
famname(4) = "Stale"
famname(5) = "Kai Jim"
famname(6) = "Borge"
For i = 1 to 6
response.write(famname(i) & "<br />")
Next
%>
</body>
</html>
5. Ví dụ về vòng lặp<html>
<body>

<%
dim i
for i=1 to 6
response.write("<h" & i & ">Header " & i
& "</h" & i & ">")
next
%>
</body>
</html>
6. Câu lệnh If và hàm thời gian dạng
đơn giản<html>
<body>
<%
dim h
h=hour(now())
response.write("<p>" & now())
response.write(" (Norwegian Time) </p>")
If h<12 then
response.write("Good Morning!")
else
response.write("Good day!")
end if
%>
</body>
</html>
7. Sử dụng lồng JavaScript (đối tượng
Date trong JavaScript)
<%@ language="javascript" %>
<html>
<body>

<%
var d=new Date()
var h=d.getHours()
Response.Write("<p>")
Response.Write(d + " (Norwegian Time)")
Response.Write("</p>")
if (h<12)
{
Response.Write("Good Morning!")
}
else
{
Response.Write("Good day!")
}
%>
</body>
</html>
8. Date và Time trong VbScript
<html>
<body>
Today's date is: <%response.write(date())
%>.
<br>
The server's local time is: <
%response.write(time())%>.
</body>
</html>
9. Tính số ngày, tháng, năm từ năm
hiện tại cho đến năm 3000
<html>

<body>
<p>Countdown to year 3000:</p>
<p>
<%millennium=cdate("1/1/3000
00:00:00")%>
It is
<%response.write(DateDiff("yyyy", Now(),
millennium))%>
years to year 3000!
<br>
It is
<%response.write(DateDiff("m", Now(),
millennium))%>
months to year 3000!
<br>
It is
<%response.write(DateDiff("ww", Now(),
millennium))%>
weeks to year 3000!
<br>
It is
<%response.write(DateDiff("d", Now(),
millennium))%>
days to year 3000!
<br>
It is
<%response.write(DateDiff("h", Now(),
millennium))%>
hours to year 3000!
<br>

It is
<%response.write(DateDiff("n", Now(),
millennium))%>
minutes to year 3000!
<br>
It is
<%response.write(DateDiff("s", Now(),
millennium))%>
seconds to year 3000!
</p>
</body>
</html>
10. Tìm ra 1 ngày cụ thể sau ngày
hiện tại một số ngày nào đó (ví dụ 30
ngày)
<html>
<body>
<%
response.write(DateAdd("d",30,date()))
%>
<p>
Syntax for DateAdd:
DateAdd(interval,number,date). You can
use <b>DateAdd</b> to for example
calculate a date 30 days from today.
</p>
</body>
</html>
11. Định dạng ngày tháng theo các
dạng khác nhau. Ví dụ này rất cần

cho các ứng dụng web xử lý ngày
tháng như ngày sinh, ngày thi, ngày
lên lương…
<html>
<body>
<%
response.write(FormatDateTime(date(),vbg
eneraldate))
response.write("<br />")
response.write(FormatDateTime(date(),vblo
ngdate))
response.write("<br />")
response.write(FormatDateTime(date(),vbs
hortdate))
response.write("<br />")
response.write(FormatDateTime(now(),vblo
ngtime))
response.write("<br />")
response.write(FormatDateTime(now(),vbsh
orttime))
%>
<p>
Syntax for FormatDateTime:
FormatDateTime(date,namedformat).
</p>
</body>
</html>
12. Kiểm tra xem 1 hằng số dạng
ngày có phải là 1ngày đúng không.
<html>

<body>
<%
somedate="10/30/99"
response.write(IsDate(somedate))
%>
</body>
</html>
Các hàm về xâu ký tự
13. Hàm chuyển đổi chữ thường ßà
chữ hoa
<html>
<body>
<%
name = "Bill Gates"
response.write(ucase(name))
response.write("<br>")
response.write(lcase(name))
%>
</body>
</html>
14. Các hàm huỷ bỏ dấu cách khi làm
việc với xâu ký tự
<html>
<body>
<%
name = " W3Schools "
response.write("visit" & name &
"now<br />")
response.write("visit" & trim(name) &
"now<br />")

response.write("visit" & ltrim(name) &
"now<br />")
response.write("visit" & rtrim(name) &
"now")
%>
</body>
</html>
15. Đảo ngược xâu
<html>
<body>
<%
sometext = "Hello Everyone!"
response.write(strReverse(sometext))
%>
</body>
</html>
16. Làm tròn số như thế nào?
<html>
<body>
<%
i = 48.66776677
j = 48.3333333
response.write(Round(i))
response.write("<br>")
response.write(Round(j))
%>
</body>
</html>
17. Tạo ra một số ngẫu nhiên
<html>

<body>
<%
randomize()
response.write(rnd())
%>
</body>
</html>
18. Trích xâu con từ một xâu đã cho
<html>
<body>
<%
sometext="Welcome to this Web"
response.write(Left(sometext,5))
response.write("<br>")
response.write(Right(sometext,5))
%>
</body>
</html>
19. Thay thế một từ trong 1 câu
<html>
<body>
<%
sometext="Welcome to this Web!!"
response.write(Replace(sometext, "Web",
"Page"))
%>
</body>
</html>
20. Trích một xâu con từ một xâu
<html>

<body>
<%
sometext="Welcome to this Web!!"
response.write(Mid(sometext, 9, 2))
%>
</body>
</html>

Các lệnh liên quan đến thủ tục
chương trình con
21. Gọi thủ tục con viết theo kiểu
VbScript khi lập trình ASP
<html>
<head>
<%
sub vbproc(num1,num2)
response.write(num1*num2)
end sub
%>
</head>
<body>
<p>
You can call a procedure like this:
</p>
<p>
Result: <%call vbproc(3,4)%>
</p>
<p>
Or, like this:
</p>

<p>
Result: <%vbproc 3,4%>
</p>
</body>
</html>
22. Gọi 1 thủ tục viết bằng JavaScript
khi lập trình Asp
<%@ language="javascript" %>
<html>
<head>
<%
function jsproc(num1,num2)
{
Response.Write(num1*num2)
}
%>
</head>
<body>
<p>
Result: <%jsproc(3,4)%>
</p>
</body>
</html>
23. Một sự so sánh giữa hai cách gọi
chương trình con từ VbScript và
JavaScript
<html>
<head>
<%
sub vbproc(num1,num2)

Response.Write(num1*num2)
end sub
%>
<script language="javascript"
runat="server">
function jsproc(num1,num2)
{
Response.Write(num1*num2)
}
</script>
</head>
<body>
<p>Result: <%call vbproc(3,4)%></p>
<p>Result: <%call jsproc(3,4)%></p>
</body>
</html>
Các xử lý trên form
24. Kiểm tra giá trị của một ô dữ liệu
trên form
<html>
<body>
<form action="demo_reqquery.asp"
method="get">
Your name: <input type="text"
name="fname" size="20" />
<input type="submit" value="Submit" />
</form>
<%
dim fname
fname=Request.QueryString("fname")

If fname<>"" Then
Response.Write("Hello " & fname & "!
<br />")
Response.Write("How are you today?")
End If
%>
</body>
</html>
25. Lấy giá trị từ form ra biến
<html>
<body>
<form action="demo_simpleform.asp"
method="post">
Your name: <input type="text"
name="fname" size="20" />
<input type="submit" value="Submit" />
</form>
<%
dim fname
fname=Request.Form("fname")
If fname<>"" Then
Response.Write("Hello " & fname & "!
<br />")
Response.Write("How are you today?")
End If
%>
</body>
</html>
26. Tương tác với người dùng dùng
trên form sử dụng Option radio

<html>
<%
dim cars
cars=Request.Form("cars")
%>
<body>
<form action="demo_radiob.asp"
method="post">
<p>Please select your favorite car:</p>
<input type="radio" name="cars"
<%if cars="Volvo" then
Response.Write("checked")%>
value="Volvo">Volvo</input>
<br />
<input type="radio" name="cars"
<%if cars="Saab" then
Response.Write("checked")%>
value="Saab">Saab</input>
<br />
<input type="radio" name="cars"
<%if cars="BMW" then
Response.Write("checked")%>
value="BMW">BMW</input>
<br /><br />
<input type="submit" value="Submit" />
</form>
<%
if cars<>"" then
Response.Write("<p>Your favorite car is:
" & cars & "</p>")

end if
%>
</body>
</html>
27. Sử dụng cookies để đếm số lần
truy cập vào website
<html>
<body>
<%
dim numvisits
response.cookies("NumVisits").Expires=dat
e+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
%>

</body>
</html>
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
%>
<html>
<body>
<form action="demo_redirect.asp"
method="post">
<input type="radio" name="select"
value="demo_server.asp">
Server Example<br>
<input type="radio" name="select"
value="demo_text.asp">
Text Example<br><br>
<input type="submit" value="Go!">
</form>
</body>
</html>
29. Xử lý thông tin của form ngay tại
form
<html>
<body>
<form action="demo_reqquery.asp"

method="get">
Your name: <input type="text"
name="fname" size="20" />
<input type="submit" value="Submit" />
</form>
<%
dim fname
fname=Request.QueryString("fname")
If fname<>"" Then
Response.Write("Hello " & fname & "!
<br />")
Response.Write("How are you today?")
End If
%>
</body>
</html>

×