Asp gridview batch Delete and select all, And gridview batch Delete

Source: Internet
Author: User

Asp gridview batch Delete and select all, And gridview batch Delete

When I was a newbie, I just learned how to select and delete asp.net. I also referenced others' code and tried to implement it. I felt very good and sent it up.

Front-end 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 = "details">
<ItemTemplate>
<A href = "Detailed. aspx? Id = '<% # Eval ("Project id") %>' ">
<Asp: Label ID = "lbl details" runat = "server" Text = "details"> </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" type = "regxph" text = "yourobjectname"/>
<SelectedRowStyle BackColor = "#669999" Font-Bold = "True" ForeColor = "White"/>
<PagerStyle BackColor = "White" ForeColor = "#000066" HorizontalAlign = "Left"/>
<HeaderStyle BackColor = "#006699" Font-Bold = "True" ForeColor = "White"/>
</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 = voting 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;
}
}
}
// Cancel all selections
Protected void button#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 a program to delete a task.
SqlConnection con = new SqlConnection ("server =.; database = voting database; uid = sa; pwd = 401 ");
Con. Open ();
SqlCommand com = new SqlCommand (SQL, con );
Com. ExecuteNonQuery ();
Bind ();

}
// Query
Protected void Button1_Click1 (object sender, EventArgs e)
{
SqlConnection con = new SqlConnection ("server =.; database = voting database; uid = sa; pwd = 401 ");
String name = this. TextBox1.Text;

String SQL = "select * from project table where project 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 = '# efefefef'} "); // you can change the background color when you move your mouse over the canvas.
E. Row. Attributes. Add ("onmouseout", "if (this! = Prevselitem) {this. style. backgroundColor = '# ffff'} "); // restore the background color when you move the mouse away.
E. Row. Attributes. Add ("onclick", e. Row. ClientID. ToString () + ". checked = true; selectx (this)"); // click the Row to change color.

E. Row. Attributes ["style"] = "Cursor: hand"; // you can specify the hover mouse pointer as a "small hand"

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

 

Related Article

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.