Ajax more focused on the front page asynchronous display of the Background database field, the main idea is similar to the previous article, focusing on the DAL layer to get stored in the process of the table collection dataset, in general stored procedures when stitching strings, the table row of data into the form of key value pairs
1. Front page contains script (AJAX) and basic HTML tags to facilitate the search and understanding
The code is as follows |
Copy Code |
function InitData () {//page load Call this event $.ajax ({ Type: "POST", URL: "/ajaxrequest/getuserlist.ashx",//link to the corresponding generic handler Data: {state:1}, Success:function (data) { if (data!= null) { var html = "<li ><span class=" LB1 >ID</span><span> username </span><span > creation time </span ><span > Perform operation </span></li> "; Splice string tags and put them in variable HTML, note: stitching "needs to be escaped-like the class in the first span $.each (data, function (I, n) {//jquery each row of the output list, if you use JavaScript to consider using a for loop HTML + + "<li><span class=" LB1 ">" + (1 + i) + "</span><span >" + n.user_name + "</span><s Pan > "+ n.user_time + </span><span ><a href=" javascript:editprovince (' "+ this. User_name + "'," + this. user_id + "," + (i + 1) + ")" >[edit]</a></span><span ><a href= "javascript:deleteprovince" + this. USER_ID + ");" onclick= "Javascript:return confirm (' You are sure you want to delete" +this. User_name+ "?"); >[delete]</a></span></li> "; Still stitching to show the mark, note: variable html+=, in addition, if it is part of the content or a different part of each line to use the variable Variable with data[i]. Key (You can get the value of the key in the JSON object in the file that the URL points to), which is the key value pair when the JSON object is spliced in the generic handler file. n here represents Data[i] (is the jquery from each usage, if it is in JavaScript can use Data[i]. USER_ID) }); $ ("#list_ul"). Append (HTML); Get the UL to display the list, add the append label string with the
} else { Alert ("error!"); } } }) } |
2. General handler Getuserlist.ashx, converts data from the underlying dataset into a JSON object and returns it to the calling page
The code is as follows |
Copy Code |
public void ProcessRequest (HttpContext context) { Context. Response.ContentType = "Text/plain"; string result = "["; String name=string. Empty; Int? state = null; DateTime time; Int id=0; if (!string. IsNullOrEmpty (context. request["State")) State = Convert.ToInt32 (context. request["State"]); DataSet _set=new BLL. Province (). Getprovincelist (state); foreach (DataRow Dr in _set. Tables[0]. Rows) { Name=convert.tostring (dr["user_name"]); Id=convert.toint32 (dr["user_id"]); Time=convert.todatetime (dr["User_time"]); result+= "{" user_name ":" "+name+" "," user_id ":" "+id+" "," User_time ":" "+time+" "},"; } resul= result. Trim (",") + "]"; Context. Response.Write (Result); } |