How to get values from the first column that is a checkbox of a GridView?

1

I have a GridView and I need to get the values from the first column of the Grid.

My first column is a checkbox, I need to make sure they are checked and get their values.

    
asked by anonymous 18.08.2014 / 16:47

1 answer

3
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplicationForms.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>    
    <script src="Scripts/jquery-1.10.2.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>                        
                        <input type="checkbox" value='<%#Eval("Codigo") %>' runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Nome" HeaderText="Nome Completo" />
            </Columns>
        </asp:GridView>
        <button type="button" id="BtnPegar">Pegar os Escolhidos</button>
        <div id="divesc"></div>
    </div>
    </form>
    <script>
        $(document).ready(function () {
            $("#BtnPegar").click(function () {
                console.log('u');
                var elementos = $("#<%=GridView1.ClientID%> input[type=checkbox]:checked");                
                if (elementos.length > 0) {
                    var msg = '';
                    $.each(elementos, function (index, obj) {
                        msg = msg + obj.value + '<br>';
                    });
                    $("#divesc").html(msg);
                }
            });
        })
    </script>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
    Sub_Carregar();
}

private void Sub_Carregar()
{
    GridView1.DataSource = new object[] 
    { 
        new {Codigo = 1,Nome = "Ademar"},
        new {Codigo = 2,Nome = "Rosana"},
        new {Codigo = 3,Nome = "Furukama"},
    }.ToArray();
    GridView1.DataBind();
}

    
18.08.2014 / 18:49