asp开发学校OA项目中常用到知识点(1)基础

1、检验表单填写不能为空,代码如下:



onsubmit="javascript:return checkAllInput();"

2、防止二次提交相同内容

'防止插入相同记录

set rss = connq.Execute("select * from student where sname = '"&sname&"'")
if  rss.bof and rss.eof  then

'执行插入记录操作
…… ……

else  
        response.write("该记录已经存在!")  
end   if  

返回

3、批量修改,删除操作

<%
'asp 数组 批量修改记录
id=Request("id")
id=Split(id,",")

hits=Request("hits")
hits=Split(hits,",")

regtime=Request("regtime")
regtime=Split(regtime,",")

For i=LBound(id) To UBound(id)
        Set rs = Server.CreateObject("ADODB.Recordset")
        sql= "Select * from article where id ="&id(i)
        If Not rs.EOF Then
        rs("hits")=hits(i)
        rs("regtime")=regtime(i)
        rs.Update
        End If
Next

%>

原理:就是用一个FOR循环朝数据库里面插入数据,但是过程比较复杂,但是用到数据库的存储过程的时候要用到事务,以防止插入过程中数据出错。



stuinput2.asp 页面部分:

<%option explicit%>




<%
dim schname,classname,classid,schid
schname=""
classname=""
classid=""
schid=""
schname=ubb(request.querystring("schname"))
classname=ubb(request.querystring("classname"))
classid=ubb(request.querystring("classid"))
schid=ubb(request.querystring("schid"))
%>




导入学生









      
  
  
  
        
      
  
  
  

   学校:<%=schname%>  班级:<%=classname%>
  

      onClick="form1.txta_examsco.value=clipboardData.getData('text');">   
      
  
      
  
  










  
  
  
  
  
  
  


  
  


  
  


    注意:此部分数据导入只对职校学生,请慎重导入,以免出错。导入学生时,必须按照下列格式,第一列为学生编号,第二列为学生手机;复制时只需要复制像格式中的蓝色区域!
(重复的号码只能导入一次,若有一号多个学生的只能一一添加)

  
学生编号
学生手机
108309****1345******
108309****1345******
108309****1345******







数据处理及提交部分:

<%option explicit%>






<%

Dim examsco,schid,returnvalue,temp,i,templist2,stuid,stuname,stumobile,stumobilelist,stumobile2,studentid,classid,k,errormsg,noinnum,errormobnum,errstunum,schname,classname
classid=""
examsco=""
schid=""
schname=""
classname=""

'Response.Write("1111")
examsco=ubb(Request.Form("txta_examsco"))
schid=ubb(Request.Form("hid_schid"))
classid=ubb(Request.Form("hid_classid"))
schname=ubb(Request.Form("hid_schname"))
classname=ubb(Request.Form("hid_classname"))
if examsco="" then
response.write("")
response.End
End if

examsco=replace(examsco,chr(13) & chr(10),"|@|")
examsco=replace(examsco," ","|#|")
examsco=replace(examsco,"'","")
examsco=examsco&"|#||@|"

'Response.Write(examsco)
'response.End
returnvalue=""
returnvalue=checkdate(examsco)

if returnvalue="1" then
  response.write("")
  response.end
elseif returnvalue="2" then
  response.write("")
  response.end
elseif returnvalue="3" then
  response.write("")
  response.end
end if

temp=split(examsco,"|@|") '分行
for i=0 to ubound(temp)-1
  templist2=split(temp(i),"|#|")'分字段
  stuid=""
  stuname=""
  stumobile=""
  stuid=trim(templist2(0))
  stumobile=trim(templist2(1))
  if stumobile<>"" then
   if right(stumobile,1)="/" then
    stumobile=left(stumobile,len(stumobile)-1)
   end if
   stumobilelist=""
   stumobilelist=split(stumobile,"/")
   if ubound(stumobilelist)=0 then '如果为一个号码时
    stumobile2=""
    stumobile2=stumobile
    if len(stumobile2)=8 then
     stumobile2="0512"&stumobile2
    end if

    if isnull(studentid) or studentid="" then
     returnvalue=""
     returnvalue=addstudent(stuid,stumobile2,schid)
     if returnvalue="0" then
      k=k+1
     elseif returnvalue="9" then
      noinnum=noinnum+1
     elseif returnvalue="6" then
      errormobnum=errormobnum+1
     else
      errstunum=errstunum+1
     end if
    else
     returnvalue=""
     returnvalue=addmobile(studentid,stumobile2)
     'response.write("
