Ajax Dynamic Database loading example

Source: Internet
Author: User

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;
}
}

}

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.