ASP.NET-007: Save the GridView data

Source: Internet
Author: User

 

We have seen how to add and delete the Gridview data. However, in addition to displaying the data of the Gridview, you also need to edit the data and save it.

That is, the data of the Gridview is retrieved and uploaded to the background. The background is identified and processed to obtain the data and saved to the database.
The main principle is the previous $. ajax method for data transmission. The JS object is used. The string is uploaded to the backend, Which is intercepted to obtain data.


Here, we need to make preparations to install asp.net ajax, because the json operations in the project need to be used
C: Under Program FilesMicrosoft ASP. NETASP. NET 2.0 AJAX Extensions1.0.61025
System. Web. Extensions. dll and System. Web. Extensions. Design. dll,
Asp.net ajax installation package download: http://download.csdn.net/detail/yysyangyangyangshan/7114393


Directly run the code. The important code is the method for JS to obtain the data of the gridview.
Front-end
  

Code of the content received in the background

 

Public partial class ChildFrm: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {switch (Request [operateType]) {case SaveAll: SaveAllData (); return; case SaveSingleData: SaveSingleData (); return; default: break;} if (! IsPostBack) {DataTable dt = InitData (); this. dgPersons. dataSource = dt; this. dgPersons. dataBind () ;}} private DataTable InitData () {DataTable PersonCollect = new able (); PersonCollect = new DataTable (); PersonCollect. columns. add (p_id); PersonCollect. columns. add (p_name); PersonCollect. columns. add (p_age); PersonCollect. columns. add (p_sex); PersonCollect. columns. add (p_remark); if (PersonCollect. rows. count <1) {for (int I = 0; I <11; I ++) {DataRow nrow = PersonCollect. newRow (); nrow [p_id] = System. guid. newGuid (). toString (); nrow [p_name] = poplar trees in the northwest China; nrow [p_age] = 27; nrow [p_sex] = male; nrow [p_remark] = string. empty; PersonCollect. rows. add (nrow) ;}} return PersonCollect;} protected void SaveSingleData () {string id = Request [snID]. toString (). trim (); string name = Request [Name]. toString (). trim (); string age = Request [Age]. toString (). trim (); string sex = Request [Sex]. toString (). trim (); string remark = Request [Remark]. toString (). trim (); bool savesuccess = true; // obtain the list and save it to the database. Then, assign whether the list is saved successfully to savesuccess, the foreground prompts according to this variable. // here, a json string is generated for the foreground prompt. // you can add several attributes to the prompt to enrich the ArrayList list = new ArrayList (); hashtable ht = new Hashtable (); ht. add (Success, savesuccess. toString (). toLower (); list. add (ht); JavaScriptSerializer json = new JavaScriptSerializer (); string jsonResult = json. serialize (list); SendTextMessage (jsonResult);} protected void SaveAllData () {string id = Request [ID]. toString (); string name = Request [Name]. toString (); string age = Request [Age]. toString (); string sex = Request [Sex]. toString (); string remark = Request [Remark]. toString (); string [] ids = SplitString (id); string [] names = SplitString (name); string [] ages = SplitString (age ); string [] sexs = SplitString (sex); string [] remarks = SplitString (remark); List
 
  
ResultList = new List
  
   
(); If (ids! = Null & ids. length> 0) {for (int I = 0; I <ids. length; I ++) {Person m = new Person (); m. ID = ids [I]; m. name = names [I]; m. age = ages [I]; m. sex = sexs [I]; m. remark = remarks [I]; resultList. add (m) ;}} bool savesuccess = true; // obtain the list and save it to the database. Then, assign whether the list is saved successfully to savesuccess, the foreground prompts according to this variable. // here, a json string is generated for the foreground prompt. // you can add several attributes to the prompt to enrich the ArrayList list = new ArrayList (); hashtable ht = new Hashtable (); ht. add (Success, savesuccess. toString (). toLower (); list. add (ht); JavaScriptSerializer json = new JavaScriptSerializer (); string jsonResult = json. serialize (list); SendTextMessage (jsonResult);} private string [] SplitString (string value) {if (string. isNullOrEmpty (value) {return new string [0];} return value. split (',');} private List
   
    
GetResults (bool result, string message) {List
    
     
Response = new List
     
      
(); Results r = new Results (); r. success = result. toString (). toLower (); r. message = message; response. add (r); return response;} protected void SendTextMessage (string message) {Response. contentType = text/plain; Response. write (message); Response. end ();}}
     
    
   
  
 

 

A single row is encapsulated as an entity.
  public class Person    {        private string id;        public string ID        {            get { return id; }            set { id = value; }        }        private string name;        public string Name        {            get { return name; }            set { name = value; }        }        private string age;        public string Age        {            get { return age; }            set { age = value; }        }        private string sex;        public string Sex        {            get { return sex; }            set { sex = value; }        }        private string remark;        public string Remark        {            get { return remark; }            set { remark = value; }        }    }

The effect is as follows,
Save a single row

Batch save, and the value retrieved by the background is given to the object


Code download: http://download.csdn.net/detail/yysyangyangyangshan/7114605

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.