ASP GridView Bulk Delete and select all

Source: Internet
Author: User

I novice just learned to select and delete all of the code is also quoted other people's codes to try a try can be achieved, feel good, sent up.

Front Code

<asp:gridview id= "GridView1" runat= "Server" allowsorting= "True" autogeneratecolumns= "False"
cellpadding= " 3 "font-size=" 9pt "backcolor=" white "bordercolor=" #CCCCCC "
borderstyle=" None "borderwidth=" 1px "width=" 675px "
onrowdatabound= "GridView1_RowDataBound" datakeynames= "Project id"
;
<footerstyle backcolor= "White" Forecolor= "#000066"/>
<columns>
<asp:templatefield headertext= "select";
<itemtemplate
<asp:checkbox id= "CheckBox1" runat= "Server"/>
</itemtemplate>
</asp:templatefield
<asp:boundfield datafield= "project name" headertext= "project name"/>
<asp:boundfield datafield= "project status" headertext= "project status"/>
<asp:templatefield headertext= "verbose";
<itemtemplate>
<a href= Detailed.aspx?id= ' <% #Eval ("Project ID")%> ' ",
<asp:label id=" lbl verbose "runat=" server "text=" verbose "></asp: Label></a>
</itemtemplate>
</asp:templatefield>

<asp:templatefield headertext= "Edit" >
<ItemTemplate>
<a href= "editproject.aspx?id= ' <% #Eval (" Project ID ")%> '" >
<asp:label id= "LbL edit" runat= "Server" text= "edit" ></asp:Label></a>
</ItemTemplate>
</asp:TemplateField>

</Columns>
<rowstyle forecolor= "#000066"/>
<selectedrowstyle backcolor= "#669999" font-bold= "True" forecolor= "white"/>
<pagerstyle backcolor= "White" forecolor= "#000066" horizontalalign= "left"/>
</asp:GridView>

Background code

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data;
Using System.Data.SqlClient;

public partial class _default:system.web.ui.page
{

protected void Page_Load (object sender, EventArgs e)
{

if (! IsPostBack)
{
Bind ();
}
}


public void bind ()
{
SqlConnection con = new SqlConnection ("server=.; Database= Poll Survey database; uid=sa;pwd=401 ");
String sql = "SELECT * from Project table";
SqlCommand com = new SqlCommand (sql, con);
SqlDataAdapter da = new SqlDataAdapter (COM);
DataSet ds = new DataSet ();
Da. Fill (DS);
Gridview1.datasource = ds;
Gridview1.databind ();
}
Select All
protected void Checkbox2_checkedchanged (object sender, EventArgs e)
{
for (int i = 0; I <= gridview1.rows.count-1; i++)
{
CheckBox Cbox = (checkbox) Gridview1.rows[i]. FindControl ("CheckBox1");
if (checkbox2.checked = = True)
{
Cbox. Checked = true;
}
Else
{
Cbox. Checked = false;
}
}
}
Select All to cancel
protected void Button1_Click (object sender, EventArgs e)
{
checkbox2.checked = false;
for (int i = 0; I <= gridview1.rows.count-1; i++)
{
CheckBox Cbox = (checkbox) Gridview1.rows[i]. FindControl ("CheckBox1");
Cbox. Checked = false;
}
}
Delete
protected void button2_click (object sender, EventArgs e)
{

String sql = "Delete from project table where";
String cal = "";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox Cbox = (checkbox) Gridview1.rows[i]. Cells[0]. FindControl ("CheckBox1");
if (Cbox. Checked = = True)
{
Cal + = "Project id=" + gridview1.datakeys[i]. Value.tostring () + "or";
}
}
if (cal! = "")
{
SQL + = Cal. Substring (0, Cal. LENGTH-3);
}
Else
{sql = "";}
Response.Write (SQL);//You can define your own programs here to delete tasks
SqlConnection con = new SqlConnection ("server=.; Database= Poll Survey database; uid=sa;pwd=401 ");
Con. Open ();
SqlCommand com = new SqlCommand (sql, con);
Com. ExecuteNonQuery ();
Bind ();

}
Inquire
protected void Button1_click1 (object sender, EventArgs e)
{
SqlConnection con = new SqlConnection ("server=.; Database= Poll Survey database; uid=sa;pwd=401 ");
String name = this. TextBox1.Text;

String sql = "SELECT * from Project table where item id= '" + TextBox1.Text + "'";

SqlCommand cmd = new SqlCommand (sql, con);
SqlDataAdapter da = new SqlDataAdapter (cmd);
DataSet ds = new DataSet ();
Da. Fill (DS);
Gridview1.datasource = ds;
Gridview1.databind ();
}


protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
E.row.attributes.add ("onmouseover", "if (this!=prevselitem) {this.style.backgroundcolor= ' #Efefef '}");// Change the background color when the mouse rests
E.row.attributes.add ("onmouseout", "if (this!=prevselitem) {this.style.backgroundcolor= ' #ffffff '}"), and//restore the background color when the mouse is moved away
E.row.attributes.add ("onclick", e.row.clientid.tostring () + ". Checked=true;selectx (This)");//click the line Color

e.row.attributes["style"] = "Cursor:hand"; Set the hover mouse pointer shape to "small hands"

}
}
protected void Button4_Click (object sender, EventArgs e)
{
Response.Redirect ("add.aspx");
}
}

ASP GridView Bulk Delete and select all

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.