WebForm obtains the object class data, fills the page (pass the value using session), webformsession
Use AJAX to obtain data asynchronously and dynamically display data on the page
<1>
Create a UserService class.
Using System; using System. collections. generic; using System. linq; using System. web; using System. data; namespace ModelService {public class UserService {public static string SelectDataToJson () {// Save the Data queried from the database to a able DataTable dt = SqlHelper. executeDataTable ("select * from T_User"); // example of converting the dt table to Json format: {"Json": [{"Id": "1 ", "UserName": "Zhang San"}]} string JsonStr = DataTableToJson. dtToJson ("Json", dt); // return this Json string return JsonStr ;}}}
<2>
Create a Handler1.ashx (get data from AJAX requests)
Using System; using System. collections. generic; using System. linq; using System. web; using entity class. modelService; namespace entity class {// <summary> // summary of Handler1 /// </summary> public class Handler1: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/html"; // call the SelectDataToJson () method in the UserService class to obtain a Json string (the string stores the data queried by executing the SQL statement) string str = UserService. selectDataToJson (); // write data to the context of the browser. response. write (str) ;}public bool IsReusable {get {return false ;}}}}
<3>
WebForm1.aspx page
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "WebForm1.aspx. cs" Inherits = "entity class. WebForm1" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Pass the data to the WebForm. aspx page to display the data in the form of Session values.
<1>
First, create an object class User
Using System; using System. collections. generic; using System. linq; using System. web; namespace entity class {public class User {public int Id {get; set;} public string UserName {get; set;} public int Age {get; set ;} public int Gender {get; set ;}}}
<2>
Call SqlHelper to convert the queried data into a list object (map the queried data to the object class)
Using System; using System. collections. generic; using System. linq; using System. web; using System. data; namespace entity class. modelService {public class UserService {public static List <User> SelectDataToEntity () {// by querying the database, convert the obtained data into a list List <User> list = SqlHelper. selectDataToList <User> ("select * from T_User"); return list ;}}}
<3>
WebForm1.aspx. cs page (Note: The WebForm1.aspx page inherits the WebForm1.aspx. cs class)
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using entity class. modelService; namespace entity class {public partial class WebForm1: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {// store the obtained list (List <User> lsit) to the Session. You can then traverse this list on the WebForm1.aspx page to obtain the object class data Session. Add ("User", UserService. SelectDataToEntity ());}}}
<4>
WebForm1.aspx page
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "WebForm1.aspx. cs" Inherits = "entity class. WebForm1" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">