我有一个gridview,允许您下载所检查行的文件。它将文件下载到.zip文件中。一切都很好,除了两件事:
这是我的代码:
复选框和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();
}
}
答案 0 :(得分:0)
最好的方法是首先将详细信息存储在缓存或其他替代存储(已检查数据)中,然后从缓存中提取详细信息以下载文件。