ASP.NET-如何一次选中网格视图页面中的所有复选框,并跨页面访问网格视图中的选中行
本文关键字:视图 网格 访问 复选框 ASP 何一次 NET- 中网 | 更新日期: 2023-09-27 18:30:13
我有一个网格视图,它允许您下载所检查行的文件。它将这些文件下载到一个.zip文件中。除了两件事之外,一切都很好:
- 如果我选择"全选",它只会在网格视图的第一页中全选。如果有足够的数据可以有多个页面,我该如何使其成为选中全部实际上选择全部
- 如果我在不同的页面上选中了复选框,然后单击了一个应该下载所有文件的按钮,它只下载我当前查看的页面的文件。因此,如果我正在查看gridview第2页,并且我下载了选中的行,它将只下载我在第2页中选中的文件——即使我在第1页中选中了框
这是我的代码:
复选框和网格视图:
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();
}
}
最好的方法是先将详细信息存储在缓存或其他替代存储中(检查数据),然后从缓存中提取详细信息,也许可以下载文件。