Anda di halaman 1dari 9

ASP Lab Guide 3 C0607L

90 pht tip Step by step


Hng dn lm DataGrid trong ASP
Buc 1: To CSDL Authors trong SQL Server To bng Authors Field 1: Au_ID (int, Primary Key, identity(1,1) - t ng tng) Field 2: Author (varchar(20)) Field 3: YearBorn (int) Sau nhp d liu vo trong CSDL Dng Query Analyser vit cu truy vn Bc 2: To th mc DataGrid trong wwwroot To site tn l DataGrid trong DreamWeaverMX (ASP Javascript, th mc gc l DataGrid) Bc 3: To cc file nh hng dn di y v lu vo th mc DataGrid File Connection.asp: Cha cc thng tin kt ni ti CSDL ca bn, v c incude vo cc trang cn li File Display.asp: Hin th d liu di dng bng File AddForm.asp: Hin th form thm d liu File Add.asp: File ny dng x l d liu t file AddForm.asp sau a d liu vo trong CSDL File UpdateForm.asp: Hin th form cp nht d liu, hin th thng tin chi tit ca mt bn ghi t trang Display.asp File Update.asp: File ny dng x l d liu t file UpdateForm.asp sau cp nht d liu vo trong CSDL File DeleteSome.asp: File ny dng xa nhiu bn ghi da vo s nh du trong checkbox File DeleteOne.asp: File ny dng xa mt bn ghi trn tng dng

AddForm.asp

DeleteOne.asp UpdateForm.asp

DeleteSome.asp

1. Kt ni ti CSDL (Connection.asp) -u tin to bn phi to file Connection.asp -File ny cha tt c cc thng tin kt ni ti CSDL ca bn. -Cc file khc trong ng dng ca bn phi include ti file Connection.asp to ra s kt ni ti CSDL -Chng ta s dng connection ti SQL Server, cc bn c th nghin cu thm v vic dng Access Ni dung ca file Connection.asp nh sau: <!-- Creating Connection To Database --> <% var oConn; // Khi to i tng kt ni v to kt ni ADO ti CSDL SQL Server Authors

oConn = Server.CreateObject("ADODB.Connection"); oConn.Open("Provider=SQLOLEDB;Server=ten_server;Database=authors;UID=sa;PWD=sa"); // Khi to i tng kt ni v to kt ni ADO ti CSDL Access Authors // nh x CSDL Access authors thnh mt ng dn vt l //var filePath; //filePath = Server.MapPath("authors.mdb"); //oConn = Server.CreateObject("ADODB.Connection"); //oConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +filePath);

%>

2.Hin th d liu (Display.asp) -t tn file l Display.asp -File ny hin th tt c d liu trong bng authors di dng v cung cp cc ng link cho php ta c th thm d liu, cp nht v xa d liu Ni dung ca file Display.asp nh sau:
<%@LANGUAGE="JavaScript"%> <!-- a ni dung file Connection.asp vo trong trang display --> <!--#include file="Connection.asp"--> <html> <head> <title>Display Record</title> </head> <SCRIPT LANGUAGE="JavaScript"> //Hm ny dng x l khi ta click vo nt Delete Some function DeleteLinks() { if (confirm("Are you sure to delete?")) { document.frmDelete.action = "DeleteSome.asp"; document.frmDelete.submit(); } } </SCRIPT> <body> <% // Khai bo mt i tng Recordset var oRs = Server.CreateObject("ADODB.Recordset"); //Khai bo bin cha cu truy vn v gn gi tr cho n var strSQL; strSQL = "SELECT * FROM authors"; //Thi hnh cu truy vn thng qua phng thc Execute ca i tng connection oConn //c include vo file Display.asp t file Connection.asp oRs = oConn.Execute(strSQL); %> <br> <p><a href="AddForm.asp">Add New</a> </p> <form name="frmDelete" method="post" action=""> <TABLE border = 1> <tr> <td>Author ID</td> <td>Author Name</td> <td>Year Born</td> <td colspan="3">&nbsp;</td> </tr> <% // Dng vng lp while hin th bn ghi, vng lp s duyt tt cc bn ghi trong recordset v a // vo thnh tng dng ca bng, thn vng lp l cc th <tr>, d liu ca cc trng c a vo // th <td> while (!oRs.eof) { %> <tr> <td> <%= oRs.fields('Au_ID') %> </td> <td> <%= oRs.fields('Author') %> <!-- To mt trng n lu gi tr ca author name trong form -- > <input name="hdAuName" type="hidden" id="hdAuName" value="<%= oRs.fields('Author') %>"> </td> <td> <%= oRs.fields('YearBorn') %> <!-- To mt trng n lu gi tr ca author birth date trong form -- >

