有个例子,自己研究一下
.源程序
1.jsCallASP.asp
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>不刷新页面,动态读取数据库</title>
<script language="javascript" src="function.js"></script>
<script language="javascript">
function getTables(obj){
var server=txtServer.value;
var database=txtDatabase.value;
var jsSrc="getTables.asp?obj=selTable&server=" + server + "&database=" + database + "&rnd=" + Math.random();
if(server==""||database=="") {alert('请填写完整!');return;}
obj.value='读取中...';
obj.disabled=true;
reloadJs(js,jsSrc);
}
function getRecords(obj){
var server=txtServer.value;
var database=txtDatabase.value;
var table=selTable.value;
var jsSrc="getRecords.asp?obj=DisData&server=" + server + "&database=" + database + "&table=" + table + "&rnd=" + Math.random();
if(server==""||database==""||table=="") {alert('请填写完整!');return;}
obj.value='读取中...';
obj.disabled=true;
reloadJs(js,jsSrc);
}
</script>
</head>
<body>
<p>不刷新页面,动态读取数据库</p>
<script language="javascript" id="js" src=""></script>
<table width="421" border="0">
<tr>
<td width="146">Sql Server服务器: </td>
<td width="265"><input name="txtServer" type="text" id="txtServer" value="localhost" readonly></td>
</tr>
<tr>
<td>数据库:</td>
<td><input name="txtDatabase" type="text" id="txtDatabase" value="Northwind" readonly> </td>
</tr>
<tr>
<td>表:</td>
<td><select name="selTable" id="selTable">
</select>
<input name="cmdRefresh" type="button" id="cmdRefresh" value="刷 新" onClick="JavaScript:getTables(this)"> </td>
</tr>
<tr>
<td>前100条数据:</td>
<td><input name="cmdRead" type="button" id="cmdRead" value="读 取" onClick="JavaScript:getRecords(this)"></td>
</tr>
<tr>
<td colspan="2">
<div id="DisData"></div>
</td>
</tr>
</table>
</body>
</html>
2.getTables.asp
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Dim CnStr,SQL,Rs,ServerName,DatabaseName,Obj
ServerName=Request.QueryString("server")
DatabaseName=Request.QueryString("database")
Obj=Request.QueryString("obj")
Set Rs=Server.CreateObject("ADODB.Recordset")
CnStr="Driver={SQL Server};Server=" & ServerName & ";Database=" & DatabaseName & ";uid=sa;pwd=sa"
SQL="select name from sysobjects where type='u' and status>0"
Rs.Open SQL,CnStr,1,1
If Not Rs.Eof then
Response.Write("ClearSelect(" & Obj & ");" & vbcrlf)
While not Rs.eof
text=Rs("name")
value=Rs("name")
Response.Write("FillSelect(" & Obj & ",'" & text & "','" & value & "');" & vbcrlf)
Rs.MoveNext
Wend
End If
Rs.Close
Set Rs=Nothing
%>
cmdRefresh.value="刷 新";
cmdRefresh.disabled=false;
3.getRecords.asp
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
On Error Resume Next
Dim CnStr,SQL,Rs,ServerName,DatabaseName,Obj
ServerName=Request.QueryString("server")
DatabaseName=Request.QueryString("database")
Table=Request.QueryString("Table")
Obj=Request.QueryString("obj")
Set Rs=Server.CreateObject("ADODB.Recordset")
CnStr="Driver={SQL Server};Server=" & ServerName & ";Database=" & DatabaseName & ";uid=sa;pwd=sa"
SQL="select top 100 * from [" & table & "]"
Rs.Open SQL,CnStr,1,1
If Not Rs.Eof then
Response.Write("SetHTML(" & Obj & ",'');" & vbcrlf)
outHTML="<table border=1><tr>"
for i=0 to Rs.Fields.count-1
outHTML=outHTML & "<th>" & Rs.Fields(i).Name & "</th>"
next
outHTML=outHTML & "</tr>"
While not Rs.eof
outHTML=outHTML & "<tr>"
For i=0 to Rs.Fields.count-1
FieldType=Rs.Fields(i).Type
FieldValue=Rs.Fields(i).Value
Select Case FieldType
Case 128
outHTML=outHTML & "<td>[Binary]</td>"
Case 205
outHTML=outHTML & "<td>[LongVarBinary]</td>"
Case Else
outHTML=outHTML & "<td>" & FieldValue & "</td>"
End Select
Next
outHTML=outHTML & "</tr>"
Rs.MoveNext
Wend
outHTML=outHTML & "</table>"
outHTML=Replace(outHTML,vbcrlf,"<br>")
outHTML=Replace(outHTML,"""","\""")
Response.Write("var HTML=""" & outHTML & """;" & vbcrlf)
Response.Write("SetHTML(" & Obj & ",HTML);" & vbcrlf)
Else
Response.Write("SetHTML(" & Obj & ",'没有记录!');" & vbcrlf)
End If
Rs.Close
Set Rs=Nothing
%>
cmdRead.value="读 取";
cmdRead.disabled=false;
4.Function.js
var _onjsload=null;
function reloadJs(obj,jsfile)
{
_onjsload= function(){window.status=jsfile + " is loaded.";}
obj.src=jsfile;
}
function jsLoadReady()
{
if(_onjsload!=null)
{
_onjsload();
_onjsload=null;
}
}
function FillSelect(obj,text,value){
var oOption = document.createElement("OPTION");
oOption.text=text;
oOption.value=value;
obj.add(oOption);
}
function ClearSelect(obj){
for(var i=obj.length;i>=0;i--){
obj.remove(i);
}
}
function SetHTML(obj,value)
{
obj.innerHTML=value;
}