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>