<%@ Language=VBScript %> <html> <head> <title>2000081402</title> </head> <body> <form id="sample1" method="post"action="sample1.asp"> <% If Request.Form.Count <> 0 Then Response.Write "Your name is " Response.Write Request.Form("txtName") Response.Write ", and your favoritecolor is " Response.Write Request.Form("selColor") Response.Write "<br>" End If %> <table cellSpacing=0 cellPadding=4 border=0> <tr> <td><p align=right>What is yourname:</p></td> <td><input type="text"name= txtNamevalue="<%=Request.Form("txtName")%>"></td></tr> <tr> <td><p align=right>What is yourfavorite color:</p></td> <td> <select name=selColor> <option <%if Request.Form("selColor")= "Black" Then Response.Write "selected"%>>Black</option> <option <%if Request.Form("selColor")= "Blue" Then Response.Write "selected"%>>Blue</option> <option <%if Request.Form("selColor")= "Green" Then Response.Write "selected"%>>Green</option> <option <%if Request.Form("selColor")= "Pink" Then Response.Write "selected"%>>Pink</option> <option <%if Request.Form("selColor")= "Red" Then Response.Write "selected"%>>Red</option> </select> </td></tr> <tr> <td> </td> <td><input type="submit"id=submit value="Submit"></td></tr> </table> </form> </body> </html>
我们看到,在这里我们不得不将ASP代码和HTML混在一起,使得代码非常的难以看懂,想象一下如果一个非常复杂的页面…… 我们的页面首先判断是否是回传,还是第一次访问。我们通过检查Request.Form集合。如果是0,表示是第一次访问,否则表示提交的按钮被按下了,我们将通过Response.Write输出一条信息给用户。 <% If Request.Form.Count <> 0 Then Response.Write "Your name is " Response.Write Request.Form("txtName") Response.Write ", and your favoritecolor is " Response.Write Request.Form("selColor") Response.Write "<br>" End If %>
对于我们表单中的每个元素,我们必须通过代码来使它们保持状态。这些代码简单而相似。 <td><input type="text"name= txtNamevalue="<%=Request.Form("txtName")%>"></td></tr> ... <select name=selColor> <option <%if Request.Form("selColor")= "Black" Then Response.Write "selected"%>>Black</option> <option <%if Request.Form("selColor")= "Blue" Then Response.Write "selected"%>>Blue</option> <option <%if Request.Form("selColor")= "Green" Then Response.W