为XX人写的DEMO样例,功能实现:gridview 在列表中对信息进行增删改,通过VIEWSTATE进行缓存,然后一次性将数据提交到服务器端。 下载地址:DOWNLOAD

 

代码:

 

 

 

 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           DataTable dt = GetData();
           this.GridView1.DataSource = dt;
           this.GridView1.DataBind();
        }
    }

 

    public DataTable GetData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Title");
        dt.Columns.Add("ID");

        DataRow dr = dt.NewRow();
        dr["Title"] = "测试标题";
        dr["ID"] = "1";
        dt.Rows.Add(dr);

        DataRow dr1 = dt.NewRow();
        dr1["Title"] = "测试标题";
        dr1["ID"] = "1";
        dt.Rows.Add(dr1);
       
        //新增行处理
        if (ViewState["ADDIdS"] != null)
        {
            string idAdds = ViewState["ADDIdS"].ToString();
            idAdds = idAdds.TrimStart(char.Parse(","));
            string[] items = idAdds.Split(char.Parse(","));

            for (int i = 0; i < items.Length; i++)
            {
                DataRow dtOne = dt.NewRow();
                dtOne["ID"] = (items[i].Split(char.Parse("|")))[0];
                dtOne["Title"] = (items[i].Split(char.Parse("|")))[1];
                dt.Rows.Add(dtOne);
            }
       
        }
        //删除行处理 -- 自己搞定
        if (ViewState["DeleteIdS"] != null)
        {
            string[] ids = (ViewState["DeleteIdS"].ToString()).TrimStart(char.Parse(",")).Split(char.Parse(","));
            for (int i = 0; i < ids.Length; i++)
            {
                for (int ii = 0; ii < dt.Rows.Count; ii++)
                {
                    if (dt.Rows[ii]["ID"].ToString() == ids[i])
                    {
                        dt.Rows[ii].Delete();
                        break;
                    }
                }
            }  
       
        }

      
        return dt;
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (ViewState["DeleteIdS"] == null)
        {
            ViewState.Add("DeleteIdS", string.Format(",{0}", GridView1.DataKeys[e.RowIndex].Value.ToString()));
        }
        else
        {
            ViewState["DeleteIdS"] = ViewState["DeleteIdS"].ToString() + string.Format(",{0}", GridView1.DataKeys[e.RowIndex].Value.ToString()); // ViewState["DeleteIdS"]
        }

        //检索增加记录,看看是否存在删除的ID
        string idAdds = ViewState["ADDIdS"] != null ? ViewState["ADDIdS"].ToString() : null;
        if (idAdds != null)
        {
            idAdds = idAdds.TrimStart(char.Parse(","));
            string[] items = idAdds.Split(char.Parse(","));

            //过滤相关记录并进行组装,
            for (int i = 0; i < items.Length; i++)
            {
            }
        }


        DataTable dt = GetData();
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ViewState["ADDIdS"] == null)
        {
            ViewState.Add("ADDIdS", string.Format(",{0}|{1}",Guid.NewGuid().ToString(),this.TextBox1.Text));
        }
        else
        {
            ViewState["ADDIdS"] = ViewState["ADDIdS"].ToString() + string.Format(",{0}|{1}",Guid.NewGuid().ToString(),this.TextBox1.Text); // ViewState["DeleteIdS"]
        }

        DataTable dt = GetData();
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        //新增行数据
        string idAdds = ViewState["ADDIdS"] != null? ViewState["ADDIdS"].ToString() : null;            //
        //删除行数据
        string[] ids = ViewState["DeleteIdS"] != null ? null:(ViewState["DeleteIdS"].ToString()).TrimStart(char.Parse(",")).Split(char.Parse(","));

        //新增行处理
        if (idAdds != null)
        {
            idAdds = idAdds.TrimStart(char.Parse(","));
            string[] items = idAdds.Split(char.Parse(","));

            //新增行处理
            for (int i = 0; i < items.Length; i++)
            {
            }           
        }


       
    }

 

posted on 2010-05-27 09:38  大鱼  阅读(493)  评论(0编辑  收藏  举报