假如一个数据库的表中有“学号”和“姓名”两个字段,如果想在输入表单中输入学号后鼠标离开时自动到数据库的表中去查找相应的姓名并且在姓名的框中显示出来,请问应如何编写程序。  
    前提条件是每页显示5组(或更多)组表单框,为了使代码简化,采用数组及FOR循环生成表单,代码如下:  
<FORM  Name="form1"  Method="Post"  Action="">  
<%  
   dim  XueHao(5),XinMing(5)  
   FOR  I=0  TO  4  
%>  
   学号:<INPUT  TYPE="Text"  NAME="XueHao(<%=I%>)"  OnBlur="....">  
   姓名:<INPUT  TYPE="Text"  NAME="XinMing(<%=I%>)">  
<%Next%>  
</FORM>

解决方案 »

  1.   

    file:userinfo.htm
    ----------------------------
    <html>
    <head>
    <title>This is the testing page about how to use the database in ASP!</title>
    </head>
    <body bgcolor="#BBBBFF">
    <center>
    <font color="blue">
    user information for the login the sqlserver2000 and oracle9i<br>
    -------------------------------------------<br>
    </font>
    <form action="insert.asp" method="post">
         name: <input type="text"  name="name"><br> 
         TelNo:<input type="text"  name="telno"><br>
         PhoNo:<input type="text"  name="phono"><br>
         Email:<input type="text"  name="email"><br>
    Address:<input type="text"  name="address"><br>
    <input type="submit"  value="submit" name="submit">
    <input type="reset" value="reset" name="reset">
    </form>
    <a href="http://127.0.0.1:1996/use_db/oracle_asp.asp"><font size=5 >test oracle database</font></a>
    </center>
    </body>
    </html>
    -----------------------------------file:insert.asp
    ----------------------------------
    <%
    name=request.form("name")
    telno=request.form("telno")
    phono=request.form("phono")
    email=request.form("email")
    address=request.form("address")
    set conn=Server.CreateObject("ADODB.Connection")
    strDSN="DSN=Test_ASP;UID=psusong;PWD=psusong"
    conn.open strDSN
    SQL="INSERT INTO userinfo(name,TelNo,PhoNo,Email,Address)"
    SQL=SQL & " VALUES('" & name &  "','" & telno & "','" & phono & "','" & email & "','" & address & "')"
    conn.Execute(SQL)%>
    <%
    response.redirect "show.asp"
    %>
    ----------------------------------file:show.asp
    ----------------------------------
    <%strDSN="DSN=Test_ASP;UID=psusong;PWD=psusong"
    set conn=createobject("ADODB.Connection")
    conn.open strDSN
    SQL="SELECT * FROM userinfo"
    SET rs=conn.Execute(SQL)%>
    <body bgcolor="#BBBBFF">
    name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TelNO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PhoNo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Address&nbsp<br>
    <%do while not rs.eof %>
    <font color="Red">
    <%=rs("name")%>
    </font>
    <%=rs("TelNo")%>
    <%=rs("PhoNo")%>
    <%=rs("Email")%>
    &nbsp;&nbsp;&nbsp;
    <font color="blue">
    <%=rs("Address")%>
    </font><br>
    <% rs.movenext
    loop%>
    </body>
    ------------------------file:oracle_asp.asp
    ------------------------
    <%Set OraSession=CreateObject("OracleInProcServer.XOraSession")
    Set OraDatabase=OraSession.DbOpenDatabase("itanynj","scott/psusong",0)
    Set rs=OraDatabase.CreateDynaset("select * from EMP",0) 
    %>
    <body bgcolor="#BBBBFF">
    <center>
    <font color="red" size=5>
    empno&nbsp;&nbsp;ename&nbsp;&nbsp;hiredate&nbsp;&nbsp;job<br>
    </font>
    -----------------------------------------<br>
    <% DO WHILE NOT rs.eof%>
    <%=rs("empno")%>
    &nbsp;&nbsp
    <font color="blue">
    <%=rs("ename")%>
    </font>
    &nbsp;&nbsp
    <%=rs("hiredate")%>
    &nbsp;&nbsp
    <font color="blue">
    <%=rs("JOB")%>
    </font>
    <br>
    <%rs.movenext
    loop%>
    -----------------------------------------<br>
    <font color="blue">
    This is the example of testing Active Server Page by oracle database!<br>
    This is the example "SELECT * FROM EMP" from the user:scott/tiger<br>
    Set OraSession=CreateObject("OracleInProcServer.XOraSession")<br>
    Set OraDatabase=OraSession.DbOpenDatabase("database_name","scott/tiger",0)<br>
    Set rs=OraDatabase.CreateDynaset("select * from EMP",0)
    </center>
    </body>
    -------------------------
    说明:
    IIS中建立了一个web站点端口号:1996
    建立了一个SQL server2000的数据库表userinfo
    其中有字段:id ,name,TelNo,PhoNo,Email,Address 
    在程序中也测试了oracle地联结方式!
      

  2.   

    如有问题:请发邮件到[email protected]
      

  3.   

    这是不现实的,因为每次查询都更新了页面,会使用户很迷惑。如果数据很少,可以使用 JavaScript 实现,将可能的数据都保存到一个 javascript 的书组中。