ASP网页编程之User Tips: Using Return Values from ...
对于中小型web应用来说,php有很强的竞争力,linux+apache+mysql+php(lamp)的组合几乎可以胜任绝大多数网站的解决方案,对于大型应用来讲,对于系统架构要求更高,需要有成熟的框架支持,jsp的struts是个不错的框架,国内介绍它的资料也非常多,应用逐渐广泛起来。asp就不用说了,error|serverWhenIstarteddevelopingwebpagesthatinteractwithaSQLServerdatabase,Iprobablystartedlikeeverbodyelse:withinlineSQLstatements.Ithenprogressedtousingtheconnectionobjecttocallstoredproceduresandeventuallystartedusingthecommandobject.Ieventuallyrealizedhowusefulreturnvaluesfromstoredprocedurescouldbe,sinceIcouldusethemtoreturnavaluebasedonapotentialerrorconditionthatIcheckforinthestoredprocedure.Recently,Iwasdevelopinganonlinecatalogandhadasituationtodealwith:
Userentersdataintoaform
Needtovalidatetheentries(easyenoughwithclient-sidejavascript)
NeedtoinsertthedataintoaSQLServerdatabaseaftercheckingtomakesurevariousconditionsdontexist.Forexample,theusercouldenteraproduct,butonlyiftheproductdoesntalreadyexistinthecatalog.Thatsnotsomethingthatseasilyaccomplishedwithclient-sidevalidation!
InitiallyIdecideduponafairlypopularroute:createaforminPage1.aspthatsubmitstoPage2.aspwhichattemptstoinserttheuser-enteredinformationintothedatabase.Iftheproductalreadyexists,gobacktoPage1.asp,displayingamessageandpopulatingthefieldswithwhattheuserentered.Whilethisisapossibleapproach,trustmewhenIsaythatitsapaintocodeifyouhavealotofformfields!IdeallyIwantedapop-upmessagethatIcouldcustomizebasedontheconditionfoundinthestoredprocedure.(Ilikepop-upsbecausebytheirnature,theydrawmoreattentionthanamessagedisplayedonapage.)Also,IwantedtheusertakenbacktoPage1.aspwithallofhis/herentriesalreadyfilledin.
Hereisanexamplestoredprocedurethatreturnsanerrorresultifsomethinggoesawry:
CreateProcedure
(
@productnamevarchar(50)=null,
@pricemoney=null
}
AS
ifexists(selectproductnamefromtblProductswhereproductname=@productname)
return55555
else
insertintotblproducts(productname,price)
values(@productname,@price)
return@@error
Asimplesamplestoredprocedurethatcheckstoseeiftheproductalreadyexists.Ifso,itreturns55555,otherwiseitinsertstheproductandreturnstheerrorcode(whichwill0ifsuccessful).Now,ontheASPside,Iusethecommandobjecttosendtheformcontentstothestoredprocedure.Thestoredproceduresreturnvalueisalwaysthefirstitemintheparameterscollectionofthecommandobject(cmd.parameters(0))afterthecommandobjectsExecutemethodhasbeencalledinthiscase.
<%
optionexplicit
response.buffer=true
response.exires=-1441
dimconnect,cmd,returnvalue
setconnect=server.createobject("adodb.connection")
setcmd=server.createobject("adodb.command")
connect.openYourConnectionString
setcmd.activeconnection=connect
cmd.commandtype=4sp(Iknow,magicnumbers,butaddacommentandthereyougo)
cmd.commandtext="Proc_InsertProduct"
cmd.parameters(1)=request.form("productname")
cmd.parameters(2)=request.form("price")
cmd.execute
%>
Atthispointthestoredprocedurehasbeenexecutedandcmd.parameters(0)containsthereturnvalue.Now,ifthereturnvalueisnot0,meaningsomesortoferroroccurred,wewishtocalltheMyErrorsubroutine,whichwillgenerateaJavaScriptpopuperrormessage.Otherwise,ifthereisnoerror,sendtheuserontosomeotherpage,onethat,perhaps,displaysaconfirmationmessageofthedatabaseactionjustperformed.
<%
returnvalue=cmd.parameters(0)
Didanerroroccur?
ifreturnvalue0
Somesortoferroroccurred
response.writeMyError(returnvalue)
else
Noerrors...
response.redirect("Whereever.asp")
endif
setcmd=nothing
connect.close
setconnect=nothing
%gt;
TheMyErrorsubroutinenowneedstousesomeclient-sideJavascriptcodetosendtheuserbackapop-upmessage;oncethispopupmessageisreadandtheOKbuttonisclicked,itshouldtaketheuserbacktoPage1.asp.
<%
functionMyError(errorcode)
dimtitle,message
selectcaseerrorcode
&nbs</p>缺乏可以共同遵循的行业标准,ASP还处在发展初期,大家对它的理解不同,如产品和服务标准,收费标准等,不利于行业的健康发展。 用户端的浏览器不需要提供任何别的支持,这样大提高了用户与服务器之间的交互的速度。 学习ASP其实应该上升到如何学习程序设计这种境界,其实学习程序设计又是接受一种编程思想。比如ASP如何学习,你也许在以前的学习中碰到过。以下我仔细给你说几点: ASP(ActiveServerPages)是Microsfot公司1996年11月推出的WEB应用程序开发技术,它既不是一种程序语言,也不是一种开发工具,而是一种技术框架,不须使用微软的产品就能编写它的代码,能产生和执行动态、交互式、高效率的站占服务器的应用程序。 ASP.Net摆脱了以前ASP使用脚本语言来编程的缺点,理论上可以使用任何编程语言包括C++,VB,JS等等,当然,最合适的编程语言还是MS为.NetFrmaework专门推出的C(读csharp),它可以看作是VC和Java的混合体吧。 先学习用frontpage熟悉html编辑然后学习asp和vbscript建议买书进行系统学习 我可以结合自己的经验大致给你说一说,希望对你有所帮助,少走些弯路。 封装性使得代码逻辑清晰,易于管理,并且应用到ASP.Net上就可以使业务逻辑和Html页面分离,这样无论页面原型如何改变,业务逻辑代码都不必做任何改动;继承性和多态性使得代码的可重用性大大提高。 如何学好ASP,以前也有人问过,把回答给你转过来看看能否对你有帮助: 我可以结合自己的经验大致给你说一说,希望对你有所帮助,少走些弯路。
页:
[1]