js控制Gridview中的checkbox全选(转)

js代码部分

function SelectAll(chkbox)
           {
              var theBox=chkbox;
              xState=theBox.checked;   
              elem=theBox.form.elements;
              for(i=0;i<elem.length;i++)
              if(elem[i].type=="checkbox" && elem[i].id!=theBox.id)
               {
                    if(elem[i].checked!=xState)
                       elem[i].click();
                      
               }
          }

 

.net代码部分

<asp:TemplateField>
                          <HeaderTemplate>
                            <input id="chkHeader" onclick="SelectAll(this)" type="checkbox" />
                          </HeaderTemplate>
                            <ItemTemplate>
                                <input id="chkItem" type="checkbox" name="chkItem" runat="server" value='<%#Eval("UserID") %>' />
                            </ItemTemplate>
</asp:TemplateField>                         

 

获取选中的checkbox的值

       string str = "";
        for (int i = 0; i < this.GridViewUser.Rows.Count; i++)
        {
            HtmlInputCheckBox chk = (HtmlInputCheckBox)this.GridViewUser.Rows[i].FindControl("chkItem");
            if (chk.Checked == true)
            {
                str += chk.Value+",";
            }
        }

posted on 2010-08-17 09:40  学中医的程序员  阅读(105)  评论(0编辑  收藏  举报

导航