«
ASP技巧实例:关于对表单操作的程序

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  InsertingFormcontentintoDatabasewithASP.</p><p><code><%<br /> '--Loader.asp--<br /> '--version1.5.2<br /> '--lastupdated12/5/2002<br /> '<br /> 'FaisalKhan<br /> 'faisal@stardeveloper.com<br /> 'www.stardeveloper.com<br /> 'Classforhandlingbinaryuploads<br /> ClassLoader<br />  Privatedict<br />  <br />  PrivateSubClass_Initialize<br />   Setdict=Server.CreateObject("Scripting.Dictionary")<br />  EndSub<br />  PrivateSubClass_Terminate<br />   IfIsObject(intDict)Then<br />    intDict.RemoveAll<br />    SetintDict=Nothing<br />   EndIf<br />   IfIsObject(dict)Then<br />    dict.RemoveAll<br />    Setdict=Nothing<br />   EndIf<br />  EndSub<br />  PublicPropertyGetCount<br />   Count=dict.Count<br />  EndProperty<br />  PublicSubInitialize<br />   IfRequest.TotalBytes>0Then<br />    DimbinData<br />     binData=Request.BinaryRead(Request.TotalBytes)<br />     getDatabinData<br />   EndIf<br />  EndSub<br />  PublicFunctiongetFileData(name)<br />   Ifdict.Exists(name)Then<br />    getFileData=dict(name).Item("Value")<br />    Else<br />    getFileData=""<br />   EndIf<br />  EndFunction<br />  PublicFunctiongetValue(name)<br />   Dimgv<br />   Ifdict.Exists(name)Then<br />    gv=CStr(dict(name).Item("Value"))<br />    <br />    gv=Left(gv,Len(gv)-2)<br />    getValue=gv<br />   Else<br />    getValue=""<br />   EndIf<br />  EndFunction<br />  PublicFunctionsaveToFile(name,path)<br />   Ifdict.Exists(name)Then<br />    Dimtemp<br />     temp=dict(name).Item("Value")<br />    Dimfso<br />     Setfso=Server.CreateObject("Scripting.FileSystemObject")<br />    Dimfile<br />     Setfile=fso.CreateTextFile(path)<br />      FortPoint=1toLenB(temp)<br />        file.WriteChr(AscB(MidB(temp,tPoint,1)))<br />      Next<br />      file.Close<br />     saveToFile=True<br />   Else<br />     saveToFile=False<br />   EndIf<br />  EndFunction<br />  PublicFunctiongetFileName(name)<br />   Ifdict.Exists(name)Then<br />    Dimtemp,tempPos<br />     temp=dict(name).Item("FileName")<br />     tempPos=1+InStrRev(temp,"")<br />     getFileName=Mid(temp,tempPos)<br />   Else<br />    getFileName=""<br />   EndIf<br />  EndFunction<br />  PublicFunctiongetFilePath(name)<br />   Ifdict.Exists(name)Then<br />    Dimtemp,tempPos<br />     temp=dict(name).Item("FileName")<br />     tempPos=InStrRev(temp,"")<br />     getFilePath=Mid(temp,1,tempPos)<br />   Else<br />    getFilePath=""<br />   EndIf<br />  EndFunction<br />  PublicFunctiongetFilePathComplete(name)<br />   Ifdict.Exists(name)Then<br />    getFilePathComplete=dict(name).Item("FileName")<br />   Else<br />    getFilePathComplete=""<br />   EndIf<br />  EndFunction<br />  PublicFunctiongetFileSize(name)<br />   Ifdict.Exists(name)Then<br />    getFileSize=LenB(dict(name).Item("Value"))<br />   Else<br />    getFileSize=0<br />   EndIf<br />  EndFunction<br />  PublicFunctiongetFileSizeTranslated(name)<br />   Ifdict.Exists(name)Then<br />    temp=LenB(dict(name).Item("Value"))<br />     Iftemp<=1024Then<br />      getFileSizeTranslated=temp&"bytes" <br />     Else<br />      temp=FormatNumber((temp/1024),2)<br />      getFileSizeTranslated=temp&"kilobytes"<br />     EndIf<br />   Else<br />    getFileSizeTranslated=""<br />   EndIf<br />  EndFunction<br />  PublicFunctiongetContentType(name)<br />   Ifdict.Exists(name)Then<br />    getContentType=dict(name).Item("ContentType")<br />   Else<br />    getContentType=""<br />   EndIf<br />  EndFunction<br /> PrivateSubgetData(rawData)<br />  Dimseparator<br />   separator=MidB(rawData,1,InstrB(1,rawData,ChrB(13))-1)<br />  DimlenSeparator<br />   lenSeparator=LenB(separator)<br />  DimcurrentPos<br />   currentPos=1<br />  DiminStrByte<br />   inStrByte=1<br />  Dimvalue,mValue<br />  DimtempValue<br />   tempValue=""<br />  WhileinStrByte>0<br />   inStrByte=InStrB(currentPos,rawData,separator)<br />   mValue=inStrByte-currentPos<br />   IfmValue>1Then<br />    value=MidB(rawData,currentPos,mValue)<br />    DimbegPos,endPos,midValue,nValue<br />    DimintDict<br />     SetintDict=Server.CreateObject("Scripting.Dictionary")<br />     begPos=1+InStrB(1,value,ChrB(34))<br />     endPos=InStrB(begPos+1,value,ChrB(34))<br />     nValue=endPos<br />    DimnameN<br />     nameN=MidB(value,begPos,endPos-begPos)<br />    DimnameValue,isValid<br />     isValid=True<br />     <br />     IfInStrB(1,value,stringToByte("Content-Type"))>1Then<br />      begPos=1+InStrB(endPos+1,value,ChrB(34))<br />      endPos=InStrB(begPos+1,value,ChrB(34))<br />      IfendPos=0Then<br />       endPos=begPos+1<br />       isValid=False<br />      EndIf<br />      midValue=MidB(value,begPos,endPos-begPos)<br />       intDict.Add"FileName",trim(byteToString(midValue))<br />     begPos=14+InStrB(endPos+1,value,stringToByte("Content-Type:"))<br />     endPos=InStrB(begPos,value,ChrB(13))<br />      midValue=MidB(value,begPos,endPos-begPos)<br />       intDict.Add"ContentType",trim(byteToString(midValue))<br />      begPos=endPos+4<br />      endPos=LenB(value)<br />      nameValue=MidB(value,begPos,((endPos-begPos)-1))<br />     Else<br />      nameValue=trim(byteToString(MidB(value,nValue+5)))<br />     EndIf<br />     IfisValid=TrueThen<br />      intDict.Add"Value",nameValue<br />      intDict.Add"Name",nameN<br />      dict.AddbyteToString(nameN),intDict<br />     EndIf<br />   EndIf<br />   currentPos=lenSeparator+inStrByte<br />  Wend<br /> EndSub<br /> <br /> EndClass<br /> PrivateFunctionstringToByte(toConv)<br />  DimtempChar<br />  Fori=1toLen(toConv)<br />   tempChar=Mid(toConv,i,1)<br />   stringToByte=stringToByte&chrB(AscB(tempChar))<br />  Next<br /> EndFunction<br /> PrivateFunctionbyteToString(toConv)<br />  Fori=1toLenB(toConv)<br />   byteToString=byteToString&Chr(AscB(MidB(toConv,i,1)))<br />  Next<br /> EndFunction<br />%></code></p>
<p> </p>

   <p>  1、用户界面: </p><p><code><script language="JavaScript">   <br />function CheckAll(form)   <br />{for (var i=0;i<form.elements.length;i++)   <br />{var e = form.elements[i];   <br />if (e.name != 'chkall')   <br />e.checked = form.chkall.checked;   <br />}   <br />}   <br /></script>   <br /></head> <br /><body> <br /><form method="post" action="del.asp" name="form1"> <br /><table> <br /><tr> <br />   <td ><input type="checkbox" value="111" name="TradeName">电子邮件1</td> <br /></tr> <br /><tr> <br />   <td ><input type="checkbox" value="111" name="TradeName">电子邮件2</td> <br /></tr> <br /><tr> <br />   <td ><input type="checkbox" value="111" name="TradeName">电子邮件3</td> <br /></tr> <br /><tr> <br />   <td ><input type="checkbox" value="111" name="TradeName">电子邮件4</td> <br /></tr> <br /><tr> <br />   <td ><input type="checkbox" value="111" name="TradeName">电子邮件5</td> <br /></tr> <br /><tr> <br />   <td ><input type="checkbox" value="111" name="TradeName">电子邮件6</td> <br /></tr> <br /><tr> <br />   <td bgcolor="#F1FBFC" height="25" colspan="2"> <br />     <p align="left"><input type="checkbox" name="chkall" value="on" onclick="CheckAll(this.form)"><font color="#FF9900">选中所有显示的消息</font></p> <br />   </td> <br /><center> <br />    <br />    <br /> </tr> <br /></table> <br /></form></code></p></p><p>  2、删除文件del.asp </p><p><code><% <br />  '这行写连接数据库的代码 <br />  dim ID,strID <br />  strID="" <br />  For Each  strID In Request.Form("TradeName") <br />      ID=Cint(strID) <br />     if strID<>"" then <br />         DelRs = Server.CreateObject("ADODB.Recordset") <br />        Conn.Execute "Delete From Tbname where ID="&Cstr(ID)  <br />       strID="" <br />   end if <br />Next <br />  <br /> Conn.Close <br /> Set Conn = Nothing <br /> Response.Redirect "成功删除!" <br />%></code></p></p>