In some page processing, if you need to submit the processing in the background multiple times, the page will be refreshed constantly, which is annoying, especially when the machine configuration is poor or the network condition is poor, it is even more uncomfortable.
In this case, XMLHTTP can be used to implement non-Refresh commit, which has many such applications during ASP, and is also used in Asp.net.
First, create a page named send. aspx for the page to be submitted and do. aspx for the other page to process the submitted page.
On the send. ASPX page, there is an input box, which serves as the data to be submitted to the background,
<InputID= "Txtuser"Type= "Text"Runat= "Server">
Write another JS submission method,CodeAs follows:
Function Senddata ()
{
VaR XH = New Activexobject ( " Msxml2.xmlhttp " )
XH. Open ( " Post " , " Do. aspx? Id = 1 " , False );
Xh.send(document.all.txt user. value );
Alert (XH. responsetext ); // Print the returned data of XMLHTTP
}
This js method is very simple. Define an XMLHTTP object and then call the open and send methods.
Use a button on the page to call this method and send it to do. aspx.
On the processed page, do. aspx is in the corresponding CS file do. aspx. CS to receive the data sent in the past:
Int ID = 0 ;
If (Request. Params [ " ID " ] ! = Null )
{
//Receive parameters passed from the URL
ID= Int. Parse (request. Params ["ID"]);
}
// Receives the data transmitted from the XH. Send () method.
System. Io. Stream stream = Request. inputstream;
System. Io. binaryreader br = New System. Io. binaryreader (stream, system. Text. encoding. Default );
Int Len = Int . Parse (stream. length. tostring ());
Byte [] B = BR. readbytes (LEN );
// Here we can perform various Processing Based on the passed parameters and data.
//
// The data returned to the sending page is transmitted through response. Write ().
// The parameters and data sent here are returned.
Response. Write (ID + System. Text. encoding. Default. getstring (B ));
The whole process is OK.
Note: On the HTML code page of the do. aspx file, if any front-end code exists, it will be returned as the content of the send () request and be returned to XH. responsetext.
In addition, you can use HTML pages to replace the above send. aspx, and use the same method to submit data for static pages.