原文作者:楊友山
10年積累的網(wǎng)站建設(shè)、成都網(wǎng)站制作經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先做網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有井岡山免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
原文地址:http://blog.csdn.net/yysyangyangyangshan/article/details/22438077
前面看了如何增加和刪除Gridview數(shù)據(jù),但是無論怎樣Gridview的數(shù)據(jù)除了展示給用戶外,還需要進行編輯后保存的。
也就是將Gridview的數(shù)據(jù)獲取下來傳到后臺,然后后臺經(jīng)過識別和處理獲得數(shù)據(jù),保存至數(shù)據(jù)庫中。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 DataTable(); 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"] = "西北白楊樹"; nrow["p_age"] = 27; nrow["p_sex"] = "男"; 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; //獲取到了list拿到數(shù)據(jù)庫去保存吧,然后將是否保存成功賦值給savesuccess,前臺根據(jù)這個變量來提示 //這里是要生成一個json字符串給前臺提示用 //可以多加幾個屬性,讓提示內(nèi)容更豐富 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; //獲取到了list拿到數(shù)據(jù)庫去保存吧,然后將是否保存成功賦值給savesuccess,前臺根據(jù)這個變量來提示 //這里是要生成一個json字符串給前臺提示用 //可以多加幾個屬性,讓提示內(nèi)容更豐富 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(); } }
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; } } }