博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

datagrid 中 HeaderTemplate 的checkbox 选中则选中 正页记录

Posted on 2008-09-19 17:25  hyd309  阅读(405)  评论(0编辑  收藏  举报

datagrid控件: 

<HeaderTemplate>
                                            <asp:CheckBox ID="Cbx_DeleteAll" runat="server"/>
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:CheckBox ID="Cbx_Delete" runat="server"></asp:CheckBox>
                                        </ItemTemplate>  

为Cbx_DeleteAll添加 客户端脚本

  protected void DG1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {

            CheckBox chk = (CheckBox)e.Item.FindControl("Cbx_DeleteAll");
            chk.Attributes.Add("onclick", "javascript:return SelectAll(this.checked,this.id)");
        }
    }

客户端脚本

<script type="text/javascript" language="javascript">
        function SelectAll(chkVal,idVal)
        {
            var thisfrm=document.forms[0];  //是form 控件 作为一个集合
            for(i=0;i<thisfrm.length;i++)
            {
                if(idVal.indexOf('Cbx_DeleteAll')!=-1)
                {
                    if(chkVal==true)
                    {
                        thisfrm.elements[i].checked=true;
                    }
                    else
                    {
                        thisfrm.elements[i].checked=false;
                    }
                }
                else if(idVal.indexOf('Cbx_Delete'))
                {
                    if(thisfrm.elements[i].checked==false)
                    {
                        thisfrm.elements[i].chedked=false;
                    }
                }             
            }
        }
    </script>