returnvalue1=" & returnvalue & "
")
     if returnvalue<>"0" then
      noinnum=noinnum+1
      'response.write("")
      'exit for
      'response.end
     end if
    end if
   else '多个号码
    
    for j=0 to ubound(stumobilelist)
     stumobile2=""
     stumobile3=""
     stumobile2=stumobilelist(j)
     stumobile3=stumobilelist(0)
     if len(stumobile3)=8 then stumobile3="0512"&stumobile3
      
     if len(stumobile2)=8 then
      stumobile2="0512"&stumobile2
     end if
     'response.write("sdf"&stumobile2)
     studentid=""
     'response.write("classid="&classid&"stumobile2="&stumobile2)
     studentid=getstudentid(classid,stumobile3)
     'studentid=getstudentid("17862","13913568488")
     'response.write("studentid="&studentid&"
")
     if isnull(studentid) or studentid="" then
      returnvalue=""
      returnvalue=addstudent(stuid,stuname,stuphone,stumobile2,classid)
      'response.write("
returnvalue=" & returnvalue & "
")
      'response.write(stumobile2)
      'response.end
      if returnvalue="0" then
       k=k+1
      elseif returnvalue="9" then
       noinnum=noinnum+1
      elseif returnvalue="6" then
       errormobnum=errormobnum+1
      'else
       'response.write("")
       'exit for
       'response.end
      else
       errstunum=errstunum+1
      end if
     else
      returnvalue=""
      returnvalue=addmobile(studentid,stumobile2)
      'response.write("
returnvalue1=" & returnvalue & "
")
      if returnvalue<>"0" then
       noinnum=noinnum+1
       'response.write("")
       'exit for
       'response.end
      end if
     end if
    next
    'response.end
   end if
  else
   returnvalue=""
   returnvalue=addstudent(stuid,stuname,stuphone,stumobile,classid)
   'response.write("
returnvalue=" & returnvalue & "
")
     if returnvalue="0" then
      k=k+1
     elseif returnvalue="9" then
      noinnum=noinnum+1
     elseif returnvalue="6" then
      errormobnum=errormobnum+1
     'else
      'response.write("")
      'exit for
      'response.end
     else
      errstunum=errstunum+1
      'errstunum="dssss"
     end if
  end if
next
'response.write("共有"&k&"条记录导入数据库!")
errormsg=""
if k>0 then
  errormsg="成功导入"&k&"条学生记录\n"
end if
'if noinnum<>"0" then
  'errormsg=errormsg & "有"&noinnum&"条学生或号码记录原来已经存在于本校,本次不能导入\n"
'end if
if errormobnum>0 then
  errormsg=errormsg & "有无效号码"&errormobnum&"个未能导入\n"
end if
if errstunum>0 then
  errormsg=errormsg & "有"&errstunum&"个学生导入失败\n"
end if
response.write("")

%>


