using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ViewImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        RememberOldValues();
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
        RePopulateValue();
        e.Cancel = true;
    }
    private void RememberOldValues()
    {
        ArrayList al = null;   //声明集合
        //创建一个Session并检查是否为空，这个Session用来保存集合
        //这里是判断是否是第一次进行分页
        if(Session ["checked_items"]!=null )
        {
            al=(ArrayList )Session ["checked_items"];

        }
        else 
        {
            al=new ArrayList ();
        }
        int index=-1;
        foreach (GridViewRow row in GridView1.Rows )
        {
            index = (int)GridView1.DataKeys[row.RowIndex].Value;
            bool result = ((CheckBox)row.FindControl("CheckBox2")).Checked;
            if (result)
            {
                if (!al.Contains(index))
                {
                    al.Add(index);
                }
            }
            else
            {
                if (al.Contains(index))
                {
                    al.Remove(index);
                }
            }
        }
        if (al != null && al.Count > 0)
        {
            Session["checked_items"] = al;
        }

    }
    private void RePopulateValue()
    {
        ArrayList al = (ArrayList)Session["checked_items"];
        if (al != null && al.Count > 0)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                int index = (int)GridView1.DataKeys[row.RowIndex].Value;
                if (al.Contains(index))
                {
                    CheckBox mycheckbox = (CheckBox)row.FindControl("CheckBox2");
                    mycheckbox.Checked = true;
                }
            }
        }
    }

    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cbx = (CheckBox)sender;
        //遍历GridViewRow将其中的check控件的状态设置
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("CheckBox2");
            ch.Checked = cbx.Checked;
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DB db = new DB();
        //首先判断gridview是否有值，如果有才可以进行删除
        if (GridView1.Rows.Count > 0)
        {//通过调用保存状态方法盘判断那个记录被选中
            RememberOldValues();
            //从Session对象中取得集合的内容
            ArrayList al = (ArrayList)Session["checked_items"];
            if (al != null)
            {//调用删除多条的方法进行删除操作
                db.deletemore(al);
                GridView1.DataBind();
            }
        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }


   
}
