Gridview 全选 删除

<asp:TemplateField HeaderText="全选">
              <HeaderTemplate>
                            <asp:CheckBox ID="CheckBoxAll" runat="server" Text="全选1" onclick="chkAll('gv', this.id)" />
                            <input type="checkbox" id="Checkbox1"   onclick="CheckBoxAllHtml(this)" />全选2
                     </HeaderTemplate>
                      <ItemTemplate>
                              <asp:CheckBox ID="Check" runat="server" />
                          </ItemTemplate>
             </asp:TemplateField>

 

 

   //方法一:普通的js
         function chkAll(controlid, chkboxAllID)
        {
           var chkall = document.getElementById(chkboxAllID); 
           var chkother = document.getElementsByTagName("input");       
           for (var i = 0; i < chkother.length; i ++ )
           {
              if( chkother[i].type == 'checkbox')
              {
                 if(chkother[i].id.indexOf(controlid) > - 1)
                 {
                    if(chkall.checked == true)
                    {
                       chkother[i].checked = true;
                    }
                    else
                    {
                       chkother[i].checked = false;
                    }
                 }
              }
           }
        }

//方法二,Jquery
        function CheckBoxAllHtml(chk) {
            $("#<%=gv.ClientID %> :checkbox").attr("checked", chk.checked);
        }

===========================

  protected void btnDelete_Click(object sender, EventArgs e)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach (GridViewRow gvr in gv.Rows)
        {
            CheckBox cb1 = (CheckBox)gvr.FindControl("Check");
            if (cb1.Checked)
            {
                sb.Append(gv.DataKeys[gvr.RowIndex].Value.ToString() + ",");
            }
        }
       
        Response.Write(sb.ToString());
    }

 

posted on 2010-07-06 10:29  xrt2004  阅读(258)  评论(1编辑  收藏  举报

导航