在ASP中为实现公司简介,页脚信息,联系我们信息之类的简单信息发布页面,我会建立一个数据表,后台依靠2个文件完成。例中数据表名为table_Content,文件为default.asp和edit.asp.
table_Content有3个字段分别为:Id,Title,Content.
Id为数字,设主键索引,Title为文本,Content为备注。
edit.asp为信息编辑文件.
edit.asp代码
<!--#include Virtual="/_Inc/config.asp" -->
'调用数据库连接类文件
<!--#include Virtual="/_Inc/include.asp" -->
'调用数据库连接字符串及网站基础信息文件
<!--#include Virtual="/Admin/CheckUid.asp"-->
'调用账号验证文件用于验证后台是否为管理员登入,否则跳到登陆页面
<!--#include Virtual="/Admin/eWebEditor/Include/DeCode.asp"-->
'调用eWebEditor,这东西相信不用介绍
<%
DB_Connect(StrConn)
'连接数据库
Dim sID
'sId为default.asp传的值
sId = Bint(Request.QueryString("Id"))
If isPostBack And Request.Form("editInfo")="editInfo" Then
Title = ReplaceText(Request.Form("Title"))
'ReplaceText为自定义函数用于将输入数据在保存进数据库前转换格式及其他
Content = ReplaceText(Request.Form("Content"))
ShowInSite = Bint(Request.Form("ShowInSite"))
Content=eWebEditor_DeCode(Content,"SCRIPT,OBJECT,CLASS,STYLE")
'脚本过滤,对象过滤,样式类过滤,样式过滤
Set Rs = Server.CreateObject("ADODB.Recordset")
Sql = "Select * from table_Content where Id=" & sId
Rs.Open Sql,oConn,1,3
If Rs.eof Then
Rs.AddNew
End If
Rs("Title") = Title
Rs("Content") = Content
Rs.update
Rs.close:Set Rs = Nothing
doAlert "保存成功!",Request.ServerVariables("HTTP_REFERER")
End IF
’如果数据不存在则添加,存在则修改
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<LINK href="../Style/style.css" rel=stylesheet type=text/css>
<title>信息添加</title>
</head>
<SCRIPT LANGUAGE="javascript">
<!--
function checkadd(theForm)
{
if(theForm.Title.value=="")
{
alert("*项必须填写!")
return false;
}
return true;
}
//-->
</SCRIPT>
<body>
<form name="form1" method="post" onSubmit="return checkadd(this)">
<TABLE width="100%" border="0" align=center cellpadding="0" cellspacing="1" class="tableBorder">
<tr>
<th height=25 colspan="2">信息添加</th>
</tr>
<TR ALIGN="center">
<TD>
<TABLE width="100%" border="0" cellpadding="5" cellspacing="1" bordercolorlight="#CEE7FF" bordercolordark="#CEE7FF" style="border-collapse: collapse">
<TR>
<TD class="Forumrow" width="6%">标题:</TD>
<TD width="94%" class="Forumrow">
<input name="Title" type="text" size="30" readonly="true">
</TD>
</TR>
<TR>
<TD class="Forumrow">内容:</TD>
<TD class="Forumrow"><textarea name="Content" id="Content" style="display:none"></textarea>
<iframe ID="eWebEditor1" src="/Admin/eWebEditor/ewebeditor.asp?id=Content&style=s_coolblue" frameborder="0" scrolling="no" width="800" HEIGHT="500"></iframe></TD>
’调用eWebEditor编辑器
</TR>
<TR>
<TD class="Forumrow" colspan="2" align="center">
<input type="hidden" name="editInfo" value="editInfo">
<input type="submit" value=" 保存 " name="addok"></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</form>
</body>
</html>
<SCRIPT language="VBSCRIPT" RUNAT="SERVER">
Function ShowInfo(sId)
WriteLn("<S"&"CRIPT LANGUAGE=JAVASCRIPT>")
WriteLn(" var oForm = form1;")
WriteLn(" with(oForm){")
Set rsObj = oConn.Execute( "SELECT * FROM table_Content WHERE Id=" & sId)
If not rsObj.Eof Then
WriteLn(" Title.value='" & Str4Js(rsObj.Fields.Item("Title").Value) & "';")
WriteLn(" Content.value='" & HTMLDecode(Str4Js(rsObj.Fields.Item("Content").Value)) & "';")
End If
WriteLn(" }")
WriteLn("</S"&"CRIPT>")
End Function
</SCRIPT>
<%
If CLng(sId)>0 Then
ShowInfo sId
End If
’当sId大于0时调用数据库中各字段的值为编辑框默认值
DB_DisConnect()
‘关闭数据库连接
%>
default.asp
<!--#include Virtual="/_Inc/config.asp" -->
<!--#include Virtual="/_Inc/include.asp" -->
<!--#include Virtual="/_Inc/function.asp" -->
<!--#include Virtual="/_Inc/classDbPager.asp" -->
<!--#include Virtual="/Admin/CheckUid.asp"-->
’其他如上,function.asp为一些自定函数文件,classDbPager.asp为分页类
<%
DB_Connect(StrConn)
Dim sKeyWord, sChoose
'//删除
Dim sDelId
sDelId = ReplaceText(Request.QueryString("delid"))
If Not isEmpty(sDelId) AND sDelId<>"0" AND Len(Trim(sDelId))>0 Then
sDelId = FormatNumSerial(sDelId)
If Request("Submit")="删" Then
'删除信息
aId = Split(sDelId,",")
For i=0 To Ubound(aId)
oConn.Execute( "DELETE * from table_Content WHERE ID ="& aId(i) )
Response.Write("<li>删除信息完成</li>") : Response.Flush()
Next
doAlert "删除成功", Request.ServerVariables("HTTP_REFERER")
End If
End If
iPageSize = 30
Dim oDbPager
Set oDbPager = New cc_db_Pager
Set oDbPager.ActiveConnection = oConn
oDbPager.TableName = "table_Content"
oDbPager.Fields = "*"
oDbPager.PKey = "ID"
oDbPager.OrderBy = "ID Asc"
Set aChoose = Request("Choose")
Set aMT = Request("MT")
Set aWord = Request("KeyWord")
For x = 1 To aChoose.Count
If aChoose(x)<>"" and aWord(x)<>"" then
If aMT(x) = "like" then
If Instr(aChoose(x),"[int]")<=0 then
oDbPager.AddCondition ""&aChoose(x)&" like '%"&aWord(x)&"%'"
End if
Else
If Instr(aChoose(x),"[int]")<=0 then
oDbPager.AddCondition ""&aChoose(x)&" "&aMT(x)&" '"&aWord(x)&"'"
Else
oDbPager.AddCondition ""&Replace(aChoose(x),"[int]","")&" "&aMT(x)&" "&aWord(x)&""
End if
End if
End if
Next
oDbPager.PageSize = iPageSize
oDbPager.Page = Request.QueryString("page")
iCurrPage = oDbPager.Page
Set oRs = oDbPager.Recordset
iRecCount = oDbPager.RecordCount
iPageCount = oDbPager.PageCount
oDbPager.Style = 5
sPager = oDbPager.Pager
'Response.Write(oDbPager.getsql)
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<LINK href="../Style/style.css" rel=stylesheet type=text/css>
<title>信息管理</title>
</head>
<script language="javascript">
function CheckAll(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
if (e.Name != "chkall"&&e.disabled==false)
e.checked = form.chkall.checked;
}
}
//添加成分
var i=1;
function addComponent(){
var Component = document.getElementById_x("templet");
var ComponentCopy = Component.cloneNode(true);
var showTemplet = document.getElementById_x("showTemplet");
showTemplet.insertAdjacentElement("beforeEnd",ComponentCopy);
if(++i==4){
//alert("已增加到极限,不可再加了,谢谢!");
document.getElementById_x("button1").disabled=true;
}
}
//减少成分
function delComponent(){
document.getElementById_x("button1").disabled=false;
var Component = document.getElementsByName("templet")
if(Component.length>1) Component[Component.length-1].removeNode(true);
if(--i<1)i=1;
}
</script>
<body>
<table width="98%" border="0" cellspacing="0" cellpadding="0" align=center class="tableBorder">
<tr>
<th colspan=2 height=25>索引选项</th>
</tr>
<Form name="form1" method="get" action="default.asp">
<tr>
<td width="7%" height=25 class="forumRowHighlight"><B>条件:</B></td>
<td width="93%" class="forumRowHighlight">
<DIV id="showTemplet">
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="templet">
<tr>
<td><Select name="Choose">
<Option value="">请选择</Option>
<Option value="Id">编号</Option>
<Option value="Title">标题</Option>
</Select>
<select name="MT" id="MT">
<option value="like" >模糊匹配[like]</option>
<option value="=">精确匹配[=]</option>
</select>
<input type = "Text" name = "Keyword" style="width:300px;"> </td>
</tr>
</table>
</DIV></td>
</tr>
<tr>
<td class="forumRowHighlight" colspan="2">
<input type="button" value="增加" onClick="addComponent()" id="button1" style="cursor:hand;">
<input type="button" value="删除" onClick="delComponent()" id="button2" style="cursor:hand;">
<input type="submit" value="立刻查找" id="submit" name="submit" style="cursor:hand;"> [<a href="javascript:location.reload()">刷新页面</a>]</td>
</tr>
</form>
</table>
’以上功能为删除查找功能
<br>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" class="tableBorder">
<form name="form2" method="get">
<tr>
<th height=25 colspan="2"><font color="#FFFFFF">全部列表</font></th>
<tr>
<tr>
<td colspan="2">
<TABLE width="100%" border="0" align="center" cellpadding="3" cellspacing="1" bordercolor="#111111" bordercolorlight="#D7EBFF" bordercolordark="#D7EBFF" style="border-collapse: collapse">
<TBODY>
<%
if oRs.eof then
response.Write"<br><br><div align='center'>暂无数据信息</div><br><br>"
else
%>
<TR height=25 style="background-color:#6699CC">
<TD width="7%" height="20" align="center" class=bodytitle><font color="#CC3300"><b>编号</b></font></TD>
<TD width="22%" height="20" align="center" class=bodytitle><font color="#CC3300"><b>标题</b></font></TD>
<TD width="6%" align="center" class=bodytitle><font color="#CC3300"><b>操作</b></font></TD>
<TD width="11%" align="center" class=bodytitle>
<input type="hidden" name="Submit" value="">
<input type="button" value="删除" onClick="{if(confirm('您确定执行的操作吗?')){this.form.Submit.value='删';this.form.submit();return true;}return false;}">
<INPUT type="checkbox" value="on" name="chkall" onClick="CheckAll(this.form)">全选</TD>
</TR>
<%
i=0
do while not oRs.eof
i=i+1
Title = oRs("Title")
Id = oRs("Id")
%>
<TR height="25">
<TD class=forumRow align="center"><%=iPageSize*(iCurrPage-1)+i%></td>
<TD class=forumRow><%=Title%></td>
<TD class=forumRow align="center"><%=Site%></td>
<TD class=forumRow align="center"><a href="edit.asp?ID=<%=Id%>">修改</a></td>
’将所选数据Id字段的值传给edit.asp
<TD class=forumRow align="center"><input type="checkbox" name="delId" value="<%=Id%>"></td>
</TR>
<%
oRs.moveNext()
Loop
End If
%>
’用循环将数据库信息一一列出
</TABLE>
</td>
</tr>
<tr>
<td height="40" class=forumRow align="left" valign="bottom" style="padding-left:20px"><%=odbPager.PageInfo%></td>
<td class=forumRow align="right" valign="bottom" style="padding-right:20px"><%=sPager%></td>
</tr>
</form>
</table>
</body>
</html>
<%
oRs.Close:Set oRs = Nothing
DB_DisConnect()
%>
前台显示页思路
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include Virtual="/_Inc/config.asp" -->
<!--#include Virtual="/_Inc/include.asp" -->
<%
DB_Connect(StrConn)
Dim sId
’sId值由导航链接传值
sId = Request.QueryString("sId")
Set Rs=Server.CreateObject("Adodb.Recordset")
Rs.Open "Select * from table_Content where Id="&sId,oConn,1,1
If Not Rs.Eof Then
Title = Rs("Title")
Content = HTMLDecode(Rs("Content"))
’HTMLDecode为自定义函数用于将数据库数据转换显示于前台
End If
’根据导航传值读取数据库所需项数据
Rs.Close:Set Rs=Nothing
%>
<html>
<head><title></title></head>
<body>
<!--#include file="top.asp"-->
'调用页首包括导航LOGO等每页首部相同信息
:
:
<div class="about4_1"><%=Title%></div>
<div class="about4_2">您当前的位置 > <a href="index.asp">首页</a> > <a href="about.asp?sId=<%=sId%>"><%=Title%></a></div>
<div class="about7">
<%=Content%>
</div>
’显示就在这了
:
:
<!--#include file="foot.asp"-->
’调用页脚信息
</body>
</html>