<%
function checkdate(scotext)
dim chk_templist,chk_templist1
dim chk_stuid,chk_stumobile
dim chk_moblist
dim chk_i
chk_templist=split(scotext,"|@|")
for i=0 to ubound(chk_templist)-1
  chk_templist1=split(chk_templist(i),"|#|")
  if ubound(chk_templist1)<1 then
   checkdate="1"
   exit for
  else
   chk_stuid=""
   chk_stumobile=""
   chk_stuid=trim(chk_templist1(0))
   chk_stumobile=trim(chk_templist1(1))
   if isnumeric(chk_stuid)=false then
    checkdate="2"
    exit for
   else
    if chk_stumobile<>"" then
     if right(chk_stumobile,1)="/" then
      chk_stumobile=left(chk_stumobile,len(chk_stumobile)-1)
     end if
     if instr(chk_stumobile,"/")>0 then
      chk_moblist=split(chk_stumobile,"/")
      for chk_i=0 to ubound(chk_moblist)
       if len(chk_moblist(chk_i))=8 then
        chk_moblist(chk_i)="0512"&chk_moblist(chk_i)
       end if
       '--------------------------------------------------
       'if len(chk_moblist(chk_i))="11" and left(chk_moblist(chk_i),3)="512" then
       ' chk_moblist(chk_i)="0"&chk_moblist(chk_i)
       'end if
       '----------------------------------------------------
       if checkmobile(chk_moblist(chk_i))="bad" then
        checkdate="3"
        exit for
       else
        checkdate="0"
       end if
      next
     else
      if len(chk_stumobile)=8 then
       chk_stumobile="0512"&chk_stumobile
      end if
      '------------------------------------------
      'if len(chk_stumobile)=11 and left(chk_stumobile,3)="512" then
      '  chk_stumobile="0"&chk_stumobile
      'end if
      '---------------------------------------------
      if checkmobile(chk_stumobile)="bad" then
       checkdate="3"
       exit for
      else
       checkdate="0"
      end if
     end if
    else
     checkdate="0"
    end if
   end if  
  end if
next
end Function

function addstudent(stuid,stumobile,schid)
'Response.Write(stuid)
'Response.End
dim fun_returnvalue
set cmd=Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = connstr
cmd.commandtext="dbo.jxt_stumobile_insert"
cmd.commandtype=4
cmd.parameters.append cmd.createparameter("@schid",adinteger,adparaminput,4,schid)
cmd.parameters.append cmd.createparameter("@studentid",advarchar,adparaminput,15,stuid)
cmd.parameters.append cmd.createparameter("@mobile",advarchar,adparaminput,20,stumobile)
cmd.parameters.append cmd.createparameter("@Ret",adinteger,adparamoutput)
cmd.execute()
fun_returnvalue=""
fun_returnvalue=cmd.parameters("@Ret").value
set cmd=nothing
if fun_returnvalue="0" then
  call logopera(session("se_userid"),"stuinput2.asp","stuinput2_action.asp",session("se_userid")&"给"&classid&"导入职校学生手机号码!")
ElseIf  fun_returnvalue="1" then
  Response.Write("")
  Response.End
ElseIf  fun_returnvalue="2" Then
  Response.Write("")
  Response.End
ElseIf  fun_returnvalue="3" Then
  Response.Write("")
  Response.End
ElseIf  fun_returnvalue="4" Then
  Response.Write("")
  Response.End
end if
addstudent=fun_returnvalue
end function
%>


存储过程

Alter PROCEDURE [dbo].[jxt_stumobile_insert]
@schid  int,
@studentid nvarchar(15),
@mobile  nvarchar(20),
@ret  int output
as
begin
declare @status int
begin
  if dbo.FC_CheckMobileType(@mobile)='yd'
  begin
   set @status=12
  end
  else if dbo.FC_CheckMobileType(@mobile)='lt'
  begin
   set @status=22
  end
  else if dbo.FC_CheckMobileType(@mobile)='xlt'
  begin
   set @status=32
  end
  else if dbo.FC_CheckMobileType(@mobile)='cdma'
  begin
   set @status=52
  end
end
if not exists(select top 1 * from jxt_student where studentid=''+@studentid+'')
begin
  set @ret=1  --学生编号不存在
end
else if len(@studentid)<>10
begin
  set @ret=2  --学生编号长度不正确
end
else if len(@mobile) not in('11','12')
begin
  set @ret=3  --手机号码不正确
end
else if exists(select * from Jxt_Student_Mobile where studentid=@studentid and mobile=@mobile)
begin
  set @ret=4  --该条数据已经存在
end
else
begin
  begin transaction
  insert into Jxt_Student_Mobile (studentid,mobile,status,[path],schid)
  values(@studentid,@mobile,@status,@status,@schid)
  if @@error<>0
  begin
   rollback transaction
   set @ret=5 --数据插入成功
  end
  set @ret=0
  commit transaction
end
end

