选择Gridview中的所有复选框

时间:2016-02-23 13:36:22

标签: javascript c# jquery asp.net gridview

我正在尝试选择我在页面上运行的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 Rendered Page

这是执行时的控制台

Console Page

2 个答案:

答案 0 :(得分:1)

我创建了新项目并使用了您的代码。你的JS不工作的原因是你在服务器控件的Id上转发,例如:

table[id*=tblChck]

他们将在客户端浏览器中具有不同的ID。它是ASP.NET如何防止Id重复的。如果您有母版页,则内容页面中的所有服务器控件都会添加一个前缀,例如&#34; MainContent _&#34;。为了使你的JS工作,你需要将JQ选择器更改为类似<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> 的东西,或者你可以添加一个css类并由它选择。

要完全清楚以使代码工作,我必须将JS更改为:

File tempFile = null; 
try{            
    tempFile = File.createTempFile("sign_", "tmp.pdf");

}catch(IOException e){          
    e.printStackTrace();            
} finally {
    if (tempFile != null) {
        tempFile.deleteOnExit();
        tempFile = null;
        //Added a call to suggest the Garbage Collector
        //To collect the reference and remove
        System.gc(); 
    }
}

答案 1 :(得分:0)

&#13;
&#13;
shared_from_this
&#13;
&#13;
&#13;

在.chkHeader主复选框中添加课程 喜欢: - &gt;

$(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; 
			            });         
			        }
			    });