Selecting multiple checkboxes inside a gridview Control

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.