<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
table
{
width: 100%;
text-align: center;
background-color: blue;
font-family: 微软雅黑;
}
#tr_head
{
color: white;
}
.tr_main
{
background-color: #e0e0e0;
}
</style>
<script>
window.onload = function () {
var oCkall = document.getElementById('ckall');
var oCks = document.getElementsByClassName('ck');
//全选按钮
oCkall.onclick = function () {
for (var i = 0; i < oCks.length; i++) {
oCks[i].checked = oCkall.checked;
}
}
for (var i = 0; i < oCks.length; i++) {
oCks[i].onclick = function () {
var count = 0;
var oCkss = document.getElementsByClassName('ck');
for (var j = 0; j < oCkss.length; j++) {
if (oCkss[j].checked == true) {
count++;
}
}
if (count == oCkss.length) {
oCkall.checked = true;
}
else {
oCkall.checked = false;
}
}
}
}//onload结尾
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="删除" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br /><br />
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr id="tr_head">
<td width="70px">
<input type="checkbox" id="ckall" />全选</td>
<td>ID</td>
<td>编号</td>
<td>名称</td>
<td>系列</td>
<td>油耗</td>
<td>马力</td>
<td>排量</td>
<td>价格</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="tr_main">
<td>
<input type="checkbox" class="ck" name="shanchu" value="<%#Eval("Code") %>"/></td>
<td><%#Eval("Ids") %></td>
<td><%#Eval("Code") %></td>
<td><%#Eval("Name") %></td>
<td><%#Eval("Brand") %></td>
<td><%#Eval("Oil") %></td>
<td><%#Eval("Powers") %></td>
<td><%#Eval("Exhaust") %></td>
<td><%#Eval("Price") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += Button1_Click;
if(!IsPostBack)
{
Repeater1.DataSource = new CarsData().SelectAll();
Repeater1.DataBind();
}
}
void Button1_Click(object sender, EventArgs e)
{
string aa=Request["shanchu"];
string[] aaa = aa.Split(',');
Label1.Text = Request["shanchu"];
foreach (string a in aaa)
{
new CarsData().Delete(a);
}
Repeater1.DataSource = new CarsData().SelectAll();
Repeater1.DataBind();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
/// <summary>
/// CarsData 的摘要说明
/// </summary>
public class CarsData
{
SqlConnection conn = null;
SqlCommand cmd = null;
public CarsData()
{
conn = new SqlConnection("server=.;database=data0425;user=sa;pwd=123;");
cmd = conn.CreateCommand();
}
public List<Cars> SelectAll()
{
List<Cars> lc = new List<Cars>();
cmd.CommandText = "select * from car ";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Cars c = new Cars();
c.Ids = Convert.ToInt32(dr["Ids"]);
c.Code = dr["Code"].ToString();
c.Name = dr["Name"].ToString();
c.Brand = dr["Brand"].ToString();
c.Time = Convert.ToDateTime(dr["Time"]);
c.Oil = Convert.ToInt32(dr["Oil"]);
c.Powers = Convert.ToInt32(dr["Powers"]);
c.Exhaust = Convert.ToInt32(dr["Exhaust"]);
c.Price = Convert.ToDecimal(dr["Price"]);
lc.Add(c);
}
}
conn.Close();
return lc;
}
public void Delete(string code)
{
cmd.CommandText = "delete from car where code=@c";
cmd.Parameters.Clear();
cmd.Parameters.Add("@c",code);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Cars 的摘要说明
/// </summary>
public class Cars
{
public Cars()
{
}
private int _Ids;
public int Ids
{
get { return _Ids; }
set { _Ids = value; }
}
private string _Code;
public string Code
{
get { return _Code; }
set { _Code = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private string _Brand;
public string Brand
{
get { return _Brand; }
set { _Brand = value; }
}
private DateTime _Time;
public DateTime Time
{
get { return _Time; }
set { _Time = value; }
}
private decimal _Oil;
public decimal Oil
{
get { return _Oil; }
set { _Oil = value; }
}
private int _Powers;
public int Powers
{
get { return _Powers; }
set { _Powers = value; }
}
private int _Exhaust;
public int Exhaust
{
get { return _Exhaust; }
set { _Exhaust = value; }
}
private decimal _Price;
public decimal Price
{
get { return _Price; }
set { _Price = value; }
}
}
![](https://images2015.cnblogs.com/blog/943867/201607/943867-20160728082052481-479758363.png)