后台代码
String strCon = "server=6KT3S91GBFLZ6ZK\\SQLEXPRESS;database=Stuinfo;Integrated Security=SSPI";
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
dd();
}
}
private void dd()
{
SqlConnection con = new SqlConnection(strCon);
con.Open();
String sqlStr = "select * from t_student";
SqlDataAdapter myAdapt = new SqlDataAdapter(sqlStr, con);
DataSet ds = new DataSet();
myAdapt.Fill(ds,"e1");
GridView1.DataSource = ds.Tables["e1"];
GridView1.DataBind();
con.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strCon);
con.Open();
string strSql = "Insert into t_student(S_Number,S_Name,Sex,Birthday,Nation,Politics,Department,Address,PostalCode,Phone)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "')";
SqlCommand cmd = new SqlCommand(strSql, con);
cmd.ExecuteNonQuery();
con.Close();
}
protected void Button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strCon);
con.Open();
String sqlStr = "select * from t_student";
SqlDataAdapter myAdapt = new SqlDataAdapter(sqlStr, con);
DataSet ds = new DataSet();
myAdapt.Fill(ds,"e1");
GridView1.DataSource = ds.Tables["e1"];
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
dd();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection con = new SqlConnection(strCon);
con.Open();
string sql="delete from t_student where S_Number=" + GridView1.DataKeys[e.RowIndex].Value;
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
Response.Redirect("default2.aspx");
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string KeyId = GridView1.DataKeys[e.RowIndex].Value.ToString();
string S_Number= Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString());
string S_Name = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString());
string Sex = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString());
string Birthday = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());
string Nation = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString());
string Politics = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString());
string Department = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString());
string Addess = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.ToString());
string PostalCode = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text.ToString());
string Phone = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text.ToString());
try{
SqlConnection con=new SqlConnection(strCon);
con.Open();
string sql="update t_student set S_Name='" + S_Name + "',S_Number='" + S_Number + "',Sex='" + Sex + "',Birthday='" + Birthday + "',Nation='" + Nation + "',Politics='" + Politics + "',Department='" + Department + "',Addess='" + Addess + "',PostalCode='"+PostalCode+"',Phone='"+Phone+"' where S_Number="+KeyId;
SqlCommand cmd=new SqlCommand(sql,con);
cmd.ExecuteNonQuery();
Response.Write("执行成功了.");
GridView1.EditIndex=-1;
con.Close();
}
catch(Exception exc){
Response.Write(exc.Message);
}
dd();
}