方法一:
<%
id=request.form("checkbox")
id=Split(id,",")
shu=0
for i=0 to UBound(id)
sql="select * from jiang_fname where id="&id(i)
set rs=conn.execute(sql)
if not rs.eof then
delete_file(rs("fname"))
end if
rs.close
set rs=nothing
sql="delete from jiang_fname where id="&id(i)      
conn.execute sql,shu1
shu=shu+1
next  

if shu>0 then
response.Write("")
else
response.write("")
end if
conn.close
set conn=nothing
%>


方法二:巧用in关键字实现数据的批量删除
managenews.asp


<%'数据库的连接文件我就不多说了%>


管理新闻






<%
set rs=server.createobject("adodb.recordset")
sql="select * from news order by addtime desc"
rs.open sql,conn,1,3
%>
<%if rs.eof then%>




没有新闻!

<%else%>














<%
do while not rs.eof
%>








<%
rs.movenext
loop
%>






新闻管理中心

删除框

新闻标题

发布时间

管理
" ID="Checkbox1">
<%If Len(rs("title"))<=30 Then%><%=rs("title")%><%else%>
<%=(Left(rs("title"),30))%>...
<%end if %>
<%=rs("addtime")%>">编 辑

选中所有的显示新闻 


<%end if%>
<%
set rs=nothing
conn.close
set conn=nothing
%>



delnews.asp文件

<%
arrdel=Request("id")
'Response.Write arrdel
sql="delete from news where id in ("&arrdel&")"
'Response.Write sql
conn.Execute sql
set conn=nothing
response.write""
response.end
%>








<%
set c.CreateObject("ADODB.Connection")
conn.c
conn.open
set com=server.CreateObject("ADODB.Command")
com.CommandText="delete from Portal_Articles where articleid="+ request.QueryString("articleId")
com.ActiveC
com.execute()
response.Redirect("admin_articles.asp")
'conn.execute("insert into portal_articles (title,author,catid) values('1314','test',5)")
'conn.close()
'response.Redirect("admin_articles.asp")
%>

''批量删除代码
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>


<%
'获取所有选中ID串
dim myIds
myIds=request.Form("chkId")
'分离Id到数组
dim ArticleIds
ArticleIds=split(myIds,", ")
'response.write(ubound(ArticleIds))
'连接数据库
'response.write(myIds)
set c.CreateObject("ADODB.Connection")
conn.c
conn.open
'''''''''''''''''''''''''''''''''''''''''''
'循环删除选中id
dim i
for i=0 to ubound(ArticleIds)
sqlstr="delete from portal_articles where ArticleId="+ArticleIds(i)
conn.execute(sqlstr)
'response.write(sqlstr+"
")
next
conn.close
if ubound(ArticleIds)>=0 then response.redirect("admin_articles.asp")'修正dreamweaver自动生成代码仍显示清除记录的Bug
%>










第一种

<%
id=request.form("checkbox")
    id=Split(id,",")
    shu=0
    for i=0 to UBound(id)
sql="select * from jiang_fname where id="&id(i)
set rs=conn.execute(sql)
if not rs.eof then
   delete_file(rs("fname"))
end if
rs.close
set rs=nothing
   sql="delete from jiang_fname where id="&id(i)    
   conn.execute sql,shu1
shu=shu+1
    next

    if shu>0 then
   response.Write("")
    else
   response.write("")
    end if
    conn.close
    set conn=nothing

%>
第二种
[ASP]通过复选框批量删除的处理方法:得到返回值的个数,通过一个for循环就可以解决问题..


解决方法:
<%
dim conn,connstr,rs,sqlstr,id
id=request("id")   '//id为复选框的NAME属性
set conn=server.createobject("ADOBC.connection")
connstr= "Driver={SQL Server};server=(local);uid=sa;pwd=;database=database_name;"
conn.open connstr
set rs=server.createobject("ADOBC.recordset")
on error resume next
for   i=0 to id.fields.count-1
sqlstr="delete * from table_name where id='"+Cstr(id.fields(i))+"';"
rs.execute(sql) '//或用下面语句:set rs=conn.execute(sqlstr)
next
rs.close
set rs=nothing
conn.close
set conn=nothing
Response.redirect("index.asp")   '//跳转到相关的页面
%>


''删除代码


