选中 Gridview 中的所有复选框
Posted
技术标签:
【中文标题】选中 Gridview 中的所有复选框【英文标题】:Select all Checkboxes in a Gridview 【发布时间】:2016-06-05 09:03:40 【问题描述】:我正在尝试选择我在页面上运行的 gridview 中的所有复选框,我正在使用 JQuery 和 javascript 作为函数,但 javascript 似乎不起作用。 我在普通表上尝试了相同的脚本并且它有效,但它似乎不适用于我添加到我的 gridview 的复选框。 你们能帮我解决这个问题吗? 这是页面的代码。
<%@ Page Title="" Language="C#" MasterPageFile="~/SMSWebMaster.Master" AutoEventWireup="true" CodeBehind="Search.aspx.cs" EnableEventValidation ="false" Inherits="SMSWebApp.Views.Search" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function ()
$("#tblChck [id*=chkHeader]").click(function ()
if ($(this).is( ":checked"))
$("#gvData [id*=cbSelect2]").attr("checked", "checked");
else
$("#gvData [id*=cbSelect2]").removeAttr("checked");
);
$("#gvData [id*=cbSelect2]").click(function ()
if ($("#gvData [id*=cbSelect2]").length == $("#gvData [id*=cbSelect2]:checked").length)
$("#tblChck [id*=chkHeader]").attr("checked", "checked");
else
$("#tblChck [id*=chkHeader]").removeAttr("checked");
);
);
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager1" EnableViewState="true" EnablePartialRendering="true" runat="server">
</asp:ScriptManager>
<div>
<div id="contectcontainer">
<div class="content">
<br />
<br />
<center>
<table>
<tr>
<td>
<asp:Label ID="lblapo" Text=" Apolice" CssClass="label_style3" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="TBoxApo" runat="server"></asp:TextBox>
</td>
<td>
<asp:Label ID="Label1" Text=" Recibo" CssClass="label_style3" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="TBoxRec" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<br />
<br />
<table>
<tr>
<td>
<asp:Label ID="Label3" Text=" Mediador" CssClass="label_style3" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="TBoxMed" runat="server"></asp:TextBox>
</td>
<td>
<asp:Label ID="Label4" Text=" Tomador" CssClass="label_style3" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="TBoxTom" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<br />
<br />
<table>
<tr>
<td>
<asp:Label ID="Label5" Text=" Veiculo" CssClass="label_style3" runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="TBoxVeic" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<table id="tblChck" runat="server">
<tr>
<td>
<asp:CheckBox id="chkHeader" Text="Selecione Todos" runat="server" />
</td>
</tr>
</table>
<br />
<br />
<br />
<asp:GridView id="gvData" CssClass="table-design" runat="server" SkinID="Fun" AutoGenerateColumns="false" >
<EmptyDataTemplate>
<div style="padding:10px;margin-left:10px">
Sem Dados !!!
</div>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:checkbox id="cbSelect2" CssClass="gridCB" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Apolice" DataField="Apolice" />
<asp:BoundField HeaderText="Recibo" DataField="Recibo" />
<asp:BoundField HeaderText="Agente_Mediador" DataField="Agente_Mediador" />
<asp:BoundField HeaderText="Cliente_Tomador" DataField="Cliente_Tomador" />
<asp:BoundField HeaderText="Veiculo" DataField="Veiculo" />
<asp:BoundField HeaderText="Data" DataField="Data" />
<asp:BoundField HeaderText="ID" DataField="Id" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="TruBtn_Click" Text="Pesquisar" />
</div>
</div>
</div>
</form>
</asp:Content>
还有后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mysql.Data.MySqlClient;
using System.Data;
using System.Configuration;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Net;
using System.Collections;
using System.Data.SqlClient;
using SMSWebApp.SupprotClasses;
using System.IO;
using System.IO.Compression;
namespace SMSWebApp.Views
public partial class Search : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)
SqlConnection msqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
public bool OpenConnection()
// open the connection
if (msqlConnection.State != System.Data.ConnectionState.Open)
msqlConnection.Open();
return true;
public void CloseConnection()
// close the connection
if (msqlConnection.State != System.Data.ConnectionState.Closed)
msqlConnection.Close();
public void TruBtn_Click(object sender, EventArgs e)
if (TBoxApo.Text == "" && TBoxMed.Text == "" && TBoxRec.Text == "" && TBoxTom.Text == "" && TBoxVeic.Text == "")
string script = "alert(\"Enter any value!\");";
ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
TBoxApo.Focus();
else
Btn_Click4( );
protected void Btn_Click3(object sender, EventArgs e)
SaveFilePath path = new SaveFilePath();
List<string> batata = new List<string>();
for (int i = 0; i < gvData.Rows.Count; i++)
CheckBox chkb = (CheckBox)gvData.Rows[i].Cells[0].FindControl("cbSelect2");
if (chkb.Checked)
string seven = gvData.Rows[i].Cells[7].Text;
int Id = Convert.ToInt32(seven);
string fullpath = path.GetFullPath4(Id);
Response.Write("<br>" + fullpath);
batata.Add(fullpath);
var archive = Server.MapPath("~/archive.zip");
var temp = Server.MapPath("~/temp");
if (System.IO.File.Exists(archive))
System.IO.File.Delete(archive);
// Directory.EnumerateFiles(temp).ToList().ForEach(f => System.IO.File.Delete(f));
Directory.GetFileSystemEntries(temp).ToList().ForEach(f => System.IO.File.Delete(f));
batata.ForEach(f => System.IO.File.Copy(f, Path.Combine(temp, Path.GetFileName(f))));
ZipFile.CreateFromDirectory(temp, archive);
Response.ContentType = "application/zip";
Response.AppendHeader("Content-Disposition", "attachment; filename= archive.zip");
Response.TransmitFile(archive);
protected void DownloadFile(object sender, EventArgs e)
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
public void Btn_Click4()
try
SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["Constring"].ConnectionString);
OpenConnection();
SqlCommand sqlcommand = new SqlCommand();
sqlcommand.Connection = sql;
string apolice = TBoxApo.Text;
string recibo = TBoxRec.Text;
string mediador = TBoxMed.Text;
string tomador = TBoxTom.Text;
string veiculo = TBoxVeic.Text;
// Pesquisas com a Apolice
if (TBoxApo.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%'";
if (TBoxApo.Text != "" && TBoxRec.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%' and Recibo like '%" + recibo + "%'";
else if (TBoxApo.Text != "" && TBoxMed.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%' and Agente_Mediador like'%" + mediador + "%' ";
else if (TBoxApo.Text != "" && TBoxTom.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%' and Cliente_Tomador like'%" + tomador + "%' ";
else if (TBoxApo.Text != "" && TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%' and Veiculo like'%" + veiculo + "%' ";
else if (TBoxApo.Text != "" && TBoxRec.Text != "" && TBoxMed.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%' and Recibo like '%" + recibo + "%' and Agente_Mediador like'%" + mediador + "%' ";
else if (TBoxApo.Text != "" && TBoxRec.Text != "" && TBoxTom.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%' and Recibo like '%" + recibo + "%' and Cliente_Tomador like'%" + tomador + "%' ";
else if (TBoxApo.Text != "" && TBoxTom.Text != "" && TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%' and Cliente_Tomador like '%" + tomador + "%' and Veiculo like'%" + veiculo + "%' ";
else if (TBoxApo.Text != "" && TBoxRec.Text != "" && TBoxMed.Text != "" && TBoxTom.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%' and Recibo like '%" + recibo + "%' and Agente_Mediador like'%" + mediador + "%'and Cliente_Tomador like'%" + tomador + "%' ";
else if (TBoxApo.Text != "" && TBoxRec.Text != "" && TBoxMed.Text != "" && TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%' and Recibo like '%" + recibo + "%' and Agente_Mediador like'%" + mediador + "%'and Veiculo like'%" + veiculo + "%' ";
else if (TBoxApo.Text != "" && TBoxRec.Text != "" && TBoxTom.Text != "" && TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%' and Recibo like '%" + recibo + "%' and Cliente_Tomador like'%" + tomador + "%'and Veiculo like'%" + veiculo + "%' ";
else if (TBoxApo.Text != "" && TBoxTom.Text != "" && TBoxMed.Text != "" && TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%' and Cliente_Tomador like '%" + tomador + "%' and Agente_Mediador like'%" + mediador + "%'and Veiculo like'%" + veiculo + "%' ";
else if (TBoxApo.Text != "" && TBoxRec.Text != "" && TBoxMed.Text != "" && TBoxTom.Text != "" && TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Apolice like '%" + apolice + "%' and Recibo like '%" + recibo + "%' and Agente_Mediador like'%" + mediador + "%' and Cliente_Tomador like'%" + tomador + "%' Veiculo like'%" + veiculo + "%' ";
// Pesquisas com o Recibo
else
if (TBoxRec.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Recibo like '%" + recibo + "%'";
if (TBoxRec.Text != "" && TBoxTom.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Recibo like '%" + recibo + "%' and Cliente_Tomador like'%" + tomador + "%' ";
else if (TBoxRec.Text != "" && TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Recibo like '%" + recibo + "%' and Veiculo like'%" + veiculo + "%' ";
else if (TBoxRec.Text != "" && TBoxMed.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Recibo like '%" + recibo + "%' and Agente_Mediador like'%" + mediador + "%' ";
else if (TBoxRec.Text != "" && TBoxMed.Text != "" && TBoxTom.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Recibo like '%" + recibo + "%' and Agente_Mediador like'%" + mediador + "%'and Cliente_Tomador like'%" + tomador + "%' ";
else if (TBoxRec.Text != "" && TBoxMed.Text != "" && TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Recibo like '%" + recibo + "%' and Agente_Mediador like'%" + mediador + "%'and Veiculo like'%" + veiculo + "%' ";
else if (TBoxRec.Text != "" && TBoxVeic.Text != "" && TBoxTom.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Recibo like '%" + recibo + "%' and Veiculo like'%" + veiculo + "%'and Cliente_Tomador like'%" + tomador + "%' ";
else if (TBoxRec.Text != "" && TBoxMed.Text != "" && TBoxTom.Text != "" && TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Recibo like '%" + recibo + "%' and Agente_Mediador like'%" + mediador + "%'and Cliente_Tomador like'%" + tomador + "%'and Veiculo like'%" + veiculo + "%' ";
// Pesquisas com o Mediador
else if (TBoxMed.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Agente_Mediador like '%" + mediador + "%'";
if (TBoxMed.Text != "" && TBoxTom.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Agente_Mediador like'%" + mediador + "%'and Cliente_Tomador like'%" + tomador + "%' ";
else if (TBoxMed.Text != "" && TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Agente_Mediador like '%" + mediador + "%' and Veiculo like'%" + veiculo + "%' ";
else if (TBoxMed.Text != "" && TBoxTom.Text != "" && TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Agente_Mediador like'%" + mediador + "%'and Cliente_Tomador like'%" + tomador + "%'and Veiculo like'%" + veiculo + "%' ";
// Pesquisas com o Tomador
else if (TBoxTom.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Cliente_Tomador like '%" + tomador + "%'";
if (TBoxTom.Text != "" && TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Cliente_Tomador like'%" + tomador + "%'and Veiculo like'%" + veiculo + "%' ";
// Pesquisas com o Veiculo
else if (TBoxVeic.Text != "")
sqlcommand.CommandText = "select Apolice,Recibo,Agente_Mediador,Cliente_Tomador,Veiculo,Data,Id from UniDownTeste.dbo.DownUni where Veiculo like '%" + veiculo + "%'";
SqlDataAdapter adapter = new SqlDataAdapter(sqlcommand);
DataSet ds = new DataSet();
adapter.Fill(ds);
gvData.DataSource = ds;
gvData.DataBind();
finally CloseConnection();
这是呈现的 html
这是执行时的控制台
【问题讨论】:
GridView 复选框使用 ajax 渲染? 请显示你的结果html! 问题出在你的html中,在渲染你的表格时,你可能在绘制表格时出现了混乱,显示你的结果html代码来帮助。 【参考方案1】:我创建了新项目并使用了您的代码。您的 JS 不工作的原因是因为您中继了服务器控件的 Id,例如:
#tblChck, #gvData
它们在客户端浏览器中会有不同的 ID。这就是 ASP.NET 防止 Id 重复的工作方式。如果您有一个母版页,则内容页中的所有 serwer 控件都将在 ID 中添加一个前缀,例如“MainContent_”。为了让您的 JS 工作,您需要将 JQ 选择器更改为 table[id*=tblChck]
之类的东西,或者您可以添加一个 css 类并通过它进行选择。
为了让您的代码能够正常工作,我必须将 JS 更改为:
<script type="text/javascript">
$(function ()
$("table[id*=tblChck] [id*=chkHeader]").click(function ()
if ($(this).is(":checked"))
$("table[id*=gvData] [id*=cbSelect2]").attr("checked", "checked");
else
$("table[id*=gvData] [id*=cbSelect2]").removeAttr("checked");
);
$("table[id*=gvData] [id*=cbSelect2]").click(function ()
if ($("table[id*=gvData] [id*=cbSelect2]").length == $("table[id*=gvData] [id*=cbSelect2]:checked").length)
$("table[id*=tblChck] [id*=chkHeader]").attr("checked", "checked");
else
$("table[id*=tblChck] [id*=chkHeader]").removeAttr("checked");
);
);
</script>
【讨论】:
我在master中引用了JQuery 但它似乎并没有干扰我在此页面中引用的脚本。我在普通静态表上尝试了相同的脚本引用相同的函数,它有效,它共享相同母版页。我已经在 Internet Explorer 上发布了渲染的 HTML 和控制台。感谢您的帮助 通过呈现的 html 我的意思是 html 源视图而不是浏览器屏幕;) 对不起,你能不能指定,因为这个特定页面的所有源代码都在问题中,我是否也应该发布母版页的代码。 @Rob 谢谢你的指导,我已经相应地更新了我的答案。【参考方案2】:$(document).ready(function()
$('.chkHeader').click(function(event)
if(this.checked) // check select status
$('.gridCB').each(function()
$('.chkHeader').each(function()
this.checked = true;
);
this.checked = true;
);
else
$('.gridCB').each(function()
$('.chkHeader').each(function()
this.checked = false;
);
this.checked = false;
);
);
在 .chkHeader 主复选框中添加类 喜欢:->
<asp:CheckBox id="chkHeader" class="chkHeader" Text="Selecione Todos" runat="server" />
【讨论】:
以上是关于选中 Gridview 中的所有复选框的主要内容,如果未能解决你的问题,请参考以下文章
使用 Jquery 在 asp.net GridView 中查找所有选中复选框
使用 JQuery 在 GridView ASP.NET 中选择所有复选框
C# 怎样判断DEV GridView 中选中的哪一行是分组的标题行
如果选中 CheckAll 则全选,如果未选中则使用 jQuery 从 Gridview 中取消全选