2 Methods for using checkbox deletion in the ASP.net Gridview sharing _ Practical Tips

Source: Internet
Author: User
Method One:
Background code:
Copy Code code as follows:

protected void Btn_delete_click (object sender, EventArgs e)
{
for (int i = 0; I <this. GridView1.Rows.Count; i++)
{
int id = Convert.ToInt32 (this. Gridview1.datakeys[i]. Value);
if (this. Gridview1.rows[i]. Cells[0]. FindControl ("CheckBox1") as CheckBox). Checked = = True)
{
Delete (ID);
Clientscript.registerstartupscript (GetType (), "hint", "<script>alert" (' Delete succeeded! ') </script> ");
}
}
This. Gridview1.databind ();
}//Delete
private void Delete (int id)
{
using (SqlConnection conn = new SqlConnection (str))
{
Conn. Open ();
SqlCommand COMM = conn. CreateCommand ();
Comm.commandtext = "Delete from notice_msg where id= @id";
Comm. Parameters.Add (New SqlParameter ("@id", id));
Comm. ExecuteNonQuery ();
}
}

Foreground code:
Copy Code code as follows:

<asp:gridview id= "GridView1" runat= "Server" datakeynames= "ID" >

You also have to add a column to bind the field ID, and set the Visable property of this column to False
Method Two:
Background:
Copy Code code as follows:

protected void Btn_delete_click (object sender, EventArgs e)
{
foreach (GridViewRow row in this.) Gridview1.rows)
{
if (row. RowType = = Datacontrolrowtype.datarow)
{
CheckBox ckb = row. CELLS[2]. FindControl ("CheckBox1") as CheckBox;
if (ckb. Checked)
{
using (SqlConnection sqlcnn = new SqlConnection (str))
{
using (SqlCommand SQLCMM = Sqlcnn.createcommand ())
{
Sqlcmm.commandtext = "Delete from regime_table where id= '" + row. Cells[0]. Text + "'";
Sqlcnn.open ();
int a= sqlcmm.executenonquery ();
if (a>0)
{
Clientscript.registerstartupscript (GetType (), "hint", "<script>alert" (' Delete succeeded! ') </script> ");
}
Else
{
Clientscript.registerstartupscript (GetType (), "hint", "<script>alert" (' Delete failed! ') </script> ");
}
This. DataBind ();
}
}
}
}
}
}

Front desk:
Copy Code code as follows:

<style type= "Text/css" >
. Hidden
{
Display:none;
}
</style>
<asp:boundfield datafield= "id" headertext= "number" >
<itemstyle cssclass= "Hidden"/>
</asp:BoundField>

Add a new column, this column binds the ID field, and the Visable property cannot be false, otherwise the value cannot be taken.

CheckBox Full selection Feature:
Copy Code code as follows:

<script type= "Text/jscript" >
function Change (sender) {
var table = document.getElementById ("GridView1");
for (var i = 1; i < table.rows.length; i++) {
Table.rows[i].cells[1].getelementsbytagname ("input") [0].checked = sender.checked;
}
}
</script>
<HeaderTemplate>
<input id= "Checkbox2" type= "checkbox" onclick= "Change (this)"/>
Select All
</HeaderTemplate>

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.