The WebForm. aspx page accesses the methods in the WebForm. aspx. cs class through AJAX to obtain data, aspx. cs
The WebForm. aspx page uses AJAX to access methods in the WebForm. aspx. cs class to obtain data.
WebForm1.aspx page (native AJAX request, method 1)
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "WebForm1.aspx. cs" Inherits = "IsPostBack. WebForm1" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
WebForm1.aspx page (native AJAX request, written in 2, which is usually used)
<Head runat = "server"> <title> </title> <script src = "jquery-1.11.2.js" type = "text/javascript"> </script> <script type = "text /javascript "> function btnClick () {$. ajax ({url: "WebForm1.aspx", type: "Post", dataType: "Text", // data type returned by the request to the server: {"id ": "2"}, success: function (data) {var obj = $. parseJSON (data); // var name = obj. json [0]. userName; var age = obj. json [0]. age; document. getElementById ("name "). innerHTML = name; document. getElementById ("age "). innerHTML = age ;}})} </script>
WebForm1.aspx. cs if you use Response. End () in try... catch, an exception will be caught: The thread is aborted. Solution: Click
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using System. data. sqlClient; using System. data; namespace IsPostBack {public partial class WebForm1: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {string id = Request ["id"]; // This id is not obtained if data is not submitted through ajax requests, therefore, the id is null. However, if you submit data through an ajax request, because the submitted data contains a submission id, you can obtain this id, and the id has a value. If (! String. isNullOrEmpty (id) // if data is not submitted through ajax requests, the GetUserData (string id) method {GetUserData (id) is not called in curly brackets ); // if the data is submitted through an ajax request, the GetUserData (string id) method will be called in curly brackets} void GetUserData (string id) {DataTable dt = SqlHelper. executeDataTable ("select * from T_User where id = @ id", new SqlParameter ("id", id); string data = DataTableConvertJson. dataTableToJson ("Json", dt); Response. write (data); Response. end ();// Send all the current buffered output to the client to stop the execution of the page. If this step is not completed, the program will execute later. In addition to outputting data to the client, the html code in webForm1.aspx is also output to the page. }}}