Usuário com melhor resposta
Não consigo selecionar os campos(checkbox) no gridview

Pergunta
-
Galera tenho um gridview e coloquei um checkbox para o usuario selecionar o campo, mas quando tem uma média de 100 registros e ele marca uma opção para selecionar todos os documentos fica mostrando uma mensagem de aguarde que é um updatepanel, a mensagem some e os campos não ficam selecionados, e isso é só no cliente aqui na minha maquina(local) funciona normal, qual a sugestão para solucionar esse problema.
código do checkbox selecionar todos:
protected void ckbMarcarTodos_CheckedChanged(object sender, EventArgs e) { for (int i = 0; i <= grvReceber.Rows.Count - 1; i++) { GridViewRow dgItem = grvReceber.Rows[i]; CheckBox cb = (CheckBox)dgItem.FindControl("ckbBaixar"); if (!cb.Checked) cb.Checked = true; else cb.Checked = false; } }
gridview:
<asp:UpdateProgress ID="UpdateProgress1" runat="server"> <ProgressTemplate> <div style="color:#333; font-size:14px; font-weight:bold;"> <img alt="" src="imagens/ajax-loader.gif" /> Aguarde... </div> </ProgressTemplate> </asp:UpdateProgress>
Respostas
-
Caso você não esteja usando paginação no seu GridView, eu utilizaria de javascript + jquery para fazer isso no cliente, evitando de ir no servidor:
.cs
using System; using System.Data; namespace WebApplication1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("ValorNumerico", typeof(string)); for (int i = 0; i < 100; i++) { dt.Rows.Add("Valor" + i); } grvValores.DataSource = dt; grvValores.DataBind(); } } }
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> <script type="text/javascript" language="javascript"> function AtivarSelecao(gridview, checboxPai, checboxFilho) { $('[id*=' + gridview + '] input[id*=' + checboxFilho + ']').each(function () { $(this).attr('checked', checboxPai.checked); }); } function LimparSelecao(gridview, checboxPai, checboxFilho) { if ($('[id*=' + gridview + '] input[id*=' + checboxFilho + ']').length == $('[id*=' + gridview + '] input[id*=' + checboxFilho + ']:checked').length) { $('[id*=' + gridview + '] input[id*=' + checboxPai + ']').first().attr('checked', true); } else { $('[id*=' + gridview + '] input[id*=' + checboxPai + ']').first().attr('checked', false); } } </script> </head> <body> <form id="form1" runat="server"> <asp:GridView runat="server" ID="grvValores" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox ID="chkSelecionarTodos" runat="server" ToolTip="Selecionar Todos!" onclick="AtivarSelecao('grvValores',this,'chkSelecionar');" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkSelecionar" onclick="LimparSelecao('grvValores','chkSelecionarTodos','chkSelecionar');" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Valores" DataField="ValorNumerico" /> </Columns> </asp:GridView> </form> </body> </html>
Vitor Mendes | Seu feedback é muito importante para todos!
Visite o meu site: http://www.vitormendes.com.br/- Marcado como Resposta Thiago_Porto terça-feira, 8 de maio de 2012 16:01
Todas as Respostas
-
o que pode estar ocorrendo e que no postBack possa estar desmarcando tudo .
http://www.codeproject.com/Articles/11207/Selecting-multiple-checkboxes-inside-a-GridView-co
Não esqueça de usar o componente </> na barra para posta seu código. Microsoft MCPD,MCTS,MCC
-
Caso você não esteja usando paginação no seu GridView, eu utilizaria de javascript + jquery para fazer isso no cliente, evitando de ir no servidor:
.cs
using System; using System.Data; namespace WebApplication1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("ValorNumerico", typeof(string)); for (int i = 0; i < 100; i++) { dt.Rows.Add("Valor" + i); } grvValores.DataSource = dt; grvValores.DataBind(); } } }
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> <script type="text/javascript" language="javascript"> function AtivarSelecao(gridview, checboxPai, checboxFilho) { $('[id*=' + gridview + '] input[id*=' + checboxFilho + ']').each(function () { $(this).attr('checked', checboxPai.checked); }); } function LimparSelecao(gridview, checboxPai, checboxFilho) { if ($('[id*=' + gridview + '] input[id*=' + checboxFilho + ']').length == $('[id*=' + gridview + '] input[id*=' + checboxFilho + ']:checked').length) { $('[id*=' + gridview + '] input[id*=' + checboxPai + ']').first().attr('checked', true); } else { $('[id*=' + gridview + '] input[id*=' + checboxPai + ']').first().attr('checked', false); } } </script> </head> <body> <form id="form1" runat="server"> <asp:GridView runat="server" ID="grvValores" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox ID="chkSelecionarTodos" runat="server" ToolTip="Selecionar Todos!" onclick="AtivarSelecao('grvValores',this,'chkSelecionar');" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkSelecionar" onclick="LimparSelecao('grvValores','chkSelecionarTodos','chkSelecionar');" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Valores" DataField="ValorNumerico" /> </Columns> </asp:GridView> </form> </body> </html>
Vitor Mendes | Seu feedback é muito importante para todos!
Visite o meu site: http://www.vitormendes.com.br/- Marcado como Resposta Thiago_Porto terça-feira, 8 de maio de 2012 16:01