软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 网络编程 > ASP.NET > asp.net 通过Ajax验证当前页面数据

asp.net 通过Ajax验证当前页面数据

  烈火网(LieHuo.Net)教程 asp.net检测数据是否能够正常进数据库,代码通常都会写在aspx.cs文件中,这样直接用服务器端检测的话,就会刷新页面,从而使一些客户端控件中的数据丢失。通过ajax post的方法提交当前的页面,可以完成不刷新页面,又能检测数据的目的。
事例文件如下:

  Ajax.js -- 提供了Ajax的post方法。

  

以下为引用的内容:
Ajax = {
    requests: [],
    ERROR_MESSAGE_URL_NOT_EXISTS: "Url is doesn't  exists!",
    ERROR_MESSAGE_UNKNOW: "Unknow!",

    _requestId: 0,
    requests: [],

    _msXmlId: [
        'MSXML2.XMLHTTP.3.0',   //3.0 version is best than other MSXML2.XMLHTTP
        'MSXML2.XMLHTTP',
        'Microsoft.XMLHTTP'
    ],

    _createXhr: function(requestId) {
        var xmlHttp = null;
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e1) {
            for (var i = 0; i < this._msXmlId.length; ++i) {
                try {
                    xmlHttp = new ActiveXObject(this._msXmlId[i]);
                    break;
                }
                catch (e2) {

                }
            }
        }

        if (xmlHttp != null) {
            this.requests[requestId] = { rId: requestId, httpObj: xmlHttp };
            this._requestId++;
        }

        return xmlHttp;
    },
    post: function(boxId, url, additionalData, handlers) {
        var box = document.getElementById(boxId);
        if (!box || !url) return;

        var postData = this._getData(box);

        var xmlHttp = this._createXhr(this._requestId);
        if (xmlHttp.overrideMimeType) {
            xmlHttp.overrideMimeType("text/html");
        }

        var ajax = this;
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    if (handlers && handlers.success) {
                        handlers.success.apply(handlers.success, [xmlHttp]);
                    }
                } else {
                    if (handlers && handlers.failure) {
                        handlers.failure.apply(handlers.failure, [xmlHttp]);
                    }
                }
            }
        }

        xmlHttp.open("POST", url, true);
        //add this row.sure it is post method
        //this row must open method belowe
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.setRequestHeader("Content-Length", postData.length);
        if (additionalData && additionalData.length > 0) {
            xmlHttp.send(postData + "&" + additionalData);
        } else {
            xmlHttp.send(postData);           
        }
    },
    _getData: function(box) {
        var fields;

        var data = [];

        if (box.tagName.toLowerCase() == "form") {
            fields = box.elements;
        } else {
            fields = this._getDataElement(box);
        }

        for (var i = 0; s = fields.length, i < s; i++) {
            if ((name = fields[i].name) && (value = fields[i].value)) {
                data.push(name + "=" + encodeURIComponent(value));
            }
        }

        return data.join("&");
    },
}

相关阅读
网友评论
栏目导航
推荐软件