Asynchronous Ajax data calling using ashx

Source: Internet
Author: User

Ashx file,. Net General processing program. Inherit from the ihttphandler interface, and httpcontext implements individual HTTP requests.

Implementation: use the default northwind database of local SQL2000

<% @ Webhandler Language = "C #" class = "handler" %>

Using system;
Using system. Web;
Using system. Data;
Using system. Data. sqlclient;
Using system. text;

Public class handler: ihttphandler {

Public void processrequest (httpcontext context)
{

/* --------------------------- Custom code ----------------------------*/
String STR = @ "Server =.; uid = sa; Password = sa; database = northwind ";

// Retrieve the table in the database

Sqlconnection con = new sqlconnection (STR );
Dataset DS = new dataset ();
Sqldataadapter SDA = new sqldataadapter ();
SDA. selectcommand = new sqlcommand ("select * from employees", con );
Con. open ();
SDA. Fill (DS );
Con. Close ();
// Fill the table in the datatable
Datatable dt = Ds. Tables [0];

// Construct the output stream format
Stringbuilder strpart = new stringbuilder ();
Strpart. append ("<Table> ");
For (INT I = 0; I <5; I ++)
{
Strpart. appendformat ("<tr> <TD> <a href = 'default. aspx? Id = {0} '> {1} </a> </TD> </tr> ", DT. rows [I] ["employeeid"], DT. rows [I] ["lastname"]);
}
Strpart. append ("</table> ");
String S = strpart. tostring ();

// Streams are returned to the page according to the HTTP local request
Context. response. Write (s );


}

Public bool isreusable
{
Get {
Return false;
}
}

}

Run this ashx file to copy the address

Call this ashx file on the Static Page

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> No title page </title>

<SCRIPT type = "text/JScript">
Function list (URL, divlist) // address of the ashx file, leaving the output Div ID (parameter)
{
VaR XMLHTTP;
Try
{
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Catch (E)
{
XMLHTTP = new XMLHttpRequest ();
}

XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = function ()
{
If (XMLHTTP. readystate = 4)
{
If (XMLHTTP. Status = 200)
{
Document. getelementbyid (divlist). innerhtml = XMLHTTP. responsetext;

}
}
}
XMLHTTP. Send (null );
}
</SCRIPT>

</Head>
<Body onload = "list ('HTTP: // localhost: 14379/webhtmlpartdynamic/handler. ashx', 'divlist')">
<Div id = "divlist">
</Div>

<Div id = "divtest">
<Input id = "button1" type = "button" value = "button" onclick = "list ('HTTP: // localhost: 14379/webhtmlpartdynamic/handler. ashx', 'divtest') "/>
</Div>

</Body>
</Html>

 

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.