<input name="hdAuBirth" type="hidden" id="hdAuBirth" value="<%= oRs.fields('YearBorn') %>"> </td> <td> <!-- To mt checkbox lu gi tr ca author id trong form -- > <input name="chkAuID" type="checkbox" id="chkAuID" value="<%= oRs.fields.item('Au_ID') %>"> <!-- To mt trng n lu gi tr ca author id trong form -- > <input name="hdAuID" type="hidden" id="hdAuID" value="<%= oRs.fields('Au_ID') %>"> </td> <td> <a href="DeleteOne.asp?vAuID=<%=oRs.fields('Au_ID')%>">Delete One </a> </td> <td> <a href="UpdateForm.asp?vAuID=<%= oRs.fields('Au_ID') %>">Update</a> </td> </tr> <% oRs.MoveNext(); } %> </TABLE> <p> <input type="button" name="Button" value="Delete Some" onClick="DeleteLinks();" <% if ((oRs.EOF) && (oRs.BOF)) { %>disabled<% } %>> (You must select the checkbox to delete more than one record)</p> </form> <% // ng kt ni CSDL va RecordSet oRs.Close(); oConn.Close(); %> </body> </html>

3.Thm d liu vo bng (AddForm.asp v Add.asp) -Thm d liu vo CSDL c thc hin qua hai file AddForm.asp v Add.asp -File AddForm.asp dng hin th giao din form cho php bn c th nhp gi liu v gi ti file Add.asp x l. -File Add.asp cha cc cu lnh asp dng x l gi liu gi t file AddForm.asp ti, c th n s a d liu bn va gi vo database
Ni dung ca file AddForm.asp nh sau:

<%@LANGUAGE="JavaScript"%> <html> <head> <title>Add Record</title> </head> <body> <h3 align="center">Insert Record</h3> <form action="Add.asp" method="post" name="frmAdd" id="frmAdd"> <table width="326" border="1" align="center"> <tr> <td width="136" nowrap>Author Name: </td> <td width="174"><input name="txtAuName" type="text" id="txtAuName" size="30"></td> </tr> <tr> <td>Year Born: </td> <td><input name="txtAuBirth" type="text" id="txtAuBirth" size="15"></td> </tr> <tr>

<td colspan="2" align="center"><input name="btnInsert" type="submit" id="btnInsert" value="Insert"> &nbsp; <input name="btnReset" type="reset" id="btnReset" value="Reset"></td> </tr> </table> </form> </body> </html>
Ni dung ca file Add.asp nh sau: <%@ LANGUAGE = JavaScript %> <!--#include file="Connection.asp"--> <% //Ly gi tr t trang AddForm.asp bng i tng Request var vAuName = Request("txtAuName"); var vAuBirth = Request("txtAuBirth"); var strSQL; // Thm bn ghi vo CSDL, chng ta dng phng thc Execute ca i tng Connection // thi hnh cu truy vn insert gi tr ly t trang AddForm vo bng Authors strSQL = "INSERT INTO Authors (Author, YearBorn) VALUES (' "+vAuName+ " ' "+","+vAuBirth+")"; oConn.Execute(strSQL); // Nu thm d liu thnnh cng th s chuyn hng sang trang Display.asp oConn.Close(); Response.Redirect("Display.asp"); %>

4.Cp nht d liu vo bng (UpdateForm.asp v Update.asp) -File UpdateForm.asp: Hin th form cp nht d liu, hin th thng tin chi tit ca mt bn ghi t trang Display.asp v gi ti file UpdateForm.asp x l. -File Update.asp cha cc cu lnh asp dng x l gi liu gi t file UpdateForm.asp ti v s cp nht s sa i ca bn ghi chi tit c hin th trn trang UpdateForm.asp Ni dung ca file UpdateForm.asp nh sau: <%@LANGUAGE="JavaScript"%> <!--#include file="Connection.asp"--> <html> <head> <title>Update Record Form</title> </head> <% // Ly gi tr vAuID trn form t trang Display ly ra thng tin chi tit ca mt author // thng qua ID ca author var AuID = Request.QueryString("vAuID"); // Thi hnh cu truy vn v ly kt qu chi tit da vo gi AuID ly trn v a vo mt // Recordset var oRs = Server.CreateObject("ADODB.Recordset"); strSQL = "SELECT * FROM authors WHERE Au_ID = " + AuID ; oRs = oConn.Execute(strSQL); %> <body> <h3 align="center">Update Record</h3> <form action="Update.asp" method="post" name="frmUpdate" id="frmUpdate"> <table width="326" border="1" align="center"> <tr> <td width="136" nowrap>Author Name:

