GridView 用 checkbox 全选并取值


 

方法一,用 js 实现

 

  <script type= " text/javascript ">
   // <![CDATA[
  function CheckAll(oCheckbox)
  {
    var GridView1 = document.getElementById( " <%=GridView1.ClientID %> ");
    for(i =  1;i < GridView1.rows.length- 1; i++)
   {
    
    GridView1.rows[i].cells[ 0].getElementsByTagName( " INPUT ")[ 0]. checked = oCheckbox. checked;
   }
  }
  
   // ]]>
  </script>

 <asp:GridView ID= " GridView1 " runat= " server " Font-Size= " 12px " BackColor= " #FFFFFF "
            GridLines= " Both " CellPadding= " 4 " DataKeyNames= " id " AutoGenerateColumns= " false "  OnRowCancelingEdit= " GridView1_RowCancelingEdit " OnRowDataBound= " GridView1_RowDataBound " OnRowDeleting= " GridView1_RowDeleting " OnRowEditing= " GridView1_RowEditing " OnRowUpdating= " GridView1_RowUpdating " AllowPaging= " True " OnPageIndexChanging= " GridView1_PageIndexChanging1 ">
            <HeaderStyle BackColor= " #EDEDED " Height= " 26px " />
            <Columns>
              <asp:TemplateField>
                <HeaderTemplate>
                  <input id= " Checkbox2 " type= " checkbox " onclick= " CheckAll(this) " runat= " server " /><label>全选</label>
                </HeaderTemplate>
                <ItemTemplate>
                  <asp:CheckBox ID= " ItemCheckBox " runat= " server " />
                </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText= " 序号 ">
<ItemTemplate>
<%#Container.DataItemIndex +  1%>
</ItemTemplate>
</asp:TemplateField>
                <asp:BoundField DataField= " id " HeaderText= " 编号 " ReadOnly= " True " />
                <asp:BoundField DataField= " name " HeaderText= " 姓名 " ReadOnly= " True " />
                <asp:TemplateField HeaderText= " 身份证号 ">
                    
                        <ItemTemplate>
                            <%#Eval( " card ") %>
                        </ItemTemplate>
                        
                        <EditItemTemplate>
                            <asp:TextBox ID= " TBCard " Text= ' <%#Eval("card") %> ' runat= " server "></asp:TextBox>
                            <asp:RequiredFieldValidator ID= " RequiredFieldValidator1 " Display= " Dynamic " ControlToValidate= " TBCard " runat= " server " ErrorMessage= " 请填写身份证号 "></asp:RequiredFieldValidator>
                      
                        </EditItemTemplate>
                        
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText= " 学历 ">
         
                        <ItemTemplate>
                            <%#Eval( " xueliText ")%>
                        </ItemTemplate>
                        
                        <EditItemTemplate>
                        <asp:HiddenField ID= " HDFXueli " runat= " server " Value= ' <%#Eval("xueli") %> ' />
                            <asp:DropDownList ID= " DDLXueli " runat= " server ">
                            </asp:DropDownList>         
                        </EditItemTemplate>
                        
                    </asp:TemplateField>
                
                    <asp:TemplateField HeaderText= " 价格 ">
                    
                        <ItemTemplate>
                            <%#Eval( " price ")%>
                        </ItemTemplate>
                        
                        <EditItemTemplate>      
                             <asp:TextBox ID= " TBPrice " Text= ' <%#Eval("price") %> ' runat= " server "></asp:TextBox>     
                        </EditItemTemplate>
                    
                    </asp:TemplateField>
          <asp:BoundField HeaderText= " 建立时间 " DataField= " createdate " ReadOnly= " True " />
               <asp:CommandField   ShowEditButton= " True " HeaderText= " 修改 " />
               <asp:TemplateField HeaderText= " 删除 ">
                <ItemTemplate>
                    <asp:LinkButton ID= " LinkButton1 " runat= " server " CausesValidation= " False " CommandName= " Delete "
                        Text= " 删除 " OnClientClick= " return confirm('您确认删除该记录吗?'); "></asp:LinkButton>
                </ItemTemplate>
              </asp:TemplateField>
            </Columns>

 </asp:GridView>  

 

 

       

 在 .CS 中取值:

 

 pp


  protected  void  Button1_Click( object  sender, EventArgs e)

 {
        this.Label1.Text = "";
       
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
            if (ch.Checked)
            {
                this.Label1.Text += "<li>GridView1 您选择的是(键值):" + GridView1.DataKeys[gvr.RowIndex].Value.ToString();
              
            }
        }
       
    }


 

 方法二:

 

  < asp:GridView  ID ="GridView1"  runat ="server"  AllowPaging ="True"  OnPageIndexChanging ="GridView1_PageIndexChanging"  OnRowCancelingEdit ="GridView1_RowCancelingEdit"  OnRowDataBound ="GridView1_RowDataBound"  OnRowDeleting ="GridView1_RowDeleting"  OnRowEditing ="GridView1_RowEditing"  OnRowUpdating ="GridView1_RowUpdating"  AutoGenerateColumns ="False"  DataKeyNames ="id,name"  DataMember ="card,price" >
             < Columns >
             < asp:TemplateField >
                 < HeaderTemplate >
                   < asp:CheckBox  ID ="CheckBox1"  runat ="server"  Text ="全选"  AutoPostBack ="true"  OnCheckedChanged ="CheckAll"   />
                 </ HeaderTemplate >
                 < ItemTemplate >
                   < asp:CheckBox  ID ="ItemCheckBox"  runat ="server"   />
                 </ ItemTemplate >
               </ asp:TemplateField >
               < asp:TemplateField  HeaderText ="序号" >
