JS全选,复选框是.net服务器控件checkBox 在GridView中的模板列

11 篇文章 0 订阅
5 篇文章 0 订阅

<%@   Page   Language= "C# "   %>   
 
<%@   Import   Namespace= "System.Data "   %>    
   
<%--http://community.csdn.net/Expert/TopicView3.asp?id=5714048--%>    
 
<!DOCTYPE   html   PUBLIC   "-//W3C//DTD   XHTML   1.0   Transitional//EN "   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">    
    
 
<script   runat= "server ">   
 
 
 
        protected   void   Page_Load(object   sender,   EventArgs   e)   
 
        {   
 
                if   (!IsPostBack)   {   
 
                        LoadProductData();   
 
                }   
 
        }       
 
          
 
        void   LoadProductData()   
 
        {   
 
                DataTable   dt   =   CreateSampleProductData();   
 
 
 
                GridView1.DataSource   =   dt;   
 
                GridView1.DataBind();             
 
        }  
 
 
 
        #region   sample   data   
 
 
 
        static   DataTable   CreateSampleProductData()   
 
        {   
 
                DataTable   tbl   =   new   DataTable( "Products ");   
 
 
 
                tbl.Columns.Add( "ProductID ",   typeof(int));   
 
                tbl.Columns.Add( "ProductName ",   typeof(string));                   
 
                tbl.Columns.Add( "UnitPrice ",   typeof(decimal));   
 
                tbl.Columns.Add( "CategoryID ",   typeof(int));   
 
 
 
                tbl.Rows.Add(1,   "Chai ",   18,   1);   
 
                tbl.Rows.Add(2,   "Chang ",   19,   1);   
 
                tbl.Rows.Add(3,   "Aniseed   Syrup ",   10,   2);   
 
                tbl.Rows.Add(4,   "Chef   Anton 's   Cajun   Seasoning ",   22,   2);   
 
                tbl.Rows.Add(5,   "Chef   Anton 's   Gumbo   Mix ",   21.35,   2);   
 
                tbl.Rows.Add(47,   "Zaanse   koeken ",   9.5,   3);   
 
                tbl.Rows.Add(48,   "Chocolade ",   12.75,   3);   
 
                tbl.Rows.Add(49,   "Maxilaku ",   20,   3);                   
 
 
 
                return   tbl;   
 
        }  
 
 
 
        #endregion         
 
 
 
</script>   
 
 
 
<html   xmlns= "http://www.w3.org/1999/xhtml "   >   
 
<head   runat= "server ">   
 
        <title> CSDN_GridViewAllAndRowCheckBox </title>   
 
        <script   type= "text/javascript ">   
 
        //   选择   GridView   中指定的   checkbox   
 
        function   chkAll(sender)   
 
        {   
 
                var   grd   =   document.getElementById( "GridView1 ");   
 
                var   chkArr   =   grd.getElementsByTagName( "input ");   
 
                for(var   i   =   0;   i   <   chkArr.length;   i++)   {   
 
                        if(chkArr[i].type   ==   "checkbox "   &&     
 
                                chkArr[i].id.indexOf( "chkItem ")   >   -1)   {   
 
                                chkArr[i].checked   =   sender.checked;   
 
                        }   
 
                }   
 
        }   
 
 
 
        //   同步chkItem和chkAll   
 
        function   chkItem(sender)   
 
        {                   
 
                var   grd   =   document.getElementById( "GridView1 ");   
 
                var   chkArr   =   grd.getElementsByTagName( "input ");   
 
                var   chkAll;   
 
                for(var   i   =   0;   i   <   chkArr.length;   i++)   {                           
 
                        if(chkArr[i].type   ==   "checkbox "   &&     
 
                                chkArr[i].id.indexOf( "chkAll ")   >   -1)   {   
 
                                chkAll   =   chkArr[i];   
 
                                break;   
 
                        }   
 
                }   
 
                for(var   i   =   0;   i   <   chkArr.length;   i++)   {                           
 
                        if(chkArr[i].type   ==   "checkbox "   &&     
 
                                chkArr[i].id.indexOf( "chkItem ")   >   -1   &&     
 
                                !chkArr[i].checked)   {   
 
                                chkAll.checked   =   false;   
 
                                return;   
 
                        }   
 
                }   
 
                chkAll.checked   =   true;   
 
        }   
 
        </script>   
 
</head>   
 
<body>   
 
        <form   id= "form1 "   runat= "server ">   
 
        <div>   
 
                <asp:GridView   ID= "GridView1 "   runat= "server "   AutoGenerateColumns= "false ">   
 
                        <Columns>   
 
                                <asp:TemplateField>   
 
                                        <HeaderTemplate>   
 
                                                <asp:CheckBox   ID= "chkAll "   οnclick= 'chkAll(this) '   runat= "server "   />   
 
                                        </HeaderTemplate>   
 
                                        <ItemTemplate>   
 
                                                <asp:CheckBox   ID= "chkItem "   οnclick= 'chkItem() '   runat= "server "   />   
 
                                        </ItemTemplate>   
 
                                </asp:TemplateField>   
 
                                <asp:TemplateField   HeaderText= "ProductName ">   
 
                                        <ItemTemplate>   
 
                                                <%#   Eval( "ProductName ")   %>   
 
                                        </ItemTemplate>   
 
                                </asp:TemplateField>   
 
                                <asp:BoundField   DataField= "UnitPrice "   HeaderText= "UnitPrice "   />   
 
                        </Columns>   
 
                </asp:GridView>   
 
        </div>   
 
        </form>   
 
</body>   
 
</html>  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值