页面有两个控件,GridView 和 Html控件 Input(CheckBox),GridView第一列为CheckBox类型列,怎样通过控制Input(CheckBox)来控制 GridView 第一列的全选或者全消。请各位达人指教,谢谢!

解决方案 »

  1.   

    用遍历啊  foreach   if(被选中)   foreach()---遍历找到你gridview里的 checkbox 然后设置她的checked为true   这电脑上没环境  所以也不能给你真正的代码
      

  2.   

    http://blog.csdn.net/palm_java/archive/2008/02/23/2115374.aspx这是我在上面摘出来的全选与全不选的代码:
    <%...@ Page Language="C#" AutoEventWireup="true"  CodeFile="Grdview_HtmlChekbox.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript">... 
    // 说明:Javascript 控制 CheckBox 的全选与取消全选 // 整理:http://www.CodeBit.cn  function checkAll(name)
       {    
     
          var el = document.getElementsByTagName('input');   
          var len = el.length;     
          for(var i=0; i<len; i++)    
           {         
             if((el[i].type=="checkbox") && (el[i].name==name))      
                 {          
                   el[i].checked = true;         
                 }   
                   
            } 
        }
         
        function clearAll(name)
        {   
            var el = document.getElementsByTagName('input');    
            var len = el.length;    
            for(var i=0; i<len; i++)   
            {  
                if((el[i].type=="checkbox") && (el[i].name==name))      
                {  
                       el[i].checked = false;       
                 }   
            } 
                
         } 
        </script> 
    </head>
    <body>
        <form id="form1" runat="server">
     <asp:GridView ID="GridView1" runat="server" Font-Size="10pt" Height="215px" Width="698px" DataKeyNames="id">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                  <input value ="<%#Eval("id") %>"  name = "chOne" type="checkbox" />
                            </ItemTemplate>
                            <HeaderTemplate>
                                <input id="chkAll" name ="chkAll" onclick="if(this.checked==true){checkAll('chOne');}else{clearAll('chOne')}" type="checkbox" />
                            </HeaderTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
       </form>
    </body>
    </html>
      

  3.   

        protected void DataGrid1_PreRender(object sender, EventArgs e)
        {
            if (DataGrid1.Controls == null || DataGrid1.Controls.Count == 0)
            {
                return;
            }
            foreach (DataGridItem item in DataGrid1.Controls[0].Controls)
            {
                if (item.ItemType == ListItemType.Header)
                {
                    CheckBox chkAll = (CheckBox)item.FindControl("chkAll");
                    //chkAll.Attributes.Add("onclick", "checkStatus()");
                    System.Text.StringBuilder strScript = new System.Text.StringBuilder("<script language='javascript'> \n");
                    strScript.Append("    function checkStatus(test) { \n");                strScript.Append("        var bAll = true; \n");
                    strScript.Append("        bAll = document.all('" + chkAll.ClientID + "').checked; \n");
                    strScript.Append(" if(test=='1')\n{\n");
                    strScript.Append("        bAll = document.all('" + chkAll.ClientID + "').checked; \n");
                    strScript.Append(" if (bAll){bAll=false;}else{bAll=true;}\n");
                    strScript.Append(" document.all('" + chkAll.ClientID + "').checked=bAll;\n");
                    strScript.Append(" }else{\n");
                    strScript.Append(" bAll = document.all('" + chkAll.ClientID + "').checked;} \n");                for (int i = 0; i < DataGrid1.Items.Count; i++)
                    {
                        strScript.Append("        document.all('" +
                            DataGrid1.Items[i].Cells[0].FindControl("chkselect").ClientID +
                            "').checked = bAll; \n");
                    }
                    strScript.Append("    } \n");
                    strScript.Append("</script> \n");                if (!Page.IsClientScriptBlockRegistered("checkStatus"))
                        Page.RegisterClientScriptBlock("checkStatus", strScript.ToString());                return;            }
            }
        }
      

  4.   

    你可以看一下我的博客中的这篇文章也许对你有太助:全部选中与取消操作,选中后的删除(ajax)实现无刷新效果