前面看了如何增加和删除Gridview数据,但是无论怎样Gridview的数据除了展示给用户外,还需要进行编辑后保存的。也就是将Gridview的数据获取下来传到后台,然后后台经过识别和处理获得数据,保存至数据库中。主要原理还是前面的$.ajax方式传数据。要用到的就是JS的对象了。传到后台的是字符串,经过截取,获得数据。这里需要做一个准备工作,安装asp.net ajax ,因为项目中的关于json的操作需要用到C:\Program Files\Microsoft ASP.NET\ASP.NET 2.0 AJAX Extensions\v1.0.61025下的System.Web.Extensions.dll和System.Web.Extensions.Design.dll,asp.net ajax安装包下载:http://download.csdn.net/detail/yysyangyangyangshan/7114393直接上代码,重要的代码还是JS获取gridview的数据的方法。前台后台接收内容的代码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            {                for (int i = 0; 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拿到数据库去保存吧,然后将是否保存成功赋值给savesuccess,前台根据这个变量来提示            //这里是要生成一个json字符串给前台提示用            //可以多加几个属性,让提示内容更丰富            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                {                    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拿到数据库去保存吧,然后将是否保存成功赋值给savesuccess,前台根据这个变量来提示            //这里是要生成一个json字符串给前台提示用            //可以多加几个属性,让提示内容更丰富            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; }        }    }效果如下,单行保存SouthEast批量保存,后台获取的值给实体
09-22 02:25