Chinaunix首页 | 论坛 | 博客
  • 博客访问: 141876
  • 博文数量: 44
  • 博客积分: 2505
  • 博客等级: 少校
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-19 18:38
文章分类

全部博文(44)

文章存档

2018年(3)

2016年(1)

2013年(1)

2011年(2)

2010年(11)

2009年(26)

我的朋友

分类:

2009-10-17 14:53:22

在.aspx的标签下添加如下脚本:
Js代码
  1. "text/javascript" language="javascript">   
  2. function SelectAllCheckboxes(spanChk)   
  3. {   
  4.    var oItem = spanChk.children;   
  5.    var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];   
  6.    xState=theBox.checked;   
  7.    elm=theBox.form.elements;   
  8.    for(i=0;i
  9.    if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)   
  10.    {   
  11.       if(elm[i].checked!=xState)   
  12.       elm[i].click();   
  13.    }   
  14. }   
  15.   

在GridView中的模板列添加如下代码:
C#代码
  1.   
  2.   
  3. "chkall" type="checkbox" onclick="SelectAllCheckboxes(this);" />   
  4.   
  5.   
  6. "chk" runat="server" />   
  7.   
  8. "table_head" />   
  9. "15px" />   
  10.   
  11.   
  12. "PersonId" Visible="False">   
  13.   
  14. "lblPersonId" runat="server" Text='<%# bind("PersonId") %>'>   
  15.   
  16.   


在后台通过遍历GridView中的行来计算选中行的总数
C#代码
  1. int count=0;   
  2. foreach (GridViewRow gvr in PersonGridView.Rows)   
  3. {   
  4.     bool isChecked = ((CheckBox)gvr.Cells[0].FindControl("chk")).Checked;   
  5.     if (isChecked)   
  6.     {               
  7.         int iPersonId = Int32.Parse(((Label)gvr.Cells[1].FindControl("lblPersonId")).Text);   
  8.         string strSql = "DELETE SYS_PERSON WHERE PersonId=@PersonId";   
  9.         SqlConnection con = new SqlConnection(SqlHelper.ConnectionString);   
  10.         SqlCommand cmd = new SqlCommand(strSql, con);   
  11.         cmd.Parameters.Add("@PersonId",SqlDbType.Int);         
  12.         cmd.Parameters["@PersonId"].Value = iPersonId;   
  13.         con.Open();   
  14.         cmd.ExecuteNonQuery();   
  15.         con.Close();   
  16.         count++;                                                   
  17.      }                          
  18. }              
阅读(999) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~