The idea and implementation code of ASP. NET to get data without refreshing

Source: Internet
Author: User

1. Get a piece of text from the remote background

JavaScript code:

Function getdata (STR)
{
VaR ohttpreq = new activexobject ("msxml2.xmlhttp ");
Ohttpreq. Open ("Post", "notrefreshserver. aspx", false); // notrefreshserver. aspx is used to output data to the client.
Ohttpreq. Send ();
VaR result = ohttpreq. responsetext
Dialog. innerhtml = dialog. innerhtml + result; // dialog is a client control used to display data.
}

CS code:

String strputput = @ "PP: How are you? How are you? <Br> ";
Response. Buffer = true;
Response. Clear ();
Response. clearcontent ();
Response. clearheaders ();
Response. contenttype = "text/html ";
Response. charset = "UTF-8 ";
Response. Write (strputput );
Response. Flush ();
Response. Close ();

Put this code in the background page_load event of the notrefreshserver. ASPX page, then the result on the client will receive "PP said: Hello? How are you? <Br> "code.

2. Obtain the remote XML Dataset

The preceding Code retrieves a piece of text from the remote background. To obtain a remote XML dataset, use the following code:

Suppose there is an XML file with the following content:

<AA>
<A>
<B> B1 </B>
</A>
<A>
<B> b2 </B>
</A>
<A>
<B> B3 </B>
</A>
</AA>

JavaScript code:

Function getdata (STR)
{
VaR ohttpreq = new activexobject ("msxml2.xmlhttp ");
Ohttpreq. Open ("Post", "notrefreshserver. aspx", false); // notrefreshserver. aspx is used to output data to the client.
Ohttpreq. Send ();
VaR result = ohttpreq. responsetext;
VaR odoc = new activexobject ("msxml2.domdocument ");
Odoc. loadxml (result );
Items1 = odoc. selectnodes ("// AA/A/B ");
For (VAR I = 0; I <items1.length; I ++)
{
// Items1 [I]. Text to obtain the data such as "B1" and "B2", and process it here ..........
}
}

CS code:

String strpathfile = "XXX"; // relative path of the XML file
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (strpathfile );
Xmltextwriter xtw = new xmltextwriter (response. outputstream, response. contentencoding );
Xmldoc. writeto (xtw); // if you want to obtain data from the database, use Ds. writexml (xtw); DS is the DataSet object.
Xtw. Formatting = formatting. indented;
Xtw. indentation = 4;
Xtw. indentchar = '';
Xtw. Flush ();
Response. End ();
Xtw. Close ();

Put this code in the background page_load event of the notrefreshserver. ASPX page. Then, the result of the client will receive the content of the XML file, and then display the data in the XML file.

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.