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

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

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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> 30. Lấy thông tin từ máy chủ <html> <body> <p> <b>You are browsing this site with:</b> < %Response.Write(Request.ServerVariables( "http_user_agent"))%> </p> <p> <b>Your IP address is:</b> < %Response.Write(Request.ServerVariables( "remote_addr"))%> </p> <p> <b>The DNS lookup of the IP address is:</b> < %Response.Write(Request.ServerVariables( "remote_host"))%> </p> <p> <b>The method used to call the page:</b> < %Response.Write(Request.ServerVariables( "request_method"))%> </p> <p> <b>The server's domain name:</b> < %Response.Write(Request.ServerVariables( "server_name"))%> </p> <p> <b>The server's port:</b> < %Response.Write(Request.ServerVariables( "server_port"))%> </p> <p> <b>The server's software:</b> < %Response.Write(Request.ServerVariables( "server_software"))%> </p> </body> </html> 31. Lấy thông tin từ máy chủ <html> <body> <p> All possible server variables: </p> <% For Each Item in Request.ServerVariables Response.Write(Item & "<br />") Next %> </body> </html> 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=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 %> <html> <body> </body> </html> 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) <html> <body> <form action="demo_totalbytes.asp" method="post"> Please type something: <input type="text" name="txt"><br><br> <input type="submit" value="Submit"> </form> <% If Request.Form("txt")<>"" Then Response.Write("You submitted: ") Response.Write(Request.Form) Response.Write("<br><br>") Response.Write("Total bytes: ") Response.Write(Request.Totalbytes) End If %> </body> </html> 34. Kiểm tra xem 1 file nào đó bị thay đổi lần cuối là khi nào? <html> <body> <% Set fs = Server.CreateObject("Scripting.FileSystemO bject") Set rs = fs.GetFile(Server.MapPath("demo_lastmodifi ed.asp")) modified = rs.DateLastModified %> This file was last modified on: < %response.write(modified) Set rs = Nothing Set fs = Nothing %> </body> </html> 35. Mở 1 file text để đọc nội dung <html> <body> <% Set FS = Server.CreateObject("Scripting.FileSystemO bject") Set RS = FS.OpenTextFile(Server.MapPath("text") & "\TextFile.txt",1) While not rs.AtEndOfStream Response.Write RS.ReadLine Response.Write("<br />") Wend %> <p> <a href="text/textfile.txt"><img border="0" src="/images/btn_view_text.gif"></a> </p> </body> </html> 36. Kiểm tra xem 1 file nào đó được mở ra bao nhiêu lần <% Set FS=Server.CreateObject("Scripting.FileSyst emObject") Set RS=FS.OpenTextFile(Server.MapPath("coun ter.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("coun ter.txt"), 2, False) 'RS.Write fcount 'RS.Close Set RS=Nothing Set FS=Nothing %> <html> <body> <p> This page has been visited <%=fcount%> times. </p> </body> </html> 37. Kiểm tra xem 1 tên file nào đó kể cả đường dẫn có tồn tại hay không? <html> <body> <% Set fs=Server.CreateObject("Scripting.FileSyste mObject") 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 %> </body> </html> 38. Kiểm tra xem 1 thư mục nào đó có tồn tại thực sự hay không? <html> <body> <% Set fs=Server.CreateObject("Scripting.FileSyste mObject") 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 %> </body> </html> 39. Kiểm tra xem 1 ổ đĩa có tồn tại hay không? <html> <body> <% Set fs=Server.CreateObject("Scripting.FileSyste mObject") if fs.driveexists("c:") = true then Response.Write("Drive c: exists.") Else Response.Write("Drive c: does not exist.") End If Response.write("<br>") if fs.driveexists("g:") = true then Response.Write("Drive g: exists.") Else Response.Write("Drive g: does not exist.") End If set fs=nothing %> </body> </html> 40. Lấy ra tên thư mục của đường dẫn đã cho <html> <body> <% Set fs=Server.CreateObject("Scripting.FileSyste mObject") p=fs.GetParentFolderName("c:\winnt\cursor s\3dgarro.cur") Response.Write("The parent folder name of c:\winnt\cursors\3dgarro.cur is: " & p) set fs=nothing %> </body> </html> 41. Lấy ra phần mở rộng của 1 tên file <html> <body> <% Set fs=Server.CreateObject("Scripting.FileSyste mObject") Response.Write("The file extension of the file 3dgarro is: ") Response.Write(fs.GetExtensionName("c:\w innt\cursors\3dgarro.cur")) set fs=nothing %> </body> </html> Chủ đề về text 42. Mở và đọc 1 file Text <html> <body> <p>This is the text in the text file:</p> <% Set fs=Server.CreateObject("Scripting.FileSyste mObject") Set f=fs.OpenTextFile(Server.MapPath("testrea d.txt"), 1) Response.Write(f.ReadAll) f.Close Set f=Nothing Set fs=Nothing %> </body> </html> 43.Lấy ra mấy ký tự từ 1 file text <html> <body> <p>This is the first five characters from the text file:</p> <% Set fs=Server.CreateObject("Scripting.FileSyste mObject") Set f=fs.OpenTextFile(Server.MapPath("testrea d.txt"), 1) Response.Write(f.Read(5)) f.Close Set f=Nothing Set fs=Nothing %> </body> </html> 44.Đọc ra 1 dòng của 1 file text <html> <body> <p>This is the first line of the text file:</p> <% Set fs=Server.CreateObject("Scripting.FileSyste mObject") Set f=fs.OpenTextFile(Server.MapPath("testrea d.txt"), 1) Response.Write(f.ReadLine) f.Close Set f=Nothing Set fs=Nothing %> </body> </html> 45. Đọc tất cả các dòng của 1 file Text <html> <body> <p>This is all the lines in the text file:</p> <% Set fs=Server.CreateObject("Scripting.FileSyste mObject") Set f=fs.OpenTextFile(Server.MapPath("testrea d.txt"), 1) do while f.AtEndOfStream = false Response.Write(f.ReadLine) Response.Write("<br>") loop f.Close Set f=Nothing Set fs=Nothing %> </body> </html> 46. Bỏ qua 1 phần nào đó của file Text <html> <body> <p>The first four characters in the text file are skipped:</p> <% Set fs=Server.CreateObject("Scripting.FileSyste mObject") Set f=fs.OpenTextFile(Server.MapPath("testrea d.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 <html> <body> <% dim fs, f set fs=Server.CreateObject("Scripting.FileSyste mObject") 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 %> </body> </html> 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 <%@Language=VBScript%> <!--#Include file="modulieu.asp"--> <HTML> <HEAD> <meta name="GENERATOR" content="Microsoft FrontPage 5.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <META content=en http-equiv=Content- Language> <meta http-equiv="Content-Type" content="text/html; charset=Unicode"> <TITLE></TITLE> </HEAD> <BODY> <font size=5> <% 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) & "<br>" & "<br>" %> </font> </center> <% 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 "<center>" & "<h3>" & "<font color=red>" & "Danh sách các câu hỏi thảo luận" & "</font>" & "</h3>" & "</center>" kt=0 Response.Write "<Table border=0>" Do while not rs.eof Response.write "<TR>" if kt=1 then Response.write "<TD bgcolor=Pink>" kt=0 else Response.write "<TD bgcolor=Yellow>" kt=1 [...]... Response.write Response.write Response.write rs.moveNext Loop Response.write "" "" "" "" %>   3 Sửa dữ liệu > . 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. 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

Ngày đăng: 19/09/2013, 09:10

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan