shopa_editdisplay.asp
来自「ASP+SQL Server动态网站开发从基础到实践教程」· ASP 代码 · 共 288 行
ASP
288 行
<%option explicit%>
<!-- #include file="shop$db.asp" -->
<!-- #include file="shopa_Tools.asp" -->
<%
ShopCheckAdmin
'**************************************************************************
'
dim mysql
Dim Fieldcount
Dim Headnames(6)
Dim ProcType
Dim SortType
Dim Sortfield
Dim SortUpDown
Dim Sortupdownnames(2)
Dim Sortupdownvalues(2)
Dim Sortupdowncount
Dim Procnames(3)
dim Procvalues(3)
Dim Fieldnames
Dim Fieldnamecount
Dim DisplayFields
Dim displayFieldCount
Dim DisplayField
Dim Idfield
Dim SelectField
Dim SelectValue
Dim maxfields
Dim i
Dim item
dim dbtable
Dim scriptresponder
dim fieldname
Dim rstemp
Dim dbc
const cSelect="Select"
AdminPageHeader ' 显示管理员页面头
ShopOpenDatabase dbc ' 打开数据库
maxfields=6
GetInput ' 获得所有的表单域
If Request("Delete")<>"" Then
For each item in Request("DeleteUser")
DeleteRecord Item
Next
End if
GenerateDisplayHeader ' 产生排序按钮等
scriptresponder="shopa_editrecord.asp"
if ucase(dbtable)="PRODUCTS" then
scriptresponder="shopa_addproduct.asp"
end if
if ucase(dbtable)="SUBCATEGORIES" then
scriptresponder="shopa_addsubcategory.asp"
end if
ShopopenRecordSet mysql, rstemp, mypagesize, mypage
GenerateTable ' 输出表格
Call PageNavBar (Mysql) ' 显示页面导航栏
rsTemp.close ' 关闭数据库
ShopCloseDatabase dbc
AdminPageTrailer ' 输出管理员页面尾
'
Sub GetTableName
dbtable=request.querystring("table")
if dbtable="" then
dbtable=request("table")
end if
Response.write "数据库表名=" & dbtable & "<br>"
end sub
Sub GetInput
'获得需要显示第几页的记录,主要是记录较多时分页显示
mypage = Request.querystring("page")
sortfield=request("Sortfield")
' 排序
If Sortfield="" or Sortfield=cSelect then
sortfield=IdField
end if
SelectValue=request("Selectvalue")
SelectField=request("selectField")
If SelectField=cSelect then
selectvalue=""
end if
SortUpdown=request("SortUpdown")
If SortUpdown="" then
sortupdown="ASC"
end if
if mypage="" then
GetTableName
SetFieldNames ' 获得字段名
GetDisplayfields
mypage=1
GenerateSQL
else
Mysql=Session("sqlquery")
Fieldnames=session("Fieldnames")
sortfield=Session("sortfield")
sortupdown=Session("sortupdown")
IDfield=Session("IDfield")
Fieldcount=Session("Fieldcount")
dbtable=Session("table")
DisplayFields=Session("DisplayFields")
DisplayFieldCount=Session("DisplayFieldCount")
end if
maxrecs=10
mypagesize=maxrecs
SetUpDown
end sub
'
' 通过表单的设定字段产生SQL语句
Sub GenerateSQL
dim sqlproc
dim key
MySql = "SELECT * from " & dbtable
if Selectvalue<> "" then
key = SelectValue & "%"
mySQL = MySQL & " where " & SelectField & " like '" & key & "'"
end if
If sortfield<>"" then
mysql=mysql & " order by " & sortfield & " " & sortupdown
end if
Session("sqlquery")=MySQL
Session("sortfield")=sortfield
Session("sortupdown")=sortupdown
End sub
'
Sub GenerateTable()
dim howmanyfields
dim my_link
Dim howmanyrecs
Dim ArrayFields
Dim fieldvalue
Session("Table")=dbtable
Session("Dbname")=dbname
Session("Idfield")=idfield
Session("Fieldcount")=fieldcount
If DisplayFieldcount> 0 then
howmanyfields=DisplayFieldCount-1
ArrayFields=DisplayFields
else
howmanyfields=fieldcount-1
ArrayFields=Fieldnames
end if
if howmanyfields > maxfields then
howmanyfields = maxfields
end if
Response.write "<font size=4><a href=shopa_addgeneral.asp?database=" & dbname & "&table=" & dbtable & ">增加新记录</a></font><br>"
response.write "<br>Page " & mypage & " of " & maxpages & "<br>"
%>
<center>
<form action="shopa_editdisplay.asp?table=<%=dbtable%>" method="post">
<table border="2" cellpadding="2" cellspacing="3" width="100%">
<tr><td></td>
<%
for i=0 to howmanyfields
response.write "<td><b>" & ArrayFields(i) & "</b></td>"
next
Response.write "<td align=center>Delete</td></tr>"
' 下面获得所有记录
howmanyrecs=0
DO UNTIL rstemp.eof OR howmanyrecs=maxrecs
my_link=scriptresponder & "?which=" & rstemp(idfield) & "&idfield=" & idfield & "&table=" & dbtable & "&database=" & dbname
response.write "<tr><td valign=""top""><a HREF=" & my_link & ">编辑</a></font></td>"
for i = 0 to howmanyfields
If IsNull(rstemp(ArrayFields(i))) then
response.write ("<td> </td>")
else
response.write("<td BGCOLOR=""#c0c0c0"">" & rstemp(ArrayFields(i)) & "</td>")
end if
next
response.write "<td><center><input type=checkbox name=DeleteUser value=" & rstemp(idfield) & "></center></td></tr>"
howmanyrecs=howmanyrecs+1
if howmanyrecs < maxrecs then
rstemp.movenext
end if
loop
response.write("</table></font>")
%>
<input type="hidden" name="database" value="<%=database%>">
<input type="hidden" name="table" value="<%=dbtable%>">
<input type="hidden" name="idfield" value="<%=idfield%>">
<center><input type="submit" name="Delete" value="删除记录"></center></font>
<%
response.write("</form>")
'
end sub
Sub SetFieldNames
Fieldnamecount=0
dim fSql
dim rs
dim fldname
ReDim Fieldnames(100)
FSQL = "SELECT * FROM " & dbtable
Set rs = dbc.Execute(fSQL)
For each fldName in rs.Fields
Fieldnames(fieldcount)=fldName.Name
fieldcount=fieldcount+1
next
rs.close
Idfield=Fieldnames(0)
Session("Fieldnames")=Fieldnames
DisplayFields=Fieldnames
Displayfieldcount=fieldcount
Session("DisplayFields")=Displayfields
Session("DisplayFieldCount")=displayfieldCount
End Sub
Sub SetUpDown
Sortupdownnames(0)="Ascending"
Sortupdownnames(1)="Descending"
Sortupdownvalues(0)="ASC"
Sortupdownvalues(1)="DESC"
SortUpDowncount=2
end sub
Sub DeleteRecord(Item)
dim Rowsaffected
dbc.Execute "delete from " & dbtable & " where " & idfield & "=" & Item, RowsAffected, 1
end sub
Sub GenerateDisplayHeader
%>
<form action="shopa_editdisplay.asp?table=<%=dbtable%>" method="post">
<table border="0"<tr>
<td align="left">排序</td>
<td>
<%
GenerateSelectNV fieldnames,sortfield,"sortfield", fieldcount, CSelect
%>
</td><td align="left">
<%
GenerateSelectV Sortupdownnames,Sortupdownvalues,sortupdown,"sortupdown", sortupdowncount,""
%>
</td>
<tr><td align="left">特定记录</td>
<td align=left><input type="text" name="SelectValue" size="20"></td>
<td align="left">
<%
GenerateSelectNV Fieldnames,SelectField,"SelectField", fieldcount, cSelect
response.write "</td>"
%>
<tr><td align="left">选择一列显示</td>
<td align="left">
<%
Displayfield=""
GenerateSelectMult Fieldnames,fieldcount,DisplayFields,DisplayfieldCount,"DisplayFields","All"
%>
<td align=center><input type="submit" name="Sort" value="显示"></center></font>
<br><input type="reset"value="清空"></td></tr>
</table>
<%
end sub
Sub GetDisplayFields
dim i
Dim displayArray(100)
DisplayFieldCount = Request("DisplayFields").Count
if DisplayfieldCount=0 then
Session("Displayfieldcount")=displayfieldcount
exit sub
end if
displayField=Request("DisplayFields")
DisplayFields= Split(DisplayField, ", ", -1, 1)
If DisplayFields(0)="All" then
Displayfieldcount=0
end if
Session("DisplayFields")=DisplayFields
Session("Displayfieldcount")=displayfieldcount
end sub
%>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?