Asp.net verifies the data on the current page through Ajax

Source: Internet
Author: User

  Tutorial on helping customers (www.bkjia.com)Asp.net checks whether data can be normally imported into the database. The code is usually written in aspx. in the cs file, if you directly use the server side detection, the page will be refreshed, so that data in some client controls is lost. By submitting the current page through ajax post, you can do this without refreshing the page and detect data.
The example file is as follows:

Ajax. js -- provides the Ajax post method.

  

Reference content is as follows:
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 ("&");
},
}

  • Three pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.