ASP.NET - 如何一次检查gridview页面中的所有复选框,并跨页面访问gridview中的已检查行?

时间:2014-11-12 01:17:38

标签: c# asp.net gridview checkbox paging

我有一个gridview,允许您下载所检查行的文件。它将文件下载到.zip文件中。一切都很好,除了两件事:

  1. 如果我选择“全部检查”,它只会检查gridview第一页中的所有内容。如果有足够的数据来拥有多个页面,我怎样才能使check-all实际选择ALL?
  2. 如果我在不同页面上选中了复选框,我单击了一个应该下载所有文件的按钮,它只会下载我当前正在查看的页面的文件。因此,如果我正在查看gridview第2页,并且我下载了已检查的行,它将只下载我在第2页中检查过的文件 - 即使我在第1页中选中了复选框。
  3. 这是我的代码:

    复选框和Gridview:

     private void RemoveRowIndex(int index)
            {
    
                SelectedShotIndex.Remove(index);
    
            }
    
            private void PersistRowIndex(int index)
            {
                if (!SelectedShotIndex.Exists(i => i == index))
                {
                    SelectedShotIndex.Add(index);
                }
            }
    
            private List<Int32> SelectedShotIndex
            {
                get
                {
                    if (ViewState[SELECTED_SHOT_INDEX] == null)
                    {
                        ViewState[SELECTED_SHOT_INDEX] = new List<Int32>();
                    }
    
                    return (List<Int32>)ViewState[SELECTED_SHOT_INDEX];
                }
            }
    
            private void RePopulateCheckBoxes()
            {
                foreach (GridViewRow row in gvSearchResultsUserAdmin.Rows)
                {
                    var chkBox = row.FindControl("chkShot") as System.Web.UI.WebControls.CheckBox;
    
                    IDataItemContainer container = (IDataItemContainer)chkBox.NamingContainer;
    
                    if (SelectedShotIndex != null)
                    {
                        if (SelectedShotIndex.Exists(i => i == container.DataItemIndex))
                        {
                            chkBox.Checked = true;
                        }
                    }
                }
            }
    
            protected void gvSearchResultsUserAdmin_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                gvSearchResultsUserAdmin.PageIndex = e.NewPageIndex;
    
                if (Session["gvSearchResultsUserAdmin"] != null)
                {
                    gvSearchResultsUserAdmin.DataSource = Session["gvSearchResultsUserAdmin"];
                }
                else
                {
                    //gvSearchResultsUserAdmin.DataSource = ds;
    
                }
    
                foreach (GridViewRow row in gvSearchResultsUserAdmin.Rows)
                {
                    var chkBox = row.FindControl("chkShot") as System.Web.UI.WebControls.CheckBox;
    
                    IDataItemContainer container = (IDataItemContainer)chkBox.NamingContainer;
    
                    if (chkBox.Checked)
                    {
                        PersistRowIndex(container.DataItemIndex);
                    }
                    else
                    {
                        RemoveRowIndex(container.DataItemIndex);
                    }
                }
                LoadGridView();
                //gvSearchResultsUserAdmin.DataBind();
                RePopulateCheckBoxes(); 
            }
    

    选中所有复选框:

     protected void chkboxSelectAll_CheckedChanged(object sender, EventArgs e)
            {
                try
                {
                    System.Web.UI.WebControls.CheckBox ChkBoxHeader = (System.Web.UI.WebControls.CheckBox)gvSearchResultsUserAdmin.HeaderRow.FindControl("chkboxSelectAll");
                    foreach (GridViewRow row in gvSearchResultsUserAdmin.Rows)
                    {
                        System.Web.UI.WebControls.CheckBox ChkBoxRows = (System.Web.UI.WebControls.CheckBox)row.FindControl("chkShot");
                        if (ChkBoxHeader.Checked == true)
                        {
                            ChkBoxRows.Checked = true;
                        }
                        else
                        {
                            ChkBoxRows.Checked = false;
                        }
                    }
                }
                catch (Exception)
                {
    
                    }
                }
            }
    

    我的“全部下载”按钮:

        protected void btnDownloadShots_Click(object sender, EventArgs e)
        {
            using (ZipFile zip = new ZipFile())
            {
                try
                {
                    foreach (GridViewRow gvrow in gvSearchResultsUserAdmin.Rows)
                    {
                        System.Web.UI.WebControls.CheckBox chk = (System.Web.UI.WebControls.CheckBox)gvrow.FindControl("chkShot");
                        if (chk.Checked)
                        {
                            SqlCommand objcmd = new SqlCommand();
                            try
                            {
                                filmName = gvrow.Cells[1].Text;
                                shotNumber = int.Parse(gvrow.Cells[2].Text);
                            }
    
                            catch (Exception ex)
                            {
    
                            }
    
                            objcmd.CommandType = CommandType.StoredProcedure;
                            objcmd.CommandText = "ShotIDfromSearch";
    
                            objcmd.Parameters.AddWithValue("@filmName", filmName);
                            objcmd.Parameters.AddWithValue("@shotNumber", shotNumber);
                            ds = objdb.getDataSetUsingCmdObj(objcmd);
    
                            shotID = ds.Tables[0].Rows[0].Field<int>("ShotID");
    
                            FilePath = ReturnFilePath(shotID);
                            //Response.ForceDownload(FilePath, filmName + " - Shot " + shotNumber.ToString() + ".mp4");
    
                            zip.AddFile(FilePath, filmName);
                        }
                    }
                }
                catch (Exception)
                {
    
                }
    
    
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=Vertov.zip");
                Response.ContentType = "application/zip";
                zip.Save(Response.OutputStream);
                Response.End();
            }
        }
    

1 个答案:

答案 0 :(得分:0)

最好的方法是首先将详细信息存储在缓存或其他替代存储(已检查数据)中,然后从缓存中提取详细信息以下载文件。