深圳soho网,small office home office
当前位置 : 深圳soho网 >>  web技术  >> 文章正文

GridView1实现全选及更新

日期:2008-4-17  作者:szsoho  来源:www.szsoho.com   点击:

全选代码如下:
protected   void   CheckBox2_CheckedChanged(object   sender,   EventArgs   e)
        {
                int   i;
                if   (((CheckBox)sender).Checked)
                {
                        for   (i   =   0;   i   <   GridView1.Rows.Count;   i++)
                        {
                                ((CheckBox)GridView1.Rows[i].FindControl( "CheckBox2 ")).Checked   =   true;
                        }
                }
                else
                {
                        for   (i   =   0;   i   <   GridView1.Rows.Count;   i++)
                        {
                                ((CheckBox)GridView1.Rows[i].FindControl( "CheckBox2 ")).Checked   =   false;
                        }
                }
        }
--------------------------------------------
在HMTL中绑定CheckBox列的代码:
<asp:TemplateField   HeaderText= "选取 "   SortExpression= "Ispass ">
                                        <HeaderTemplate>
                                                全选 <asp:CheckBox   ID= "CheckBox2 "   runat= "server "   AutoPostBack= "True "   OnCheckedChanged= "CheckBox2_CheckedChanged "   />
                                        </HeaderTemplate>
                                                <EditItemTemplate>
                                                        <asp:CheckBox   ID= "CheckBox2 "   runat= "server "   Checked= ' <%#   Bind( "isPass ")   %> '   />
                                                </EditItemTemplate>
                                                <ItemTemplate>
                                                <asp:CheckBox   ID= "CheckBox2 "   runat= "server "   Checked= ' <%#   Bind( "isPass ")   %> '   />
                                        </ItemTemplate>
                                        </asp:TemplateField>

后台批量更新代码如下:
  protected   void   btnUpdate_Click(object   sender,   EventArgs   e)
        {
                CheckBox   chk;
                foreach   (GridViewRow   r   in   GridView1.Rows)
                {
                        chk   =   (CheckBox)r.FindControl( "CheckBox2 ");
                        if   (chk   !=   null)
                        {
                                if   (chk.Checked)
                                {
                                        //用GridView1.DataKeys[r.RowIndex].Value.ToString()找到主键列的值,然后进行更新操作
                                }
                        }
                }

                BindGrid();//重新绑定数据
        }