Implement ASP. NET refreshing commit using XMLHTTP

Source: Internet
Author: User

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.

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.