相關(guān)資訊
- 《戰(zhàn)國(guó)無(wú)雙4-2》怎么換服裝?戰(zhàn)國(guó)無(wú)
- 關(guān)于責(zé)任的名言警句大全
- 《戰(zhàn)國(guó)無(wú)雙4-2》PC版如何聯(lián)機(jī)? 戰(zhàn)
- 戰(zhàn)國(guó)無(wú)雙4-2技能覺(jué)醒牛逼嗎 全新
- 《戰(zhàn)國(guó)無(wú)雙4-2》手柄無(wú)效怎么解決
- 戰(zhàn)國(guó)無(wú)雙4-2如何設(shè)置語(yǔ)言 戰(zhàn)國(guó)無(wú)雙
- 戰(zhàn)國(guó)無(wú)雙4-2怎么樣跳過(guò)進(jìn)入開(kāi)場(chǎng)動(dòng)畫(huà)
- 什么是應(yīng)屆生畢業(yè)生
- 應(yīng)屆生簡(jiǎn)歷自我評(píng)價(jià)
- 應(yīng)屆生簡(jiǎn)歷怎么寫(xiě)
本類(lèi)常用軟件
-
福建農(nóng)村信用社手機(jī)銀行客戶(hù)端下載下載量:584204
-
Windows優(yōu)化大師下載量:416896
-
90美女秀(視頻聊天軟件)下載量:366961
-
廣西農(nóng)村信用社手機(jī)銀行客戶(hù)端下載下載量:365699
-
快播手機(jī)版下載量:325855
本文章將給大家用最精簡(jiǎn)的實(shí)例介紹到實(shí)現(xiàn)顯示、添加、修改、刪除的比較簡(jiǎn)便的方法,雖然是以ASP例子介紹,但同樣適用于PHP、JSP、ASP.NET等語(yǔ)言。文中用到了java script腳本,大家也可從中學(xué)到部分的java script知識(shí),希望給大家?guī)?lái)幫助和啟發(fā)。
因?yàn)榻鼛滋旃ぷ魈,就不祥?xì)的寫(xiě)原理文章了,我在代碼里面寫(xiě)的注釋非常清楚,相信懂點(diǎn)網(wǎng)頁(yè)編程的朋友應(yīng)該都能夠看懂。
聲明:希望各位朋友轉(zhuǎn)載時(shí),不要把原有作者版權(quán)去掉,謝謝合作。
由于本人寫(xiě)作水平有限,寫(xiě)的不好的地方,還請(qǐng)大家多多包涵,如要批評(píng)或意見(jiàn),請(qǐng)加我QQ。
以下正文開(kāi)始:
功能介紹:
平時(shí)很多人至少需要做三個(gè)表單才能實(shí)現(xiàn)數(shù)據(jù)的顯示、添加、修改、刪除,本文講的是只用一個(gè)添加表單,就可實(shí)現(xiàn)數(shù)據(jù)的顯示、添加、修改、刪除功能。用此方法寫(xiě)程序時(shí),至少可以省兩倍的精力。
實(shí)現(xiàn)例子:
例子說(shuō)明:
數(shù)據(jù)庫(kù)名:db.mdb
表名:pub_doc
字段:id和memo1-----memo30 (多少個(gè)字段大家可以自己設(shè)置)
公用部分:
公用部分,可以存成單個(gè)文件,用include調(diào)用。
<%
'數(shù)據(jù)庫(kù)連接
db="db.mdb"
set conn=server.createobject("adodb.connection")
conn.open "provider=microsoft.jet.oledb.4.0;data source="& server.mappath(db)
'發(fā)布時(shí)間:2005年12月30日 晚
'文章作者:翟振凱 (小琦)
'個(gè)人站:http://www.xiaoqi.net
'技術(shù)站:http://www.iisvs.net
'商業(yè)站:http://www.iisvs.com
'論壇站:http://www.tdqy.com
'QQ:53353866 22336848
'添加數(shù)據(jù)函數(shù)
function add_form(sl)'添加數(shù)據(jù)函數(shù)
'response.write request("act")
'response.end
if request("act")="add_save" and sl<>"" then
'------------------生成SQL語(yǔ)句----------------小琦
'--------循環(huán)memo
For i = 1 To sl
zd_name=zd_name&"memo"&i&","
if request("memo"&i&"")<>"" then
zd_value=zd_value&"'"&request("memo"&i&"")&"'"&","
else
zd_value=zd_value&"'"&null&"'"&","
end if
next
'//--------循環(huán)memo
'去掉最后的“,”
zd_name=left(zd_name,len(zd_name)-1)
zd_value=left(zd_value,len(zd_value)-1)
sql="Insert into pub_doc("&zd_name&")values("&zd_value&")"
'//------------------生成SQL語(yǔ)句----------------小琦
conn.execute(sql)
response.write "添加數(shù)據(jù)成功!"
Response.End
end if
end function
'修改數(shù)據(jù)函數(shù)
function edit_form(sl)'修改數(shù)據(jù)函數(shù)
if request("id")<>"" and request("act")="edit_save" and sl<>"" then
'------------------生成SQL語(yǔ)句----------------小琦
'--------循環(huán)memo
zd_name="" '字段名子
For i = 1 To sl
zd_name=zd_name&"memo"&i&"="
if request("memo"&i&"")<>"" then
zd_name=zd_name&"'"&request("memo"&i&"")&"'"&","
else
zd_name=zd_name&"'"&null&"'"&","
end if
next
'//--------循環(huán)memo
'去掉最后的“,”
zd_name=left(zd_name,len(zd_name)-1)
'//------------------生成SQL語(yǔ)句----------------小琦
sql="Update pub_doc set "&zd_name&" where id="&request("id")&""
conn.execute(sql)
response.write "修改成功!"
Response.End()
end if
end function
function read_edit_form(sl)'修改前讀取數(shù)據(jù)函數(shù)
if request("id")<>"" and request("act")="edit" and sl<>"" then'讀取要修改的數(shù)據(jù)
'------------------生成SQL語(yǔ)句----------------小琦
zd_name="" '字段名子
'--------循環(huán)memo
For i = 1 To sl
zd_name=zd_name&"memo"&i&","
next
'//--------循環(huán)memo
'去掉最后的“,”
zd_name=left(zd_name,len(zd_name)-1)
sql="select top 1 "&zd_name&" from pub_doc where id="&request("id")&""
'//------------------生成SQL語(yǔ)句----------------小琦
set rs = conn.execute(sql)'查詢(xún)
response.write "<script language=java script>"'寫(xiě)java script的腳本
response.write "function read_data(){"'
'--------循環(huán)賦值
for each i in split(zd_name,",")
response.write "frm."&i&".value="""&Replace(Replace(Replace(rs(i)&"||",chr(13),"\n"),chr(10),"\n"),"||","")&""";"'
next
'//--------循環(huán)賦值
response.write "}</script>"
end if
end function
function read_form(sl)'讀取數(shù)據(jù)函數(shù)
if request("id")<>"" and request("act")="" and sl<>"" then'讀取要查看的數(shù)據(jù)
'------------------生成SQL語(yǔ)句----------------小琦
'--------循環(huán)memo
For i = 1 To sl
zd_name=zd_name&"memo"&i&","
next
'//--------循環(huán)memo
'去掉最后的“,”
zd_name=left(zd_name,len(zd_name)-1)
sql="select top 1 "&zd_name&" from pub_doc where id="&request("id")&""
'//------------------生成SQL語(yǔ)句----------------小琦
set rs = conn.execute(sql)'查詢(xún)
response.write "<sc"+"ript language=java script>"'寫(xiě)java script的腳本
response.write "function read_data(){"'
'--------循環(huán)賦值
for each i in split(zd_name,",")
if rs(i)="" then kongge=" "'如果內(nèi)容為空,則用空格代替
response.write "frm."&i&".parentElement.innerText="""&rs(i)&kongge&""";"'
next
'//--------循環(huán)賦值
response.write "}</scr"+"ipt>"
end if
end function
function del()'刪除數(shù)據(jù)
if request("id")<>"" and request("act")="del" then
conn.execute("Delete from pub_doc where id="&request("id")&" ")
response.write "刪除成功!"
response.end
end if
end function
function xiaoqi_end()
if request("id")<>"" and request("act")="" then response.write "<sc"+"ript language=java script>frm.save.removeNode(true);read_data()</sc"+"ript>"
if request("id")<>"" and request("act")="edit" then response.write "<sc"+"ript language=java script>read_data()</sc"+"ript>"
session("act")=""
end function
function get_act(sl)
add_form sl
edit_form sl
read_edit_form sl
read_form sl
del
end function
'設(shè)置動(dòng)作
if request("act")="add" then session("act")="add_save"
if request("act")="edit" then session("act")="edit_save"
%>
調(diào)用實(shí)例
<%get_act("26")‘使用了26個(gè)字段%>
<% if request("act")<>"" or request("id")<>"" then’如果不是顯示列表頁(yè)面%>
<p> </p>
<form method="POST" action="index.asp" name="frm">
<div align="center">
<table border="1" width="600" id="table2" bordercolorlight="#000000" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" style="text-align: center">
<tr>
<td width="149">數(shù)據(jù)表</td>
<td width="149"><input type="text" name="memo1"></td>
<td width="150"><input type="text" name="memo2"></td>
<td width="150"><input type="text" name="memo3"></td>
</tr>
<tr>
<td width="149"><input type="text" name="memo4"></td>
<td width="149"><input type="text" name="memo7"></td>
<td width="150"><input type="text" name="memo6"></td>
<td width="150"><input type="text" name="memo5"></td>
</tr>
<tr>
<td width="149"><input type="text" name="memo9"></td>
<td width="149"><input type="text" name="memo10"></td>
<td width="150"><input type="text" name="memo11"></td>
<td width="150"><input type="text" name="memo12"></td>
</tr>
<tr>
<td width="149"><input type="text" name="memo16"></td>
<td width="149"><input type="text" name="memo15"></td>
<td width="150"><input type="text" name="memo14"></td>
<td width="150"><input type="text" name="memo13"></td>
</tr>
<tr>
<td width="149"><input type="text" name="memo17"></td>
<td width="149"><input type="text" name="memo18"></td>
<td width="150"><input type="text" name="memo19"></td>
<td width="150"><input type="text" name="memo20"></td>
</tr>
<tr>
<td width="149"><input type="text" name="memo24"></td>
<td width="149"><input type="text" name="memo23"></td>
<td width="150"><input type="text" name="memo22"></td>
<td width="150"><input type="text" name="memo21"></td>
</tr>
<tr>
<td width="149"><input type="text" name="memo25"></td>
<td width="149"><input type="text" name="memo26"></td>
<td width="150"><input type="text" name="memo8"></td>
<td width="150"><input type="submit" value="提交" name="save"></td>
</tr>
</table>
</div>
<input type="hidden" name="id" value="<%=request("id")%>">
<input type="hidden" name="act" value="<%=session("act")%>">
</form>
<p> </p>
<%
xiaoqi_end()
else%>
<p align="center"><a href="?act=add">添加</a></p>
<div align="center">
<table border="1" width="600" id="table1" bordercolorlight="#000000" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" style="text-align: center">
<tr>
<td width="142">字段1</td>
<td width="142">字段2</td>
<td width="143">字段3</td>
<td width="63">查看</td>
<td width="63">修改</td>
<td width="63">刪除</td>
</tr>
<%
set rs=server.createobject("adodb.recordset")
sql = "select * from pub_doc order by id desc "
rs.open sql,conn,1,1
do while not rs.eof
%>
<tr>
<td width="142"> <%=rs("memo1")%></td>
<td width="142"> <%=rs("memo2")%></td>
<td width="143"> <%=rs("memo3")%></td>
<td width="63"><a href="?id=<%=rs("id")%>">查看</a></td>
<td width="63"><a href="?id=<%=rs("id")%>&act=edit">修改</a></td>
<td width="63"><a href="?id=<%=rs("id")%>&act=del">刪除</a></td>
</tr>
<%
rs.MoveNext
loop %>
</table>
</div>
<%end if%>
再給大家一個(gè)我原創(chuàng)的用java script將文本框轉(zhuǎn)換為文本的例子,相信很多人都會(huì)用到。大家存成html文件打開(kāi)即可看到效果。
原理:從文本框中取值,把值賦于文本框的父對(duì)象。
<script language=java script>
function read_data(){
frm.memo1.parentElement.innerText="1";
frm.memo2.parentElement.innerText="2";
frm.memo3.parentElement.innerText="3";
}</script>
<p> </p>
<form method="POST" action="index.asp" name="frm">
<div align="center">
<table border="1" width="600" bordercolorlight="#000000" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" style="text-align: center">
<tr>
<td width="149">數(shù)據(jù)表</td>
<td width="149"><input type="text" name="memo1"></td>
<td width="150"><input type="text" name="memo2"></td>
<td width="150"><input type="text" name="memo3"></td>
</tr>
</table>
<p><input type="submit" value="提交" name="save"></div>
</form>
<script language=java script>frm.save.removeNode(true);read_data()</script>