< ItemTemplate >
<%#Container.DataItemIndex +  1 %>
</ ItemTemplate >
</ asp:TemplateField >
                 < asp:BoundField  DataField ="id"  HeaderText ="编号"  ReadOnly ="True"   />
                 < asp:BoundField  DataField ="name"  HeaderText ="姓名"  ReadOnly ="True"   />
                
                     < asp:TemplateField  HeaderText ="身份证号" >
                    
                         < ItemTemplate >
                             <%# Eval( " card "%>
                         </ ItemTemplate >
                        
                         < EditItemTemplate >
                             < asp:TextBox  ID ="TBCard"  Text ='<%#Eval("card")  % >' runat="server"> </ asp:TextBox >
                             < asp:RequiredFieldValidator  ID ="RequiredFieldValidator1"  Display ="Dynamic"  ControlToValidate ="TBCard"  runat ="server"  ErrorMessage ="请填写身份证号" ></ asp:RequiredFieldValidator >
                      
                         </ EditItemTemplate >
                        
                     </ asp:TemplateField >
                    
                     < asp:TemplateField  HeaderText ="学历" >
         
                         < ItemTemplate >
                             <%# Eval( " xueliText ") %>
                         </ ItemTemplate >
                        
                         < EditItemTemplate >
                         < asp:HiddenField  ID ="HDFXueli"  runat ="server"  Value ='<%#Eval("xueli")  % >' />
                             < asp:DropDownList  ID ="DDLXueli"  runat ="server" >
                             </ asp:DropDownList >         
                         </ EditItemTemplate >
                        
                     </ asp:TemplateField >
                
                     < asp:TemplateField  HeaderText ="价格" >
                    
                         < ItemTemplate >
                             <%# Eval( " price ") %>
                         </ ItemTemplate >
                        
                         < EditItemTemplate >      
                              < asp:TextBox  ID ="TBPrice"  Text ='<%#Eval("price")  % >' runat="server"> </ asp:TextBox >     
                         </ EditItemTemplate >
                    
                     </ asp:TemplateField >
                
                 < asp:BoundField  HeaderText ="建立时间"  DataField ="createdate"  ReadOnly ="True"   />
                < asp:CommandField    ShowEditButton ="True"  HeaderText ="修改"   />
                < asp:TemplateField  HeaderText ="删除" >
                 < ItemTemplate >
                     < asp:LinkButton  ID ="LinkButton1"  runat ="server"  CausesValidation ="False"  CommandName ="Delete"
                        Text
="删除"  OnClientClick ="return confirm('您确认删除该记录吗?');" ></ asp:LinkButton >
                 </ ItemTemplate >
               </ asp:TemplateField >
             </ Columns >

 </asp:GridView>  

       

 在 cs中:

  

  protected  void  CheckAll( object  sender, EventArgs e)

 {
        CheckBox cbx = (CheckBox)sender;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
            ch.Checked = cbx.Checked;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.Label1.Text = "";
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
            if (ch.Checked)
            {
                this.Label1.Text += "<li>GridView1 您选择的是(键值):" + GridView1.DataKeys[gvr.RowIndex].Value.ToString();
            }
        }

    }

 

 

 

 

你可能感兴趣的:(checkbox)