<%
set conn=server.CreateObject("ADODB.Connection")
conn.connectionString=MM_conn_STRING
conn.open
set com=server.CreateObject("ADODB.Command")

com.CommandText="delete from Portal_Articles where articleid="+ request.QueryString("articleId")
com.ActiveConnection=conn
com.execute()
response.Redirect("admin_articles.asp")
'conn.execute("insert into portal_articles (title,author,catid) values('1314','test',5)")
'conn.close()
'response.Redirect("admin_articles.asp")
%>

''批量删除代码

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>



<%
'获取所有选中ID串
dim myIds
myIds=request.Form("chkId")

'分离Id到数组
dim ArticleIds
ArticleIds=split(myIds,", ")
'response.write(ubound(ArticleIds))
'连接数据库
'response.write(myIds)
set conn=server.CreateObject("ADODB.Connection")
conn.connectionString=MM_conn_STRING
conn.open
'''''''''''''''''''''''''''''''''''''''''''
'循环删除选中id
dim i
for i=0 to ubound(ArticleIds)
sqlstr="delete from portal_articles where ArticleId="+ArticleIds(i)
conn.execute(sqlstr)
'response.write(sqlstr+"
")
next
conn.close
if ubound(ArticleIds)>=0 then response.redirect("admin_articles.asp")'修正dreamweaver自动生成代码仍显示清除记录的Bug
%>


<%
关于上文的方法,通过实验居然行不通。问题出在那里我也没有搞清楚。不过找到了另一个方法来代替。我试验了一下,多复选框提交,返回的值是一个字符串。形如:(1,2,3,4,5,6...)之类。但是网上说可以通过数组下标的方式取得每一个的值,如文中所说的,通过name.fields(n)的方式取下应相的值。但是没有行通。最后我用的方法是先将返回的值折分将折分后的值赋给一个数组变量。然后通过取数组最大的可用下标来取得所要的值。其中要用到两个函数:一个:split("","")是用第二个参数来折分第一个参数。二个:UBound(数组)。返回该数组的最大可用下标。好了不多说了,要上班了,先把代码帖上来,以后再来分折了。

代码:
if trim(EmailTitle)="" then
MessageBox("邮件发送失败!请填写你要发送的邮件的标题!")
else
if trim(Message)="" then
MessageBox("邮件发送失败!请填写你要发送的邮件的内容!")
else
dim EMail_Add_Array,EMail_Add_Array_Count
EMail_Add_Array = split(Student_Id_Str,",")
EMail_Add_Array_Count = UBound(EMail_Add_Array)
if EMail_Add_Array_Count<0 then
MessageBox("邮件发送失败!请选择接收邮件的用户!")
else
dim rs_EMail_Add,sql_EMail_Add
set rs_EMail_Add= Server.CreateObject("ADODB.Recordset")
sql_EMail_Add="select * from E_Mail where MailID IS NULL"
rs_EMail_Add.open sql_EMail_Add,conn,1,3
dim m_Count
m_Count=0
for i=0 to EMail_Add_Array_Count
rs_EMail_Add.addnew
rs_EMail_Add("FromUid")=UserId
rs_EMail_Add("ToUid")=EMail_Add_Array(i)
rs_EMail_Add("TiTle")=EMailTitle
rs_EMail_Add("Detail")=Message
rs_EMail_Add("IsRead")=0
if SaveMail then
rs_EMail_Add("SendDel")=1
else
rs_EMail_Add("SendDel")=0
end if
rs_EMail_Add("RecvDel")=0
rs_EMail_Add("Writetime")=Now
rs_EMail_Add.update
m_Count=m_Count+1
next
MessageBox("恭喜!!您已经成功的发送了" & m_Count &"封邮件!")
rs_EMail_Add.close
set rs_EMail_Add=Nothing
Class_Id = ""
Student_Id_Str = 0
EmailTitle= ""
Message = ""
SaveMail = 0
end if
end if
end if
%>


[本日志由 wang 于 2009-10-12 01:39 PM 编辑]
上一篇: Javascript技巧大集合
下一篇: asp开发学校OA项目中常用到知识点(2)sql
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 2830
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 300 字 | UBB代码 开启 | [img]标签 关闭