Introduction
GridviewIs a new data bound control introduced by Microsoft in
Visual Studio. NET 2005. Most of the operations like sorting, paging and
Selecting items fromGridviewAre already built in and you can
Use it through the design view. In this article, I will explain how you can
Select single as well as all the checkboxes which are inside
GridviewControl.
Selecting checkboxes inside the gridview Control
GridviewHasCheckboxfieldColumn which maps
Checkbox to a field in the database. In this article we won't be using that, we
Will make a checkbox in a template column. Simply add
ASP: checkboxControl in the item template of
GridviewControl. If you are working withDataGrid
Control and want the same functionality, then please check out my article: Selecting
Multiple checkboxes inside a DataGrid Control.
The HTML code looks something like this:
Collapse Copy code
< ASP: gridview ID =" Gridview1" Runat =" Server" Allowpaging =" True" Allowsorting =" True" Autogeneratecolumns =" False" Datakeynames =" Personid" Performanceid =" Mysource" Width =" Pixel PX" Cellpadding =" 4" Forecolor =" #333333" Gridlines =" None" > < Columns > < ASP: commandfield Showselectbutton =" True" / > < ASP: boundfield Datafield =" Personid" Headertext =" Personid" Insertvisible =" False" Readonly =" True" Sortexpression =" Personid" / > < ASP: boundfield Datafield =" Name" Headertext =" Name" Sortexpression =" Name" / > < ASP: templatefield Headertext =" Select" > < Itemtemplate > < ASP: checkbox ID =" Chkselect" Runat =" Server" / > < / Itemtemplate > < Headertemplate > < / Headertemplate > < / ASP: templatefield > < / Columns > < Footerstyle Backcolor =" #990000" Font-bold =" True" Forecolor =" White" / > < Rowstyle Backcolor =" # Fffbd6" Forecolor =" #333333" / > < Pagerstyle Backcolor =" # Ffcc66" Forecolor =" #333333" Horizontalalign =" Center" / > < Selectedrowstyle Backcolor =" # Ffcc66" Font-bold =" True" Forecolor =" Navy" / > < Headerstyle Backcolor =" #990000" Font-bold =" True" Forecolor =" White" / > < Alternatingrowstyle Backcolor =" White" / > < / ASP: gridview >
Now in the button click event, write this Code:
Collapse Copy code
// Stringbuilder object Stringbuilder STR = New Stringbuilder (); // Select the checkboxes from the gridview Control For (Int I = 0 ; I <gridview1.rows. Count; I ++) {gridviewrow ROW = gridview1.rows [I]; Bool Ischecked = (checkbox) Row. findcontrol ( " Chkselect" ). Checked; If (Ischecked ){ // Column 2 is the name column Str. append (gridview1.rows [I]. cells [ 2 ]. Text );}} // Prints out the result Response. Write (Str. tostring ());
The code above just iterates throughGridviewAnd selects
The checked checkboxes. Later it appends the selected value to
StringbuilderObject. In order to useStringbuilder
You will need to addSystem. TextNamespace.
Making a checkall functionality
To add a check-all functionality inGridview, Simply add
HtmlCheckboxTo the header template of the checkbox column.
Collapse Copy code
< Headertemplate > < Input ID =" Chkall" Onclick =" Javascript: selectallcheckboxes (this );" Runat =" Server" Type =" Checkbox" / > < / Headertemplate >
SelectallcheckboxesJavaScript method:
Collapse Copy code
<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 it. I hope you like the article, happy coding!