用XMLHTTP无刷新读取,增加,修改数据

来源:互联网 发布:windows 启用远程桌面 编辑:程序博客网 时间:2024/06/09 10:30

数据库:northwind
系统:window2003
开发环境:visual statio 2003
加载数据
1、读取,增加,修改数据页面
xmlHttpGetData.html


2、主要js
·getResponseData(),主要用来获取GetData.aspx页面中的内容,根据获取的内容返回字符串

    //获取返回的信息
        function getResponseData(params,type)
        {
            
var objHttp = new ActiveXObject("Microsoft.XMLHTTP");
            
var getStr;
            objHttp.Open(
"Get","GetData.aspx?params="+params+"&Type="+type+"&nowTime="+getNowTime(),false);
            objHttp.Send();
            getStr 
= objHttp.responseText;
            
return getStr;
        }
·getEmployee()获取employee信息,由getResponseData()中得到的字符串获得各个字段的值,分别赋值给对应的控件
//获取Employee
        function getEmployee(params)
        {
            
var i,j;
            
var getStr = getResponseData(params,"GetEmployee");
            
if( getStr=='0')
            {
                window.alert(
"没有数据");
            }
            
else
            {
                document.getElementById(
"txtLastName").value= getValue(getStr)[0];
                document.getElementById(
"txtFirstName").value= getValue(getStr)[1];
                document.getElementById(
"txtTitle").value= getValue(getStr)[2];
                document.getElementById(
"txtTitleOfCourtesy").value= getValue(getStr)[3];
                document.getElementById(
"txtBirthDate").value= getValue(getStr)[4];
                document.getElementById(
"txtHireDate").value= getValue(getStr)[5];
                document.getElementById(
"txtAddress").value= getValue(getStr)[6];
                document.getElementById(
"txtCity").value= getValue(getStr)[7];
                document.getElementById(
"txtRegion").value= getValue(getStr)[8];
                document.getElementById(
"txtPostalCode").value= getValue(getStr)[9];
                document.getElementById(
"txtCountry").value= getValue(getStr)[10];
                document.getElementById(
"txtHomePhone").value= getValue(getStr)[11];
                document.getElementById(
"txtExtension").value= getValue(getStr)[12];
                document.getElementById(
"txtPhoto").value= getValue(getStr)[13];
                document.getElementById(
"txtNotes").value= getValue(getStr)[14];
                document.getElementById(
"txtReportsTo").value= getValue(getStr)[15];
                document.getElementById(
"txtPhotoPath").value= getValue(getStr)[16];
            }
        }
        
//getStr:需要截取的字符串
        function getValue(getStr)
        {
            
var myArray = new Array();
            myArray 
= getStr.split("/");
            
return myArray;
        }

3、getData.aspx中的主要代码
GetEmployee()用来读取employee表中数据,并且返回xxxx/yy/gg/xx 形式字符串
如果没有数据则返回0
 Public Function GetEmployee(ByVal strEmployeeId As String) As String
        Response.Clear()'清空页面内容
        Dim i As Integer
        Dim str As String
        Dim dt As DataTable 
= vbp.GetEmployees(" AND EmployeeId = '" & strEmployeeId & "'")
        If dt.Rows.Count 
> 0 Then
            str 
&= dt.Rows(0)("LastName").ToString() + "/"
            str 
&= dt.Rows(0)("FirstName").ToString() + "/"
            str 
&= dt.Rows(0)("Title").ToString() + "/"
            str 
&= dt.Rows(0)("TitleOfCourtesy").ToString() + "/"
            str 
&= dt.Rows(0)("BirthDate").ToString() + "/"
            str 
&= dt.Rows(0)("HireDate").ToString() + "/"
            str 
&= dt.Rows(0)("Address").ToString() + "/"
            str 
&= dt.Rows(0)("City").ToString() + "/"
            str 
&= dt.Rows(0)("Region").ToString() + "/"
            str 
&= dt.Rows(0)("PostalCode").ToString() + "/"
            str 
&= dt.Rows(0)("Country").ToString() + "/"
            str 
&= dt.Rows(0)("HomePhone").ToString() + "/"
            str 
&= dt.Rows(0)("Extension").ToString() + "/"
            str 
&= dt.Rows(0)("Photo").ToString() + "/"
            str 
&= dt.Rows(0)("Notes").ToString() + "/"
            str 
&= dt.Rows(0)("ReportsTo").ToString() + "/"
            str 
&= dt.Rows(0)("PhotoPath").ToString() + "/"
            Response.Write(str)
        Else
            Response.Write(
"0")
        End If
        Response.End()
    End Function

4、增加,修改原理基本上与显示一样
原创粉丝点击