Js 实战3(实现全选)

时间:2022-12-08 00:05:04

Js 实战3(实现全选)

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

 <!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 100%; text-align: center; background-color: navy;">
<tr style="color: white;">
<td>全选
<input type="checkbox" id="ckall" />
</td>
<td>ids</td>
<td>pic</td>
<td>name</td>
<td>nowPrice</td>
<td>oldPrice</td>
<td>context</td>
</tr>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr style="background-color: white;">
<td>
<input type="checkbox" class="ck" name="ck" value="<%#Eval("ids") %>" />
</td>
<td><%#Eval("ids") %> </td>
<td><%#Eval("pic") %></td>
<td><%#Eval("name") %></td>
<td><%#Eval("nowPrice") %></td>
<td><%#Eval("oldPrice") %></td>
<td><%#Eval("context") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="删除" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
document.getElementById("ckall").onclick = function () {
var oCks = document.getElementsByClassName("ck");
for (var i = ; i < oCks.length; i++) {
oCks[i].checked = this.checked;
}
}; var ooCks = document.getElementsByClassName("ck"); for (var i = ; i < ooCks.length; i++) {
ooCks[i].onclick = function () {
var count = ;
for (var j = ; j < ooCks.length; j++) {
if (ooCks[j].checked == false) {
count++;
}
} if (count > ) {
document.getElementById("ckall").checked = false;
}
else {
document.getElementById("ckall").checked = true;
} };
}
</script>

删除操作:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Repeater1.DataSource = new gouwuData().Select();
Repeater1.DataBind();
} Button1.Click += Button1_Click;
} void Button1_Click(object sender, EventArgs e)
{
string s = Request["ck"]; //Label1.Text = s;
if (s != null)
{
string[] ss = s.Split(',');
for (int i = ; i < ss.Length; i++)
{
new gouwuData().Delete(Convert.ToInt32(ss[i]));
}
Response.Redirect("Default4.aspx");
}
else
{
Label1.Text = "未选择数据!";
}
}
}