如何在DATAGRID中一个按钮实现全选或全不选

来源:互联网 发布:软件版本管理方法 编辑:程序博客网 时间:2024/05/02 20:50

 在网上有DATAGRID中实现全选的功能,但有的时候我希望一个按钮实现全选或全不选的功能,于是将代码改动了一下:

一、首先在DATAGRID中加一个模板列,内容如下: 


<asp:TemplateColumn HeaderText="全选">
 <ItemStyle HorizontalAlign="Center"></ItemStyle>
  <HeaderTemplate>
   <asp:Button id="Button1" runat="server" Text="全选"  CommandName="allchoice"></asp:Button>
 </HeaderTemplate>
 <ItemTemplate>
   <asp:CheckBox id="bdstate" runat="server"></asp:CheckBox></FONT>
 </ItemTemplate>
 <FooterStyle HorizontalAlign="Center"></FooterStyle>
</asp:TemplateColumn>

二、在DATAGRID的ItemCommand事件中加入下列代码:


        If e.CommandName = "allchoice" Then
            Dim gb As DataGridItem
            If viewstate("allstate") = "1" Then
                viewstate("allstate") = "2"
            Else
                viewstate("allstate") = "1"
            End If
            For Each gb In yj_datagrid.Items
                Dim cb As CheckBox
                cb = gb.FindControl("bdstate")
                If viewstate("allstate") = "1" Then
                    cb.Checked = True
                Else
                    cb.Checked = False
                End If
            Next
        End If

三、在page_loadG事件中加入:


        If Not IsPostBack Then
            viewstate("allstate") = "2"
        End If
原创粉丝点击