How to write Ajax for asynchronous refreshes

Source: Internet
Author: User

The so-called asynchronous refresh is not to refresh the entire Web page to update data.

Ajax can be achieved only through JS, and then asynchronous refreshes are implemented

The difference between the form submission data and the AJAX submission data: The form submission is the data from the entire page submitted, and the previous page (Refresh page) is discarded when the data is submitted; Ajax extracts some data from the current page and submits it, and receives the returned data, which is processed and then displayed on the current page (without refreshing the page).

  

"Example" = = Verify that the user name is repeated = =

  

Use the idea of Ajax: Write JS and Ajax code in the page to be refreshed, submit the data to another page, write the query code in Page_Load and return the results to the refreshed page.

  

  

Interface

I. Using LINQ to connect to a database

Second, import the jquery file into the project. Code written in

  

<script src= "Js/jquery-1.2.3.pack.js" type= "Text/javascript" ></script>

Third, write the following code on the refreshed page. Code written in

  

<script src= "Js/jquery-1.2.3.pack.js" type= "Text/javascript" ></script> <%--call Jquery--%> < Script> $ (document). Ready (//Trigger function Getval () {$ ("#TextBox1") when the page has finished loading. blur (Fu                                        Nction () {///when the mouse clicks or leaves, the var txt = $ (this) is triggered. Val ();//Gets the value of the text box//use AJAX to send out the value of the text box                        $.ajax ({url: "de.aspx", type: "POST",                        Data: {id:txt},//receive the information returned by the database datatype: "XML",                            Data in the Success:function (data) {//data is the data in the De page that is the Count of var co = $. Text (); if (parseint (CO) = = 0) {var lbl = document.getElementById ("Label1")                            ;//Use JS output lbl.innerhtml = "√"; } else {var lbl = Document.getelemEntbyid ("Label1");                            lbl.innerhtml = "This user name is registered";                }                                                    }                    });            });    }); </script>

Iv. write the following code in the Page_Load on the page where the value is passed

  

protected void Page_Load (object sender, EventArgs e)    {        //query data sent over       dataclassesdatacontext  dc = new Dataclassesdatacontext ();        String uid = request["id"]. ToString ();        int count = DC. Stopro.where (r = r.stoid = = Int. Parse (UID)). Count ();        Returns Response.Write as XML        ("<?xml varsion= ' 1.0 '?>");        Response.Write ("<count>" + Count + "</count>");        Response.End ();//Turn off Response    }

Complete!

How to write Ajax for asynchronous refreshes

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.