locked
Using JQuery Each function to gather all checkboxes from Client Side, how? RRS feed

Answers

  • User-1768369891 posted

    function GetAllCheckedCheckboxes() { var ids = ''; $("#btnSend").click(function () {

    You used wrong way jquery/ javascript.... 

    1. With in button click function you defined another click event in jquery.

    $('#gvUnsent

    2.  Gridview id gvUnsent in javascript not accessible due to change in master page with content place holder id so you can set clientID mode static in gridview or try to get id through client id..

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script
      src="https://code.jquery.com/jquery-3.1.1.min.js"
      integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
      crossorigin="anonymous"></script>
        <script type="text/javascript">
            function GetAllCheckedCheckboxes() {
                var ids = '';                       
                    var text = []; //difine a string array to store the selected ID.
                //loop through the GridView and get all checkbox
    
                    $('#gvUnsent input[type=checkbox]').each(function () {
                        
                        if ($(this).is(":checked")) {//If the checkbox is checked, get the ID.
                            var id = $(this).parent().attr("data-MatrixID"); //get the custom attribute.
                            alert(id);
                            text.push(id); //insert the selected value into the array.
                        }
                    });
                    $("#hdCheckedIDs").val(text.join());
                
                return true;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div runat="server" style="text-align: center;">
                <h1>Unsent Requests</h1>
            </div>
            <div>
                <p style="text-align: center">
                    <asp:Label ID="lblNoUnsent" runat="server" Text="No unsent requests are available." Font-Names="Arial" Font-Size="Large"></asp:Label>
                    <asp:GridView ID="gvUnsent" ClientIDMode="Static" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" AutoGenerateColumns="False" DataKeyNames="RecID">
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:TemplateField HeaderText="Send" ControlStyle-Width="50px" ItemStyle-HorizontalAlign="Right">
                                <EditItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="cbSendTo" runat="server" TextAlign="Left" data-MatrixID='<%# Eval("RecID") %>' />
                                    <asp:Label ID="lblSendTo" runat="server" Text='<%# Bind("RecID") %>' Visible="False"></asp:Label>
                                </ItemTemplate>
                                <ControlStyle Width="50px" />
                                <ItemStyle HorizontalAlign="Left" Width="75px" />
                            </asp:TemplateField>
                            <asp:BoundField DataField="AppName" HeaderText="Application" ControlStyle-Width="150px">
                                <ControlStyle Width="150px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="AuditorName" HeaderText="Auditor" ControlStyle-Width="250px">
                                <ControlStyle Width="250px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="DateSubmitted" DataFormatString="{0:MM/dd/yy }" HeaderText="Submitted" ControlStyle-Width="150px">
                                <ControlStyle Width="150px" />
                                <ItemStyle HorizontalAlign="Right" />
                            </asp:BoundField>
                        </Columns>
                        <EditRowStyle BackColor="#2461BF" />
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#EFF3FB" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F5F7FB" />
                        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                        <SortedDescendingCellStyle BackColor="#E9EBEF" />
                        <SortedDescendingHeaderStyle BackColor="#4870BE" />
                    </asp:GridView>
                    <asp:HiddenField ID="hdCheckedIDs" runat="server" ClientIDMode="static" />
                    <asp:Button ID="btnSendChecked" runat="server" Text="Send Checked Items" Width="201px" OnClick="btnSendChecked_Click"
                        OnClientClick="return GetAllCheckedCheckboxes();" />
                </p>
            </div>
        </form>
    </body>
    </html>

    and cs file is 

     public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    DataSoruce _dvsource = new DataSoruce();
                    gvUnsent.DataSource = _dvsource._source.ToList();
                    gvUnsent.DataBind();
                }
            }
            protected void btnSendChecked_Click(object sender, EventArgs e)
            {
                // Checked IDs are coming back as unassigned/null - 12/9/16
                string SelectedIDs = hdCheckedIDs.Value.ToString();
                Response.Write(SelectedIDs.ToString());
            }
        }
        public class DataSoruce
        {
           public List<Items> _source = new List<Items>() { };
            public DataSoruce()
            {
                for (int i = 1; i <= 100; i++)
                {
                    Items _item = new Items();
                    _item.RecID = i.ToString();
                    _item.AppName = "Test App";
                    _item.AuditorName = "Navneet Mitawa";
                    _item.DateSubmitted = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
    
                    _source.Add(_item);
                }
            }
        }
        public class Items
        {
            public string RecID { get; set; }
            public string AppName { get; set; }
            public string AuditorName { get; set; }
            public string DateSubmitted { get; set; }
            //public string RecID { get; set; }
        }

    Try this working fine my side..


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 14, 2016 5:23 AM

All replies

  • User-1672470423 posted

    Please try this:

    At page level, add HiddenFiled for setting all checked checkboxes:

    <asp:HiddenField ID="hdCheckedIDs" runat="server" ClientIDMode="static" />

    At Grid level, add HiddenField next to checbox like below:

    <ItemTemplate> <asp:CheckBox ID="CheckSelect" runat="server" /> <asp:HiddenField ID="hdID" runat="server" Value='<%# Eval("ID") %>' /> </ItemTemplate>

    In Javascript, on button click call below function "GetAllCgheckedCheckboxes()"

    <script type="text/javascript">
     
    function GetAllCgheckedCheckboxes() {

    var ids = '';

    $('#hdCheckedIDs').val('');

        $('#<%=gvYourGrid.ClientID %>').find("input:checkbox").each(function() {      //replace gvYourGrid with your grid name
            if ($(this).is(':checked')) {
                var id = $(this).next('input:hidden[id$=hdID]').val();

                ids+=id + ',';
            }
        });

    $('#hdCheckedIDs').val(ids);
    }
    </script>

    On Server access hidden field value, trim comma (,) from last. You can then split to get ids.

    This is not tested. So, need to check syntax.

    Friday, December 9, 2016 4:36 PM
  • User-718146471 posted

    Ok Sumit, I will try this and if it works, I'll mark your answer. If not, I'll post a follow up. Thanks!

    Friday, December 9, 2016 6:03 PM
  • User-271186128 posted

    Hi bbcompent1,

    As for this issue, you could also use custom attribute to store the primary key, please refer to the following code:

         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
         <script>
             $(function () {
    //button client click event: $("#btnSend").click(function () { var text = []; //difine a string array to store the selected ID. //loop through the GridView and get all checkbox $('#GridView2 input[type=checkbox]').each(function () { if ($(this).is(":checked")) {//If the checkbox is checked, get the ID. var id = $(this).parent().attr("data-EmployeeID"); //get the custom attribute. text.push(id); //insert the selected value into the array. } }); $("#HiddenField1").val(text.join()); }) }) </script> <div> <asp:GridView ID="GridView2" runat="server"> <Columns> <asp:TemplateField HeaderText ="IsSelected"> <ItemTemplate> <asp:CheckBox ID="chb_EmployeeID" runat="server" data-EmployeeID ='<%# Eval("EmployeeID") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:HiddenField ID="HiddenField1" runat="server" /> <asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" /> </div>

    Code behind:

            protected void btnSend_Click(object sender, EventArgs e)
            {
                Response.Write(HiddenField1.Value);
            }

    Best regards,
    Dillion

    Monday, December 12, 2016 5:48 AM
  • User-718146471 posted

    Ok, I tried altering my code as shown but the values are undefined. Here is the code I am using:

    code behind:

    protected void btnSendChecked_Click(object sender, EventArgs e)
    {
        // Checked IDs are coming back as unassigned/null - 12/9/16
        string SelectedIDs = hdCheckedIDs.Value.ToString();
        Response.Write(SelectedIDs.ToString());
    

    And the UI code:

    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
            <title>Unsent Requests</title>
    <script src="../framework/js/jquery.min.js" type="text/javascript"></script> <!-- latest 3.1.1 library for JQuery -->
             <script type="text/javascript">
             $(function () {
                 //button client click event:
                 $("#btnSend").click(function () {
                     var text = []; //difine a string array to store the selected ID.
    
                     //loop through the GridView and get all checkbox
                     $('#gvUnsent input[type=checkbox]').each(function () {
                         if ($(this).is(":checked")) {//If the checkbox is checked, get the ID.
                             var id = $(this).parent().attr("data-MatrixID"); //get the custom attribute.
                             text.push(id); //insert the selected value into the array.
                         }
                     });
    
                     $("#hdCheckedIDs").val(text.join()); 
                 })
             })
          
         </script>
    ...
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"><div runat="server" style="text-align: center;">
        <h1>Unsent Requests</h1>
        </div>
        <p style="text-align: center">
            <asp:Label ID="lblNoUnsent" runat="server" Text="No unsent requests are available." Font-Names="Arial" Font-Size="Large"></asp:Label>
            <asp:GridView ID="gvUnsent" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" AutoGenerateColumns="False" DataKeyNames="RecID">
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:TemplateField HeaderText="Send" ControlStyle-Width="50px" ItemStyle-HorizontalAlign="Right">
                        <EditItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="cbSendTo" runat="server" AutoPostBack="True" TextAlign="Left" data-MatrixID ='<%# Eval("RecID") %>' />
                            <asp:Label ID="lblSendTo" runat="server" Text='<%# Bind("RecID") %>' Visible="False"></asp:Label>
                        </ItemTemplate>
    ...
                </asp:GridView>
                <asp:HiddenField ID="hdCheckedIDs" runat="server" ClientIDMode="static" />
        <asp:Button ID="btnSendChecked" runat="server" Text="Send Checked Items" Width="201px" OnClick="btnSendChecked_Click" />
    

    I'm using ... as place holder to exclude what has nothing to do with what I am trying to do. Thanks!

    Monday, December 12, 2016 1:43 PM
  • User475983607 posted

    IMHO, you should apply a style to the checkboxes and use the style in the JQuery selector .  Also add the "data-" attribute to the checkbox.  This will greatly simplify the $.each() logic.

    Keep in  mind, that the data-MatrixID is in the item template and the checkbox is in the edit temple so it makes senses that the ID is not found.

    Monday, December 12, 2016 2:04 PM
  • User-718146471 posted

    apply a style to the checkboxes and use the style in the JQuery selector .  Also add the "data-" attribute to the checkbox.  This will greatly simplify the $.each() logic.

    Hi, do you have any examples that show how I would do that? There are times I feel brain dead when it comes to JQuery.

    Monday, December 12, 2016 2:15 PM
  • User-1672470423 posted

    Can you check below:

    1. check if you are getting id by adding alert

    //loop through the GridView and get all checkbox
                     $('#gvUnsent input[type=checkbox]').each(function () {
                         if ($(this).is(":checked")) {//If the checkbox is checked, get the ID.
                             var id = $(this).parent().attr("data-MatrixID"); //get the custom attribute.

    alert(id);
                             text.push(id); //insert the selected value into the array.
                         }
                     });

                     $("#hdCheckedIDs").val(text.join());

    alert($("#hdCheckedIDs").val());

    2. If not check by setting checkbox id to RecID directly

    <asp:CheckBox ID="<%# Eval("RecID") %>" runat="server" AutoPostBack="True" TextAlign="Left" />

    and access id using

    var id = $(this).attr('id');

    Monday, December 12, 2016 6:05 PM
  • User-271186128 posted

    Hi bbcompent1,

    First, I suggest you could use F12 developer tools to check the html elements, the result should be like this:

    Then, as Sumit said, you could use alert or set a debugger to check whether you could get the custom attribute value.

    Best regards,
    Dillion

    Tuesday, December 13, 2016 7:33 AM
  • User-718146471 posted

    Ok, I modified the JQuery code as suggested and received no js alerts at all. I did notice that the JQuery runs after the code behind. When that happens, all the checkboxes are cleared and the original records are marked as Sent. I'm wondering if I shouldn't just abandon this JQuery idea and go straight c# code behind?

    Tuesday, December 13, 2016 4:56 PM
  • User-1672470423 posted

    Please remove AutoPostBack="True" from checkbox, as it will cause postback each time it is check/uncheck //in case you are not doing anything on server on that particular action
    On button client click: OnClientClick="return GetAllCgheckedCheckboxes();" //to avoid postback till the client function is executed
    add return true; as last line inside GetAllCgheckedCheckboxes() function. //to continue postback

    Tuesday, December 13, 2016 5:55 PM
  • User-718146471 posted

    Man, there are days I feel like a village idiot. I've followed your recommendations and hdCheckedIDs is blank. Here is what I have, the entire page along with the bit in the code behind and I am setting the breakpoint on the first line, then I step through the lines to SelectedIDs.ToString() but it has no value.

    Code behind class to display the variable value:

            protected void btnSendChecked_Click(object sender, EventArgs e)
            {
                // Checked IDs are coming back as unassigned/null - 12/9/16
                string SelectedIDs = hdCheckedIDs.Value.ToString();
                Response.Write(SelectedIDs.ToString());
    

    unsent.aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Unsent.aspx.cs" Inherits="AuditTool.Unsent" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
            <title>Unsent Requests</title>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <script src="../framework/js/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            function GetAllCheckedCheckboxes() {
                var ids = '';
                 $("#btnSend").click(function () {
                     var text = []; //difine a string array to store the selected ID.
                     //loop through the GridView and get all checkbox
                     $('#gvUnsent input[type=checkbox]').each(function () {
                         if ($(this).is(":checked")) {//If the checkbox is checked, get the ID.
                             var id = $(this).parent().attr("data-MatrixID"); //get the custom attribute.
                             text.push(id); //insert the selected value into the array.
                         }
                     });
                     $("#hdCheckedIDs").val(text.join()); 
                 })
    return true; } </script> <div runat="server" style="text-align: center;"> <h1>Unsent Requests</h1> </div> <p style="text-align: center"> <asp:Label ID="lblNoUnsent" runat="server" Text="No unsent requests are available." Font-Names="Arial" Font-Size="Large"></asp:Label> <asp:GridView ID="gvUnsent" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" AutoGenerateColumns="False" DataKeyNames="RecID"> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:TemplateField HeaderText="Send" ControlStyle-Width="50px" ItemStyle-HorizontalAlign="Right"> <EditItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </EditItemTemplate> <ItemTemplate> <asp:CheckBox ID="cbSendTo" runat="server" TextAlign="Left" data-MatrixID ='<%# Eval("RecID") %>' /> <asp:Label ID="lblSendTo" runat="server" Text='<%# Bind("RecID") %>' Visible="False"></asp:Label> </ItemTemplate> <ControlStyle Width="50px" /> <ItemStyle HorizontalAlign="Left" Width="75px" /> </asp:TemplateField> <asp:BoundField DataField="AppName" HeaderText="Application" ControlStyle-Width="150px" > <ControlStyle Width="150px" /> </asp:BoundField> <asp:BoundField DataField="AuditorName" HeaderText="Auditor" ControlStyle-Width="250px" > <ControlStyle Width="250px" /> </asp:BoundField> <asp:BoundField DataField="DateSubmitted" DataFormatString="{0:MM/dd/yy }" HeaderText="Submitted" ControlStyle-Width="150px" > <ControlStyle Width="150px" /> <ItemStyle HorizontalAlign="Right" /> </asp:BoundField> </Columns> <EditRowStyle BackColor="#2461BF" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#EFF3FB" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#F5F7FB" /> <SortedAscendingHeaderStyle BackColor="#6D95E1" /> <SortedDescendingCellStyle BackColor="#E9EBEF" /> <SortedDescendingHeaderStyle BackColor="#4870BE" /> </asp:GridView> <asp:HiddenField ID="hdCheckedIDs" runat="server" ClientIDMode="static" /> <asp:Button ID="btnSendChecked" runat="server" Text="Send Checked Items" Width="201px" OnClick="btnSendChecked_Click"
    OnClientClick="return GetAllCheckedCheckboxes();" /> </p> </asp:Content>

    Tuesday, December 13, 2016 7:30 PM
  • User-1672470423 posted

    Did you check what I suggested on previous post

    2. If not check by setting checkbox id to RecID directly

    <asp:CheckBox ID="<%# Eval("RecID") %>" runat="server" AutoPostBack="True" TextAlign="Left" />

    and access id using

    var id = $(this).attr('id');

    And I hope your grid binding code is inside not is postback in page_load.

    if (!IsPostBack)
        {
            PopulateGridview(); // code to populate ur grid.
        }

    Just saw that

    $('#gvUnsent input[type=checkbox]') should be

    $('#<%=gvUnsent.ClientID%> input[type=checkbox]')

    Tuesday, December 13, 2016 7:42 PM
  • User2103319870 posted

    You can try with the below jquery code to get the checkbox checked values

     function GetAllCheckedCheckboxes() {
                //variable to hold the values
                var hdntxt = '';
                //loop through each seleted checkbox in gridview and then get the value from hiddenfield column
                $("input[name$=cbSendTo]:checked").each(function () {
                    hdntxt += "," + $(this).closest("td").find(":hidden").val()
                });
                //assign the value to hiddenfield control
                $("#hdCheckedIDs").val(hdntxt.substring(1, hdntxt.length));
                return true;
            }

    Modify the Gridview markup to include a hiddenfield

    <asp:TemplateField HeaderText="Send" ControlStyle-Width="50px" ItemStyle-HorizontalAlign="Right">
                    <EditItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="cbSendTo" runat="server" TextAlign="Left" />
                        <asp:HiddenField ID="hdID" runat="server" Value='<%# Eval("AddressID") %>' />
                    </ItemTemplate>
                    <ControlStyle Width="50px" />
                    <ItemStyle HorizontalAlign="Left" Width="75px" />
                </asp:TemplateField>

    Complete Code

      <script type="text/javascript">
           
            function GetAllCheckedCheckboxes() {
                //variable to hold the values
                var hdntxt = '';
                //loop through each seleted checkbox in gridview and then get the value from hiddenfield column
                $("input[name$=cbSendTo]:checked").each(function () {
                    hdntxt += "," + $(this).closest("td").find(":hidden").val()
                });
                //assign the value to hiddenfield control
                $("#hdCheckedIDs").val(hdntxt.substring(1, hdntxt.length));
                return true;
            }
        </script>
        <asp:GridView ID="gvUnsent" CellPadding="5" runat="server" ShowFooter="True" AutoGenerateColumns="false"
            DataSourceID="SqlDataSource1" DataKeyNames="AddressID,AddressLine1">
            <Columns>
                <asp:TemplateField HeaderText="Send" ControlStyle-Width="50px" ItemStyle-HorizontalAlign="Right">
                    <EditItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="cbSendTo" runat="server" TextAlign="Left" />
                        <asp:HiddenField ID="hdID" runat="server" Value='<%# Eval("AddressID") %>' />
                    </ItemTemplate>
                    <ControlStyle Width="50px" />
                    <ItemStyle HorizontalAlign="Left" Width="75px" />
                </asp:TemplateField>
                <asp:BoundField DataField="AddressID" HeaderText="AddressID" ItemStyle-Width="150px" />
                <asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1" ItemStyle-Width="100px" />
                <asp:BoundField DataField="StateProvinceID" HeaderText="StateProvinceID" ItemStyle-Width="100px" />
                <asp:BoundField DataField="City" HeaderText="City" HtmlEncode="false"
                    ItemStyle-Width="100px" />
    
            </Columns>
            <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    
        </asp:GridView>
        <asp:HiddenField ID="hdCheckedIDs" runat="server" ClientIDMode="static" />
        <asp:Button ID="btnSendChecked" runat="server" OnClientClick="return GetAllCheckedCheckboxes();" OnClick="btnSendChecked_Click" Text="Send Checked Items" Width="201px" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
            SelectCommand="SELECT top 5 [AddressID], [AddressLine1], [AddressLine2], [City], [StateProvinceID] FROM  Person.Address a"></asp:SqlDataSource>

    Please note that above code has been changed to make it work for AdventureWorks database, you may need to modify Datafield mapping inorder to make if work for your database tables.

    Tuesday, December 13, 2016 10:05 PM
  • User2103319870 posted

    <asp:Label ID="lblSendTo" runat="server" Text='<%# Bind("RecID") %>' Visible="False"></asp:Label>

    Adding Visible False to label control will make the label to not render on page at runtime, so if your intention is to read values internally with out showing on UI, then this logic wont work. You might consider using a hiddenfield for it,

    or hide the label using css display property

    Tuesday, December 13, 2016 10:07 PM
  • User-1768369891 posted

    function GetAllCheckedCheckboxes() { var ids = ''; $("#btnSend").click(function () {

    You used wrong way jquery/ javascript.... 

    1. With in button click function you defined another click event in jquery.

    $('#gvUnsent

    2.  Gridview id gvUnsent in javascript not accessible due to change in master page with content place holder id so you can set clientID mode static in gridview or try to get id through client id..

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script
      src="https://code.jquery.com/jquery-3.1.1.min.js"
      integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
      crossorigin="anonymous"></script>
        <script type="text/javascript">
            function GetAllCheckedCheckboxes() {
                var ids = '';                       
                    var text = []; //difine a string array to store the selected ID.
                //loop through the GridView and get all checkbox
    
                    $('#gvUnsent input[type=checkbox]').each(function () {
                        
                        if ($(this).is(":checked")) {//If the checkbox is checked, get the ID.
                            var id = $(this).parent().attr("data-MatrixID"); //get the custom attribute.
                            alert(id);
                            text.push(id); //insert the selected value into the array.
                        }
                    });
                    $("#hdCheckedIDs").val(text.join());
                
                return true;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div runat="server" style="text-align: center;">
                <h1>Unsent Requests</h1>
            </div>
            <div>
                <p style="text-align: center">
                    <asp:Label ID="lblNoUnsent" runat="server" Text="No unsent requests are available." Font-Names="Arial" Font-Size="Large"></asp:Label>
                    <asp:GridView ID="gvUnsent" ClientIDMode="Static" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" AutoGenerateColumns="False" DataKeyNames="RecID">
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:TemplateField HeaderText="Send" ControlStyle-Width="50px" ItemStyle-HorizontalAlign="Right">
                                <EditItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="cbSendTo" runat="server" TextAlign="Left" data-MatrixID='<%# Eval("RecID") %>' />
                                    <asp:Label ID="lblSendTo" runat="server" Text='<%# Bind("RecID") %>' Visible="False"></asp:Label>
                                </ItemTemplate>
                                <ControlStyle Width="50px" />
                                <ItemStyle HorizontalAlign="Left" Width="75px" />
                            </asp:TemplateField>
                            <asp:BoundField DataField="AppName" HeaderText="Application" ControlStyle-Width="150px">
                                <ControlStyle Width="150px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="AuditorName" HeaderText="Auditor" ControlStyle-Width="250px">
                                <ControlStyle Width="250px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="DateSubmitted" DataFormatString="{0:MM/dd/yy }" HeaderText="Submitted" ControlStyle-Width="150px">
                                <ControlStyle Width="150px" />
                                <ItemStyle HorizontalAlign="Right" />
                            </asp:BoundField>
                        </Columns>
                        <EditRowStyle BackColor="#2461BF" />
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#EFF3FB" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F5F7FB" />
                        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                        <SortedDescendingCellStyle BackColor="#E9EBEF" />
                        <SortedDescendingHeaderStyle BackColor="#4870BE" />
                    </asp:GridView>
                    <asp:HiddenField ID="hdCheckedIDs" runat="server" ClientIDMode="static" />
                    <asp:Button ID="btnSendChecked" runat="server" Text="Send Checked Items" Width="201px" OnClick="btnSendChecked_Click"
                        OnClientClick="return GetAllCheckedCheckboxes();" />
                </p>
            </div>
        </form>
    </body>
    </html>

    and cs file is 

     public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    DataSoruce _dvsource = new DataSoruce();
                    gvUnsent.DataSource = _dvsource._source.ToList();
                    gvUnsent.DataBind();
                }
            }
            protected void btnSendChecked_Click(object sender, EventArgs e)
            {
                // Checked IDs are coming back as unassigned/null - 12/9/16
                string SelectedIDs = hdCheckedIDs.Value.ToString();
                Response.Write(SelectedIDs.ToString());
            }
        }
        public class DataSoruce
        {
           public List<Items> _source = new List<Items>() { };
            public DataSoruce()
            {
                for (int i = 1; i <= 100; i++)
                {
                    Items _item = new Items();
                    _item.RecID = i.ToString();
                    _item.AppName = "Test App";
                    _item.AuditorName = "Navneet Mitawa";
                    _item.DateSubmitted = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
    
                    _source.Add(_item);
                }
            }
        }
        public class Items
        {
            public string RecID { get; set; }
            public string AppName { get; set; }
            public string AuditorName { get; set; }
            public string DateSubmitted { get; set; }
            //public string RecID { get; set; }
        }

    Try this working fine my side..


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 14, 2016 5:23 AM