第二阶段要点提示——以多选删除为例

通过读取数据库,将信息陈列在GridView中。并可以通过点击编辑列进行更新,通过全选按钮进行选择,批量删除。界面如下图所示:

连接数据库

  1. 在Web.config中添加如下内容
    <appSettings>
       <add key="ConnectionString" value="server=localhost;database=db_09;UId=sa;password='123321'"/>
    </appSettings>
    
  2. 在使用数据库的页面中加入如下内容定义连接
    public SqlConnection GetConnection()
    {
     string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
     SqlConnection myConn = new SqlConnection(myStr);
     return myConn;
    }
    

数据库内容的绑定显示

  1. 在显示页面上加入gridview控件,并定义以下方法
    public void bind()
     {
         SqlConnection myConn = GetConnection();
         myConn.Open();
         string sqlStr = "Select * from tb_student";
         SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
         SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
         DataSet myDs = new DataSet();
         myDa.Fill(myDs, "student");
         if (myDs.Tables[0].Rows.Count > 0)
         {
             GridView1.DataSource = myDs;
             GridView1.DataKeyNames = new string[] { "id" };
             GridView1.DataBind();
         }
         else
         {
             Response.Write("表中无信息");
         }    
     }
    
  2. 在需要绑定显示数据的时候,加载bind()方法即可

更新信息

  1. 启用GridView控件的AutoGenerateEditButton属性
  2. 定义RowEditing事件
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
     {
         GridView1.EditIndex = e.NewEditIndex;
         bind();
     }
    
    其作用是调用GridView的编辑状态
  3. 定义RowUpdating事件
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
     {
         int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
         string c_Name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
         string c_Pwd = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
         string c_Sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString();
         string c_JoinTime = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
         string c_question = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString();
         string c_answer = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString();
         string c_Profession = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text.ToString();
         string sqlStr = "update tb_student set  name='"+c_Name+"',pwd='"+c_Pwd+"',sex='"+c_Sex+"',JoinTime='"+c_JoinTime+"',question='"+c_question+"',answer='"+c_answer+"',profession='"+c_Profession+"' where id="+id;
         SqlConnection myConn = GetConnection();
         myConn.Open();
         SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
         myCmd.ExecuteNonQuery();
         GridView1.EditIndex = -1;
         bind();
     }
    
  4. 定义RowCancelingEdit事件
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
     {
         GridView1.EditIndex = -1;
         bind();
     }
    

全选按钮的定义

 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            cbox.Checked = true;
        }

批量删除按钮的定义

 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                int classID = Convert.ToInt32(GridView1.DataKeys[i].Value.ToString());
                //Response.Write("<script>alert(" + classID + ")</script>");
                string sqlStr = "delete from tb_student where id=" + classID;
                SqlConnection myConn = GetConnection();
                myConn.Open();
                SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
                myCmd.ExecuteNonQuery();
            }
        }
        bind();
文档更新时间: 2018-11-20 22:13   作者:大麦