GridView implementation of the full selection and deletion of source code

Source: Internet
Author: User

foreground Code
<asp:templatefield headertext= "select" > <itemtemplate></ Itemtemplate><asp:checkbox id= "CheckBox2"  runat= "Server" ></asp:checkbox></asp: Templatefield><asp:button id= "Button1"  onclick= "Button1_Click"  runat= "Server"  text= " Select "></asp:button><asp:button id=" Button2  onclick= "button2_click"  runat= "Server"  text= "Delete selected" ></asp:button>

Background code
Protected void button1_ Click (object sender, eventargs e)  { if  (button1.text ==  "All selected")  {  foreach  (gridviewrow row in gridview1.rows)  { checkbox checkbox2  =  (CheckBox) row. Cells[0]. FindControl ("CheckBox2"); checkbox2.checked = true; } button1.text =  "all not selected" ; } else { foreach  (gridviewrow row in gridview1.rows) &NBsp { checkbox checkbox2 =  (CheckBox) row. Cells[0]. FindControl ("CheckBox2"); checkbox2.checked = false; } button1.text =  "All selected ";  } } protected void button2_click (object sender, eventargs e)  {  foreach  (gridviewrow row in gridview1.rows)  { checkbox checkbox2  =  (CheckBox) row. Cells[0]. FindControl ("CheckBox2"); if  (checkbox2.checked == true)  { SqlConnection  Conn = fun.initconn ();  sqlcommand comm = new sqlcommand ();  comm. Connection = conn; comm. commandtext =  "Delete from comment where comment_id= '"  + row. CELLS[2]. text +  "'  ";  conn. Open ();  comm. ExecuteNonQuery ();  conn. Close ();  } } } 

 

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.