Delete the selected record instance in the gridview with checkbox in ASP. net2.0!

Source: Internet
Author: User
1   < ASP: gridview ID = "Gridview1" Runat = "Server" Autogeneratecolumns = "False" Pagesize = "5"  
2 Width = "Pixel PX" Allowpaging = "True" Allowsorting = "True"
3 Datakeynames = "Db1_1, db1_2" Onrowdeleting = "Gridview1_rowdeleting" Onpageindexchanging = "Gridview1_pageindexchanging" Onrowdatabound = "Gridview1_rowdatabound" Onsorting = "Gridviewappssorting" Height = "279px"   >
4 < Columns >
5 < ASP: templatefield Headertext = "No"   >
6 < Itemtemplate >
7 <% # This. gridview1.pageindex * This. gridview1.pagesize + This. gridview1.rows. Count +   1 %>
8 </ Itemtemplate >
9 </ ASP: templatefield >
10 < ASP: templatefield Headertext = "Select"   >
11 < Itemtemplate >
12 < ASP: checkbox ID = "Chkselect" Runat = "Server"   > </ ASP: checkbox >
13 </ Itemtemplate >
14 </ ASP: templatefield >
15 < ASP: templatefield Headertext = "Operation" Showheader = "False" >
16 < Itemtemplate >
17 < ASP: linkbutton ID = "Lbtndelete" Runat = "Server" Causesvalidation = "False" Commandname = "Delete"
18 Text = "Delete" Onclientclick = "Return confirm ('Are you sure you want to delete it? ');" > </ ASP: linkbutton >
19 </ Itemtemplate >
20 </ ASP: templatefield >
21 </ Columns >
22 < Alternatingrowstyle Backcolor = "Aquamarine"   />
23 </ ASP: gridview > 1   < ASP: checkbox ID = "Chkselectall" Runat = "Server" Text = "Select all" Oncheckedchanged = "Chkselectall_checkedchanged" Autopostback = "True"   > </ ASP: checkbox >
2 < ASP: button ID = "Btndelete" Runat = "Server" Text = "Delete" Onclick = "Btndelete_click" /> 1 Protected   Void Btndelete_click ( Object Sender, eventargs E)
2 {
3 String Strdelete =   "" ;
4 For ( Int I =   0 ; I <   This . Gridview1.rows. Count; I ++ )
5 {
6 String Label;
7 Bool Ischecked = (Checkbox) gridview1.rows [I]. findcontrol ( " Chkselect " ). Checked;
8 Label = (Label) gridview1.rows [I]. findcontrol ( " Labxh " ). Text;
9 If (Ischecked)
10 {
11 Strdelete =   " Db1_1 "   +   " = "   + Label;
12 }
13 }
14 Conn. runsql ( " Delete from db1 where "   + Strdelete
15 This . Chkselectall. Checked =   False ;
16 Gridviewbind ();
17 }
18  
19 Protected   Void Chkselectall_checkedchanged ( Object Sender, eventargs E)
20 {
21 // Retrieve the checkbox attribute by traversing the gridview row
22 For ( Int I =   0 ; I <   This . Gridview1.rows. Count; I ++ )
23 {
24 (Checkbox) gridview1.rows [I]. findcontrol ( " Chkselect " ). Checked =   This . Chkselectall. checked;
25 }
26 }

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.