胖在一方

出得厅堂入得厨房的胖子

导航

datagrid中实现全选

Posted on 2006-12-31 16:57  胖在一方  阅读(656)  评论(0)    收藏  举报
一、生成全选checkbox
 Private Sub dgApply_ItemCreated(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgApply.ItemCreated
        
If e.Item.ItemType = ListItemType.Header Then
            chkQX 
= New CheckBox
            chkQX.Text 
= "全选"
            e.Item.Cells(
0).Controls.Add(chkQX)
        
End If
    
End Sub

二、在DataGrid的prerender事件中,增加对全选的脚本事件注册。在ItemCreated事件中chekbox的clientid好象还没有生成。所以不能在那里注册js脚本。
 Private Sub dgApply_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles dgApply.PreRender
        
If Not chkQX Is Nothing Then
            chkQX.Attributes.Add(
"onclick"String.Format("SelectCheckBox('{0}','{1}')", dgApply.ClientID, chkQX.ClientID))
        
End If
    
End Sub

js脚本
function SelectCheckBox(tableID,chk_QX_ID)
            {
            
            
var chk=document.getElementById(chk_QX_ID);
                
var table=document.getElementById(tableID);
                
var ctrls;
                
if (table != null && chk !=null)
                {
                    ctrls
=table.getElementsByTagName("input");
                    
for (var i=0;i<ctrls.length;i++)
                    {
                        
if (ctrls[i].type=="checkbox")
                        {
                            ctrls[i].checked
=chk.checked;
                        }
                    }
                }
            }

注:暂时不没有做支持翻页的功能。等有空再加上。