📄 st_student.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class ST_student : System.Web.UI.Page
{
string st_sqlstr;
SqlConnection st_conn;
protected void Page_Load(object sender,EventArgs e)
{
// 在此处放置用户代码以初始化页面
// 获得数据库连接字串
string st_connstr = ConfigurationManager.AppSettings["st_dbconn"];
// 建立连接
st_conn = new SqlConnection(st_connstr);
if (!IsPostBack)
ST_BindGrid();
}
protected void st_btn_all_Click(object sender, EventArgs e)
{
ST_BindGrid();
}
//点击DataGrid的导航按钮时(下侧的数字)执行下面代码
public void DataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
st_dtg_student.CurrentPageIndex = e.NewPageIndex;
ST_BindGrid();
}
public void DataGrid_cancel(object sender, DataGridCommandEventArgs e)
{
st_dtg_student.EditItemIndex = -1;
ST_BindGrid();
}
public void DataGrid_edit(object sender, DataGridCommandEventArgs e)
{
st_dtg_student.EditItemIndex = (int)e.Item.ItemIndex;
ST_BindGrid();
}
public void DataGrid_update(object sender, DataGridCommandEventArgs e)
{
//创建sql语句,实现自断更新。
string st_sqlstr = "update ST_student set ST_Student_name=@Student_name,ST_Student_sex=@Student_sex,ST_Student_nation=@Student_nation,ST_Student_birthday=@Student_birthday,ST_Student_time=@Student_time,ST_Student_classid=@Student_classid,ST_Student_home=@Student_home,ST_Student_else=@Student_else where ST_Student_id=@Student_id";
SqlCommand st_comm = new SqlCommand(st_sqlstr, st_conn);
try
{
st_comm.Parameters.Add(new SqlParameter("@Student_name", SqlDbType.VarChar, 50));
st_comm.Parameters.Add(new SqlParameter("@Student_sex", SqlDbType.Char, 10));
st_comm.Parameters.Add(new SqlParameter("@Student_nation", SqlDbType.Char, 10));
st_comm.Parameters.Add(new SqlParameter("@Student_birthday", SqlDbType.DateTime, 8));
st_comm.Parameters.Add(new SqlParameter("@Student_time", SqlDbType.DateTime, 8));
st_comm.Parameters.Add(new SqlParameter("@Student_classid", SqlDbType.VarChar, 50));
st_comm.Parameters.Add(new SqlParameter("@Student_home", SqlDbType.VarChar, 50));
st_comm.Parameters.Add(new SqlParameter("@Student_else", SqlDbType.VarChar, 50));
st_comm.Parameters.Add(new SqlParameter("@Student_id", SqlDbType.VarChar, 50));
string st_st_colvalue = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
st_comm.Parameters["@Student_name"].Value = st_st_colvalue;
st_st_colvalue = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
st_comm.Parameters["@Student_sex"].Value = st_st_colvalue;
st_st_colvalue = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
st_comm.Parameters["@Student_nation"].Value = st_st_colvalue;
st_st_colvalue = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
st_comm.Parameters["@Student_birthday"].Value = st_st_colvalue;
st_st_colvalue = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
st_comm.Parameters["@Student_time"].Value = st_st_colvalue;
st_st_colvalue = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
st_comm.Parameters["@Student_classid"].Value = st_st_colvalue;
st_st_colvalue = ((TextBox)e.Item.Cells[7].Controls[0]).Text;
st_comm.Parameters["@Student_home"].Value = st_st_colvalue;
st_st_colvalue = ((TextBox)e.Item.Cells[8].Controls[0]).Text;
st_comm.Parameters["@Student_else"].Value = st_st_colvalue;
st_comm.Parameters["@Student_id"].Value = st_dtg_student.DataKeys[(int)e.Item.ItemIndex];
//打开数据库连接
st_comm.Connection.Open();
//执行sql语句
st_comm.ExecuteNonQuery();
st_lbl_note.Text = "编辑成功";
st_dtg_student.EditItemIndex = -1;
}
catch
{
st_lbl_note.Text = "编辑失败,请检查输入!";
st_lbl_note.Style["color"] = "red";
}
st_comm.Connection.Close();
ST_BindGrid();
}
public void DataGrid_delete(object sender, DataGridCommandEventArgs e)
{
string st_sqlstr = "delete from ST_student where ST_Student_id=@userid";
SqlCommand st_comm = new SqlCommand(st_sqlstr, st_conn);
st_comm.Parameters.Add(new SqlParameter("@userid", SqlDbType.VarChar, 50));
st_comm.Parameters["@userid"].Value = st_dtg_student.DataKeys[(int)e.Item.ItemIndex];
st_comm.Connection.Open();
try
{
st_comm.ExecuteNonQuery();
st_lbl_note.Text = "删除成功";
}
catch (SqlException)
{
st_lbl_note.Text = "删除失败";
st_lbl_note.Style["color"] = "red";
}
st_comm.Connection.Close();
ST_BindGrid();
}
public void ST_BindGrid()
{
st_sqlstr = "select * from ST_student";
SqlDataAdapter st_adp = new SqlDataAdapter(st_sqlstr, st_conn);
DataSet st_ds = new DataSet();
st_adp.Fill(st_ds);
st_dtg_student.DataSource = st_ds;
st_dtg_student.DataBind();
}
protected void st_btn_exit_Click(object sender, EventArgs e)
{
Response.Redirect("default.aspx");
}
protected void st_btn_ok_Click(object sender, EventArgs e)
{
st_sqlstr = "select * from ST_student where ST_Student_id='" + st_tbx_sortid.Text + "'or ST_Student_name='" + st_tbx_name.Text + "'";
SqlDataAdapter st_adp = new SqlDataAdapter(st_sqlstr, st_conn);
DataSet st_ds = new DataSet();
st_adp.Fill(st_ds);
st_dtg_student.DataSource = st_ds;
st_dtg_student.DataBind();
}
protected void st_lbtn_add_Click(object sender, EventArgs e)
{
//将网页重定向到ST_addstudent.aspx
Response.Redirect("ST_addstudent.aspx");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -