点击一个checkbox控件,使得Datagrid里的模版里放的checkbox全部选中

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace localhost.模版后台管理
{
 /// <summary>
 /// SkinManage 的摘要说明。
 /// </summary>
 public class SkinManage : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button BtnSelectAll;
  protected System.Web.UI.WebControls.Button BtnDel;
  protected System.Web.UI.WebControls.Button BtnSubmit;
  protected System.Web.UI.WebControls.CheckBox CheckBoxSelectAll;
  protected System.Web.UI.WebControls.DataGrid DataGrid2;
  protected bool selected=false;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   // 在此处放置用户代码以初始化页面
   SqlConnection Conn;
   Conn=new SqlConnection("server=localhost;database=Skin;uid=sa;pwd=''");
   Conn.Open();   
   SqlDataAdapter da=new SqlDataAdapter("select Id,Skin_name from SkinBin",Conn);
   DataSet ds=new DataSet();
   da.Fill(ds,"skin");
   da.Fill(ds,"skinall");
//   show_string=ds.Tables["skin"].Rows[0][0].ToString();   
   DataGrid2.DataSource=ds.Tables["skinall"].DefaultView;
   DataGrid2.DataBind();
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.DataGrid2.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid2_DeleteCommand);
   this.BtnSelectAll.Click += new System.EventHandler(this.BtnSelectAll_Click);
   this.CheckBoxSelectAll.CheckedChanged += new System.EventHandler(this.CheckBoxSelectAll_CheckedChanged);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void DataGrid2_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   try
   {
    SqlConnection Conn;
    Conn=new SqlConnection("server=localhost;database=Skin;uid=sa;pwd=''");
    Conn.Open();
    string Id="";
    Id=e.Item.Cells[2].Text;
    SqlCommand Comm=new SqlCommand("update SkinBin set IsDefault=1 where Id="+Id,Conn);
    Comm.ExecuteNonQuery();
   }
   catch
   {

   }
   Response.Write("<script>alert('模版已更改,请查看用户界面');</script>");

  }

  private void BtnSelectAll_Click(object sender, System.EventArgs e)
  { 
   
//   for (int i = 0; i < GridView1.Rows.Count; i++)//从第一一行循环至结束,一次更新所有的成绩
//   {       
//    GridViewRow row = GridView1.Rows[i];//定义grideviewrow
//    c.成绩 =Convert.ToDecimal(((TextBox)row.Cells[0].FindControl("TextBox1")).Text);//将TextBox1中输入的字符串转化为Decimal型
//    c.课程号 = GridView1.DataKeys[i].Values[0].ToString();
//    c.学号 = GridView1.DataKeys[i].Values[1].ToString();
//    if (ChafenBOLActions.UpdateScore(c) == 1)
//    { Label1.Text = "更新成功"; }
//    else
//    {
//     break;//更新不成功,跳出
//    }
//          
//   }
   int ct=0;
   if(IsPostBack)
   {
    
    bool b;
    ArrayList ar=new ArrayList();
    for(int i=0;i<DataGrid2.Items.Count;i++)
    { 
     b=((CheckBox)DataGrid2.Items[i].FindControl("CheckBoxSelect")).Checked;
     if (b)
     {
      ct++;
     }
    }
    
   }
   Response.Write(ct);
  }

  private void CheckBoxSelectAll_CheckedChanged(object sender, System.EventArgs e)
  { 
   
//   selected=!selected;

   for(int i=0;i<DataGrid2.Items.Count;i++)
   {     
    ((CheckBox)DataGrid2.Items[i].FindControl("CheckBoxSelect")).Checked= CheckBoxSelectAll.Checked;
   }
  }
 

  
 }
}

你可能感兴趣的:(checkbox)