How to get values from the first column which is a checkbox of a Gridview?

Asked

Viewed 1,330 times

1

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

My first column is a checkbox, I need to check that they are marked and if yes get their values.

  • is ASP.NET Web Forms?

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

inserir a descrição da imagem aqui

  • is my kkkkk @Marconi, good luck

  • Sorry for the delay, before I knew how to use the OS. !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.