📄 admin_teacher.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 admin : System.Web.UI.Page
{
SqlConnection connections;
string strsql;
public void Bindgrid()
{
strsql = "select * from tabtea";
SqlDataAdapter da = new SqlDataAdapter(strsql, connections);
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
lbtnTeacher.Enabled = false;
if (Session["user_id"] == null)
{
//Response.Redirect("default.aspx");
}
this.Title = "教师管理";
string strconn = System.Configuration.ConfigurationManager.AppSettings["connstring"];
connections = new SqlConnection(strconn);
if (!IsPostBack)
{
Bindgrid();
}
}
public void DataGrid_delete(object sender, DataGridCommandEventArgs e)
{
int lastPageIndex = DataGrid1.CurrentPageIndex;
string strsql = "delete from tabtea where Tid=@tid";
string strsql2 = "delete from tabtea_cour where Tid=@tid";
SqlCommand commnd = new SqlCommand(strsql, connections);
SqlCommand commnd2 = new SqlCommand(strsql2, connections);
commnd.Parameters.Add(new SqlParameter("@tid", SqlDbType.VarChar, 50));
commnd.Parameters["@tid"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];
commnd2.Parameters.Add(new SqlParameter("@tid", SqlDbType.VarChar, 50));
commnd2.Parameters["@tid"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];
commnd.Connection.Open();
//commnd2.Connection.Open();
try
{
if (DataGrid1.PageCount - DataGrid1.CurrentPageIndex == 1 && DataGrid1.Items.Count == 1)
{
if (DataGrid1.PageCount > 1)
lastPageIndex = lastPageIndex - 1;
else
lastPageIndex = 0;
}
DataGrid1.CurrentPageIndex = lastPageIndex;
commnd2.ExecuteNonQuery();
commnd.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('数据删除成功!');</script>");
}
catch (SqlException ex)
{
Response.Write("<script language=javascript>alert('数据删除失败!');</script>");
Response.Write(ex.Message);
}
commnd.Connection.Close();
commnd2.Connection.Close();
Bindgrid();
}
public void DataGrid_update(object sender, DataGridCommandEventArgs e)
{
string strsql = "update tabtea set Tname=@tname,Tcol=@tcol where Tid=@tid";
SqlCommand commnd = new SqlCommand(strsql, connections);
commnd.Parameters.Add(new SqlParameter("@tname", SqlDbType.VarChar, 50));
commnd.Parameters.Add(new SqlParameter("@tcol", SqlDbType.VarChar, 50));
commnd.Parameters.Add(new SqlParameter("@tid", SqlDbType.VarChar, 50));
string colvalue = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
commnd.Parameters["@tname"].Value = colvalue;
colvalue = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
commnd.Parameters["@tcol"].Value = colvalue;
commnd.Parameters["@tid"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];
commnd.Connection.Open();
try
{
commnd.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('数据修改成功!');</script>");
DataGrid1.EditItemIndex = -1;
}
catch (SqlException)
{
Response.Write("<script language=javascript>alert('数据修改失败,请检查输入!');</script>");
}
commnd.Connection.Close();
Bindgrid();
}
public void DataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
Bindgrid();
}
public void DataGrid_cancel(object sender, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
Bindgrid();
}
public void DataGrid_edit(object sender, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
Bindgrid();
}
protected void lbtnQuit_Click(object sender, EventArgs e)
{
Session.Clear();
Response.Redirect("default.aspx");
}
protected void btnSearch_Click(object sender, EventArgs e)
{
string sqlstring;
DataGrid1.CurrentPageIndex = 0;
if (dropType.Text == "编号")
{
sqlstring = "select * from tabtea where Tid='" + txtKeyword.Text + "'";
}
else
{
sqlstring = "select * from tabtea where Tname='" + txtKeyword.Text + "'";
}
SqlDataAdapter sqlda = new SqlDataAdapter(sqlstring, connections);
DataSet datas = new DataSet();
sqlda.Fill(datas);
DataGrid1.DataSource = datas;
DataGrid1.CurrentPageIndex = 0;
DataGrid1.DataBind();
if (DataGrid1.Items.Count == 0)
{
Response.Write("<script language=javascript>alert('查无此人,请检查输入!');</script>");
return;
}
}
protected void lbtnDisplayAll_Click(object sender, EventArgs e)
{
Bindgrid();
}
protected void lbtnAdd_Click(object sender, EventArgs e)
{
Response.Redirect("addteacher.aspx");
}
protected void lbtnCourse_Click(object sender, EventArgs e)
{
Response.Redirect("admin_course.aspx");
}
protected void lbtnStudent_Click(object sender, EventArgs e)
{
Response.Redirect("admin_stu.aspx");
}
protected void lbtnResult_Click(object sender, EventArgs e)
{
Response.Redirect("admin_grade.aspx");
}
protected void lbtnTeacher_Click(object sender, EventArgs e)
{
Response.Redirect("admin_teacher.aspx");
}
protected void lbtnClass_Click(object sender, EventArgs e)
{
Response.Redirect("admin_class.aspx");
}
protected void lbtnSelect_Click(object sender, EventArgs e)
{
Response.Redirect("admin_course.aspx");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -