home »forums »asp.net topics »client side web development »Checkbox selection in gridview

Checkbox selection in gridview

Topic RSS Feed

Posts under the topic: Checkbox selection in gridview

Posted: 11/28/2009 5:19:37 AM

Lurker 230 points Lurker
  • Joined on: 10/16/2009 1:00:00 PM
  • Posts: 46

Hi, 

I have a column in gridview which contains checkbox, i need to validate that atleast one checkbox is checked.


Posted: 11/30/2009 2:55:28 AM

Starter 710 points Starter
  • Joined on: 11/11/2009 9:12:32 AM
  • Posts: 34

Posted: 11/30/2009 10:54:03 PM

Contributor 5349 points Contributor
  • Joined on: 4/15/2009 12:12:23 PM
  • Posts: 236
answered  Answered

here's another example:http://forums.asp.net/t/1410094.aspx


Posted: 3/6/2010 7:56:45 AM

Professional 9529 points Professional
  • Joined on: 4/19/2009 1:46:52 AM
  • Posts: 219
answered  Answered

 

In ASPX:

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="CheckSelectAll" runat="server" Text="Select"  />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckSelect" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
       </Columns>
    </asp:GridView>
    <br />
    <asp:Button runat="server" ID="btnDelete" Text="Button" OnClick="btnDelete_Click" />
    </form>
</body>

<script type="text/javascript">

    var gridView1Control = document.getElementById('<%= GridView1.ClientID %>');

    $('#<%= btnDelete.ClientID %>').click(function(e)
    {
        if ($('input:checkbox[id$=CheckSelect]:checked', gridView1Control).length)
        {
            return confirm('Are you sure you want to delete ?');
        }
        else
        {
            alert('Please select at lease one record.');
            return false;
        }

    });    
    
    
    $('input:checkbox[id$=CheckSelectAll]', gridView1Control).click(function(e)
    {
        if (this.checked)
        {
            $('input:checkbox[id$=CheckSelect]', gridView1Control).attr('checked', true);
        }
        else
        {
            $('input:checkbox[id$=CheckSelect]', gridView1Control).removeAttr('checked');
        }
    });



    $('input:checkbox[id$=CheckSelect]', gridView1Control).click(function(e)
    {
        //To uncheck the header checkbox when there are no selected checkboxes in itemtemplate
        if ($('input:checkbox[id$=CheckSelect]:checked', gridView1Control).length == 0)
        {
            $('input:checkbox[id$=CheckSelectAll]', gridView1Control).removeAttr('checked');
        }
        //To check the header checkbox when there are all selected checkboxes in itemtemplate
        else if ($('input:checkbox[id$=CheckSelect]:checked', gridView1Control).length == $('input:checkbox[id$=CheckSelect]', dataList1Control).length)
        {
            $('input:checkbox[id$=CheckSelectAll]', gridView1Control).attr('checked', true);
        }

    });




</script>

</html>

 

 

 

In Codebehind:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;


    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            List<int> myTestList = new List<int>();

            for (int i = 0; i < 10; i++)
            {
                myTestList.Add(i);
            }

            GridView1.DataSource = myTestList;
            GridView1.DataBind();

        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            //Do your stuff here

        }
    }



 

 

 


Page 1 of 1 (4 items)