Chinaunix首页 | 论坛 | 博客
  • 博客访问: 35921
  • 博文数量: 12
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 184
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-22 11:52
文章分类
文章存档

2014年(12)

我的朋友

分类: C#/.net

2014-04-01 09:17:59

前面看了如何增加和删除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安装包下载:


直接上代码,重要的代码还是JS获取gridview的数据的方法。
前台





   


    

     
      
             GridLines="Horizontal" PageChange="TextBox" AutoGenerateColumns="False" SortMode="Total" ForeColor="#333333"
       CellSpacing="1" Width="60%" ShowNoRecordTip="True">
    


      
      
         <%#Container.DataItemIndex +1%>
        
      

      
           序号
      

      



      
      
        <%#Eval("p_name")%>
      

      
           姓名
      

      



       
      
        <%#Eval("p_age")%>
      

      
           年龄
      

      

      
      
      
        <%#Eval("p_sex")%>
      

      
           性别
      

      



       
      
       
      

      
           备注
      

      

             
      
      
      
      
           操作
      

      



    

    
           
           
           
           
           
           
   

   









后台接收内容的代码


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拿到数据库去保存吧,然后将是否保存成功赋值给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 < 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拿到数据库去保存吧,然后将是否保存成功赋值给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
批量保存,后台获取的值给实体
阅读(1177) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~