![](/img/trans.png)
[英]Find all Check checkboxes in a asp.net GridView using Jquery
[英]ASP.NET - How can I check all checkboxes across gridview pages at once and access checked rows in a gridview across pages?
我有一个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();
}
}
最好的方法是先将详细信息存储在缓存或其他备用存储(已检查的数据)中,然后从那里拉出详细信息,也许可以下载文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.