解决方案 »

  1.   

    CheckBoxList?
    items.add(数据库返回值);
      

  2.   

    复选框实现绑定数据库并删除、更新信息
    //获取数据库信息加载到页面
    public void GV_DataBind()
        {
            string sqlstr = "select * from tb_inf ";
            sqlcon = new SqlConnection(strCon);
            SqlDataAdapter da = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet ds = new DataSet();
            sqlcon.Open();
            da.Fill(ds, "tb_inf");
            sqlcon.Close();
            this.GridView1.DataSource = ds;
            this.GridView1.DataKeyNames = new string[] { "id" };
            this.GridView1.DataBind();
            if (GridView1.Rows.Count > 0)
            {
                return;//有数据,不要处理
            }
            else//显示表头并显示没有数据的提示信息
            {
                StrHelper.GridViewHeader(GridView1);
            }
    }//CodeGo.net/
    //绑定控件实现删除操作
    protected void btnDeleteMore_Click(object sender, EventArgs e)
        {
            sqlcon = new SqlConnection(strCon);//创建数据库连接
            SqlCommand sqlcom;//创建命令对象变量
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)//循环遍历GridView控件每一项
            {
                CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("cbSingleOrMore");//查找嵌套在GridView控件中的单选框
                if (cbox.Checked == true)//如果操作为选中状态
                {
                    string strSql = "delete from tb_inf where id=@id";//定义带参数的删除语句
                    if (sqlcon.State.Equals(ConnectionState.Closed))
                        sqlcon.Open();//打开数据加连接
                    sqlcom = new SqlCommand(strSql, sqlcon);//创建执行删除操作的命令对象
                    SqlParameter prame = new SqlParameter("@id", SqlDbType.Int, 4);//定义参数
                    sqlcom.Parameters.Add(prame);//添加参数
                    sqlcom.Parameters["@id"].Value = GridView1.DataKeys[i].Value.ToString();//参数赋值
                    if (sqlcom.ExecuteNonQuery() > 0)//判断删除是否成功
                    {
                        StrHelper.Alert("删除成功!");
                    }
                    else
                    {
                        StrHelper.Alert("删除失败!");
                    }
                    sqlcon.Close();//关闭数据库连接
                }
            }
            GV_DataBind();//重新绑定数据控件
        }
       protected void btnDeleteMore_Load(object sender, EventArgs e)
        {
            ((Button)sender).Attributes["onclick"] = "return confirm('您确定要删除吗?')";
        }
    //对数据库数据更新操作
      protected void btnUpdateTime_Click(object sender, EventArgs e)
        {
            sqlcon = new SqlConnection(strCon);//创建数据库连接
            SqlCommand sqlcom;//创建命令对象变量
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)//循环遍历GridView控件每一项
            {
                CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("cbSingleOrMore");
                if (cbox.Checked == true)
                {
                    string strSql = "Update tb_inf set issueDate=@UpdateTime where id=@id";
                    if (sqlcon.State.Equals(ConnectionState.Closed))
                        sqlcon.Open();//打开数据库连接
                    sqlcom = new SqlCommand(strSql, sqlcon);
                    SqlParameter[] prams = {
                                              new SqlParameter("@UpdateTime",SqlDbType.DateTime),
                                              new SqlParameter("@id",SqlDbType.Int,4)
                                          };
                    prams[0].Value = DateTime.Now;
                    prams[1].Value = GridView1.DataKeys[i].Value;
                    foreach (SqlParameter parameter in prams)
                    {
                        sqlcom.Parameters.Add(parameter);
                    }
                    if (sqlcom.ExecuteNonQuery() > 0)
                    {
                        StrHelper.Alert("更新成功!");
                    }
                    else
                    {
                        StrHelper.Alert("更新失败!");
                    }
                    sqlcon.Close();
                }
            }
            GV_DataBind();
        }