在html中 :<script language="javascript">
function myCheck1()
{
var mycount = 0 ;
var mm = document.getElementsByTagName("input").length ;
for(var i=0;i<mm;i++)
{
var dd = document.getElementsByTagName("input").item(i);
if(dd.type == "checkbox")
{
if(dd.checked == true)
{
mycount += 1;
}
}
}
if(mycount == 0)
{
alert("您还未选择,请选择!");
return(false);
}
else
{
return(confirm("本次操作将删除该时间段的所有记录,注意!是该时间段!您真的要删除它吗?"));
}
}
</script>
<script language="javascript">
function CheckAllCus1()
{
var opt = document.getElementById("checkAccept");
var tb = document.getElementById("<%=GridView1.ClientID%>");
var arr = tb.getElementsByTagName("input");
for(i=0;i<arr.length;i++){
if(arr[i].type == "checkbox"){
arr[i].checked = opt.checked;
}
}
}
在GRIDVIEW 中
<asp:TemplateField>
<HeaderStyle Width="3%"></HeaderStyle>
<HeaderTemplate>
<asp:Label id="Label4" runat="server"> 全选 </asp:Label>
<INPUT id="checkAccept" type="checkbox" onclick="CheckAllCus1()">
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="CB1" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
在.CS中
PAGE LOAD 中:
this.shanchu.Attributes.Add("onclick", "return(myCheck1());");
在删除按钮中:
protected void shanchu_Click1(object sender, EventArgs e)
{
List<string> list1 = new List<string>();//定义一个list行的队列
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb1 = (CheckBox)GridView1.Rows[i].Cells[11].FindControl("CB1");//将GRIDVIEW中的每行的按钮的ID参数赋给 cb
if (cb1.Checked == true)
{
list1.Add(GridView1.DataKeys[i].Value.ToString());//将选中的行记录到LIST中
}
}
if (list1.Count <= 0)//没有选中的按钮
{
Response.Write("<script language='javascript'>alert('请选择要删除的项!')</script>");
return;
}
string usernos = String.Join(",", list1.ToArray());
try
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]))
{
conn.Open();
string DeleteCmd = string.Format("delete from suppliers where supplyersid in ({0})", usernos);
using (SqlCommand MyCommand = new SqlCommand(DeleteCmd, conn))
{
MyCommand.ExecuteNonQuery();
}
conn.Close();
}
GridView1.DataBind();
Response.Write("<script language='javascript'>alert('成功删除!')</script>");
}
catch
{
Response.Write("<script>alert('删除失败!');history.back();</Script>");
}
}