CheckBox的全选与不全选(刷新页面效果)

aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="wangyi.aspx.cs" Inherits="_12._18zuoye.wangyi" %>



   


   


   

   
                    DataKeyNames="sid" DataSourceID="SqlDataSource1">
           
               
                   
                                                    oncheckedchanged="CheckBox2_CheckedChanged" />
                   

                   
                       
                   

               

                                    ReadOnly="True" SortExpression="sid" />
               
               
               
           

       

       

                    ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
            DeleteCommand="DELETE FROM [student] WHERE [sid] = @sid"
            InsertCommand="INSERT INTO [student] ([sname], [sex], [age]) VALUES (@sname, @sex, @age)"
            SelectCommand="SELECT [sid], [sname], [sex], [age] FROM [student]"
            UpdateCommand="UPDATE [student] SET [sname] = @sname, [sex] = @sex, [age] = @age WHERE [sid] = @sid">
           
               
           

           
               
               
               
           

           
               
               
               
               
           

       

   
   

   



aspx.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _12._18zuoye
{
    public partial class wangyi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ckb = sender as CheckBox;//捉住CheckBook2
           
            foreach (GridViewRow row in this.GridView1.Rows)
            {
                if (row.RowType==DataControlRowType.DataRow)
                {
                    (row.Cells[0].FindControl("CheckBox1") as CheckBox).Checked = ckb.Checked;//把他的选择状态给那些记录中的CheckBook
                }
               
            }
        }
    }
}

 

你可能感兴趣的:(CheckBox的全选与不全选(刷新页面效果))