【发布时间】: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(); }
}
}
}
这是执行时的控制台
【问题讨论】:
-
GridView 复选框使用 ajax 渲染?
-
请显示你的结果html!
-
问题出在你的html中,在渲染你的表格时,你可能在绘制表格时出现了混乱,显示你的结果html代码来帮助。
标签: javascript c# jquery asp.net gridview