Copy codeThe Code is as follows:
<! 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> </title>
<Script type = "text/javascript">
Function btnClick (){
Var xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
If (! Xmlhttp ){
Alert ("An error occurred while creating the xmlhttp object! ");
Return false;
}
Var text1 = document. getElementById ("Text1 ");
Xmlhttp. open ("post", "GetPrice2.ashx? Ts "+ text1, false );
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4 ){
If (xmlhttp. status = 200 ){
Document. getElementById ("Text2"). value = xmlhttp. responseText;
}
Else {
Alert ("Ajax Return Error! ");
}
}
}
Xmlhttp. send ();
}
</Script>
</Head>
<Body>
<P>
Product Name: <input id = "Text1" type = "text"/> </p>
<P>
Price: <input id = "Text2" type = "text"/> </p>
<P>
<Input id = "Button1" type = "button" value = "query" onclick = "btnClick ()"/> </p>
</Body>
</Html>
Copy codeThe Code is as follows:
<% @ WebHandler Language = "C #" Class = "GetPrice" %>
Using System;
Using System. Linq;
Using System. Web;
Using DataSetProductsTableAdapters;
Public class GetPrice: IHttpHandler {
Public void ProcessRequest (HttpContext context)
{
Context. Response. ContentType = "text/plain ";
// Context. Response. Write ("Hello World ");
String name = context. Request ["text1"];
Var data = new PriceTableAdapter (). GetDataByName (name); // you need to create a strongly Typed dataset.
If (data. Count <= 0)
{
Context. Response. Write ("none | 0 ");
}
Else
{
Context. Response. Write ("OK |" + data. Single (). Price );
}
}
Public bool IsReusable {
Get {
Return false;
}
}
}