<input name="AuID" type="hidden" id="AuID" value="<%= AuID %>"></td> <td width="174"><input name="txtAuName" type="text" id="txtAuName" value="<%= oRs.fields('Author') %>" size="30"></td> </tr> <tr> <td>Year Born: </td> <td><input name="txtAuBirth" type="text" id="txtAuBirth" value="<%= oRs.fields('YearBorn') %>" size="15"></td> </tr> <tr> <td colspan="2" align="center"><input name="btnUpdate" type="submit" id="btnUpdate" value="Update"> &nbsp; <input name="btnReset" type="reset" id="btnReset" value="Reset"></td> </tr> </table> </form> <% // ng kt ni CSDL v RecordSet oRs.Close(); oConn.Close(); %> </body> </html> Ni dung ca file Update.asp nh sau: <%@ LANGUAGE = JavaScript %> <!--#include file="Connection.asp"--> <% // Ly thng tin t trang UpdateForm.asp gi sang var vAuName = Request.Form("txtAuName"); var vAuBirth = Request.Form("txtAuBirth"); var vAuID = Request.Form("AuID"); var strSQL; //Cp nht bn ghi vo CSDL chng ta dng phng thc Execute ca i tng Connection // thi hnh cu truy vn update vo bng author, thng qua cc gi tr ta ly trn strSQL = "UPDATE authors SET Author = "+" ' "+vAuName+" ' "+", YearBorn = "+vAuBirth +" WHERE au_id = " + vAuID ; oConn.Execute(strSQL); // Nu cp nht thnh cng th s chuyn hng sang trang Display.asp oConn.Close(); Response.Redirect("Display.asp");

%>

5.Xa d liu t bng (DeleteOne.asp v DeleteSome.asp) -File DeleteOne.asp: File ny dng xa mt bn ghi trn tng dng ca da trn Au_ID ca bn ghi -File DeleteSome.asp: File ny dng xa nhiu bn ghi da vo s nh du trong checkbox, mi checkbox s lu tr mt Au_ID Ni dung ca file DeleteOne.asp nh sau: <%@ LANGUAGE = JavaScript %> <!--#include file="Connection.asp"--> <% // Ly gi tri vAuID truyn qua URL t trang Display.asp ti trang DeleteOne.asp

// lm iu kin xa bn ghi ca cu truy vn var AuID = Request.QueryString("vAuID"); var strSQL; // Xa bn ghi trong CSDL chng ta dng phng thc Execute ca i tng Connection // thi hnh cu truy vn Delete strSQL = "DELETE FROM Authors WHERE Au_ID = "+AuID; oConn.Execute(strSQL); // Nu xa thnh cng th s chuyn hng sang trang Display.asp oConn.Close(); Response.Redirect("Display.asp");

%>

Ni dung ca file DeleteSome.asp nh sau: <%@ LANGUAGE = JavaScript %> <!--#include file="Connection.asp"--> <% // Ly gi tri vAuID trong cc hp checkbox mnh nh vo t trang Display.asp ti trang // lm iu kin xa bn ghi ca cu truy vn var vAuID = Request.Form("chkAuID"); var strSQL; // Xa bn ghi trong CSDL chng ta dng phng thc Execute ca i tng Connection // thi hnh cu truy vn Delete strSQL = "DELETE FROM Authors WHERE Au_ID IN (" + vAuID + ")"; oConn.Execute(strSQL); // Nu xa thnh cng th s chuyn hng sang trang Display.asp oConn.Close(); Response.Redirect("Display.asp"); %>

30 pht tip Step by step


Vn dng CSDL trn v to bng Books Bng Books c cu trc nh sau: Book_ID int, Primary key, identity(1,1) Au_ID kha ngoi tham chiu ti bng Authors BookName nvarchar(50) To mt trang tm kim Sch theo tc gi

Anda mungkin juga menyukai