Selecting Single and Multiple Checkboxes in Datagrid
By AzamSharp
Views: 11523

 

Introduction:

Consider a situation in which you have a checkbox template column in Datagrid web server control. If you only have 8-10 rows being displayed than you can manually select all the checkboxes. But consider selecting hundreds of rows manually. In this article I will show you that how you can select all the checkboxes quickly and more efficiently.

Selecting all checkboxes manually:

First let's see that how we can select all the checkboxes manually by clicking each checkbox one at a time. Our user interface is very simple which consists of two bound columns and one template column, which contains the checkbox.

Here is the code to select the checkboxes. This code is fired when the "Approve Articles" button is clicked.

// Iterates through the DataGrid column and select the checked articles

foreach(DataGridItem dgi in myDataGrid.Items)

{

CheckBox chkSelected = (CheckBox) dgi.FindControl("chkActive");

if(chkSelected.Checked == true)

{Response.Write(dgi.Cells[0].Text);

}

}

<ItemTemplate>
<asp:CheckBox id="chkActive" runat="server"></asp:CheckBox>
</ItemTemplate>

Let's see how we can make use of the "Select All" option which will automatically select all the checkboxes in the Datagrid control. The "Select All" checkbox is simply created in the header.

<HeaderTemplate>
Select All
<asp:CheckBox onclick="javascript:SelectAllCheckboxes(this);" id="chkAll" runat="server"></asp:CheckBox>
</HeaderTemplate>

As you can see that there is an onclick event attached to the "Select All" checkbox. When the Checkbox is clicked javascript method "SelectAllCheckboxes(this)" is executed.

<script language=javascript>
function SelectAllCheckboxes(spanChk){

// Added as ASPX uses SPAN for checkbox
var oItem = spanChk.children;
var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
xState=theBox.checked;

elm=theBox.form.elements;
for(i=0;i<elm.length;i++)
if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
{
//elm[i].click();
if(elm[i].checked!=xState)
elm[i].click();
//elm[i].checked=xState;
}
}
</script>

This is all you have to do to get the select all functionality. I hope you like the article, Happy coding ! 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Thanks
Name: luciano
Date: 1/29/2007 9:46:30 AM
Comment:
You save my work day!
Subject: Thanks
Name: mem
Date: 3/27/2007 1:24:22 AM
Comment:
Works well with asp.net 2.0 with master pages - Excelent work
Subject: Thanks for the great example
Name: shweta
Date: 4/13/2007 8:05:01 AM
Comment:
This is a great example of multi selection of checkeckboxes. Works perfect. Thanks for sharing it.
Subject: Thanks
Name: Shawn
Date: 4/19/2007 7:10:45 PM
Comment:
Thanks Gridveiew Guy, just what i needed
Subject: hii..guyyyy
Name: alagu
Date: 6/4/2007 2:55:15 AM
Comment:
nice... i got wat i needed...thanksssssssss
Subject: checked property false
Name: Pavi
Date: 7/26/2007 12:14:07 PM
Comment:
The checkbox checked property always returns false inspite of the checkbox being selected. ANy reason this could happen?
Subject: RE: Checked Property False
Name: AzamSharp
Date: 7/26/2007 2:46:52 PM
Comment:
Hi Pavi,

Make sure you are using ASP Checkbox control and not the Checkbox field column inside the GridView control.
Subject: thanks
Name: praveen
Date: 8/7/2007 7:45:35 AM
Comment:
Thanks for providing this article
Subject: What about Master/Details?
Name: rmp
Date: 8/9/2007 1:10:11 PM
Comment:
I have a gridview nexted inside a gridview. Lets call the outside Master and the nested Details. My checkboxes are in Details, and I want the Un/Check All box to only control the Master row it is in. Using this post, I get all checkboxes on the page checked. Help?
Subject: RE: What about Master/Details?
Name: AzamSharp
Date: 8/10/2007 2:14:12 PM
Comment:
Dear rmp,

You will need to use the pattern matching to match the GridView with the checkboxes. I have explained this in one of my articles. You can check out the following article:

http://thedotnetguy.com/ArticleDetails.aspx?articleID=228

Subject: CheckBox selections in rows
Name: Hash
Date: 9/14/2007 8:18:39 AM
Comment:
Is there any mechanism to select all checkboxs in a row?
Subject: RE: CheckBox selections in rows
Name: AzamSharp
Date: 9/14/2007 2:11:45 PM
Comment:
Hi Hash,

It is possible but what scenario requires this design?
Subject: mullti select checkbox
Name: ahsan
Date: 9/24/2007 2:39:25 AM
Comment:
thanks
i got good solution.
i have solve my problem with this clue.


thanks & Regards
Mohd Ahsan
Subject: Select All
Name: Frank
Date: 11/29/2007 10:49:14 AM
Comment:
Very nice. Thank you.
Subject: saying thanks
Name: Neetu
Date: 3/4/2008 12:28:13 AM
Comment:
thank you very very much....really i was searching this from last two days...finally i got here....thank you very very much..



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008