03-22 Ajax verification user logon, 03-22ajax
In a webpage, data is usually submitted through a form, and the form obtains information, discarding the current page and reloading a new page.
Now, you can use JueryAjax to submit and process data on the webform webpage to asynchronously refresh the page.
What is the difference between form-submitted data and JueryAjax-submitted data:
Form |
Form to get information, discard the current page and reload a new page. (Only submit data) |
JueryAjax |
Submit data, send it out, and then obtain data and update information without refreshing the page. (Submit and report back) |
Take verifying that the user name is repeated as an example:
Write a function in Html:
1 document. ready {}: trigger function content 2 $ document. ready (3 {4 function () // obtain the value of the text box 5 {6 $ ("# TextBox1 "). blur (function () // triggers 7 {8 var txt =$ (this) when the mouse clicks or leaves ). val (); // this indicates the current element, and 9} 10 var txt = $ ("# TextBox1") indicates the element written in the event of the current element ") // The CSS selector obtains the element 11 var txt =$ ("# TextBox1 "). val (); // The value of the element obtained in html is in val; the value of the element obtained in cs is in value 12 // submit the data through the hyperlink, feedback 13 // how to use Ajax to send the value of the text box 14 $. ajax ({15 url: "CheckuserName. aspx "; // The parameter 16 type:" "passed by the hyperlink; // the method in which the data is transmitted 17 data: {id: txt }, // send the user name out 18 datatype: "xml"; // the data type returned 19 success function (data) // The data here is the returned xml Information, different from the data above, the data name here can be changed to 20 {21 var co = $ (data ). find ("count "). text (); // how to obtain the returned variable 22 alert (co); // alert executes the output and obtains the value (usually used) 23 if (parseInt (c) = 0) 24 {25 $ ("Label1" ).html (); 26 var lb1 = document. getElementById ("Label1"); 27 lb1.innerHtml = ""; 28} 29 else30 {31 $ ("Label1" ).html ("this user already exists "); 32} 33} 34}) 35 36 37} 38 })
In Page_Load of Dafualt2.aspx:
In page_load:
Private DataClassesDataContext _ Context;
Protected void Page_Load (object sender, EventArgs e)
{
Connect to database
_ Context = new DataClassesDataContext ();
String uid = request ["id"]. tostring (); // verify the id
Int count = _ Context. Login. Where (r => r. username = uid). count (); // query from the database
Response. write ("<? Xml. version = '1. 0'?> "); // Return data to the page
Response. write ("<count>" + count + "</count>"); // you can customize tags in xml. html is an existing tag.
Response. close ();
XML is also a markup language used to mark content.
With <? Xml. version = '1. 0'?> It indicates that this is an XML file. Xml documents are used by default during parsing.
}