📄 admin_class.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 calss : System.Web.UI.Page
{
SqlConnection connections;
string stringsql;
public void Bindgrid()
{
stringsql = "select * from tabclas";
SqlDataAdapter sqlda = new SqlDataAdapter(stringsql, connections);
DataSet datas = new DataSet();
sqlda.Fill(datas, "class");
DataGrid1.DataSource = datas.Tables["class"].DefaultView;
DataGrid1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if ((Session["user_id"] == null || (int)Session["user_lever"] != 1))
{
Response.Redirect("default.aspx");
}
this.Title = "班级管理";
lbtnClass.Enabled = false;
string strconn = System.Configuration.ConfigurationManager.AppSettings["connstring"];
connections = new SqlConnection(strconn);
connections.Open();
if (!IsPostBack)
{
Bindgrid();
}
}
protected void lbtnClass_Click(object sender, EventArgs e)
{
}
protected void btnSearch_Click(object sender, EventArgs e)
{
if (dropType.Text == "班级编号")
{
stringsql = "select * from tabclas where Cid='" + txtKeyword.Text + "'";
}
else
{
stringsql = "select * from tabclas where Cname='" + txtKeyword.Text + "'";
}
SqlDataAdapter sqldater = new SqlDataAdapter(stringsql, connections);
DataSet dataset = new DataSet();
sqldater.Fill(dataset);
DataGrid1.DataSource = dataset;
DataGrid1.CurrentPageIndex = 0;
DataGrid1.DataBind();
if (DataGrid1.Items.Count == 0)
{
lblMsg.Text="<b>查无此班级,请检查输入!</b>";
}
}
protected void btnQuit_Click(object sender, EventArgs e)
{
Session.Clear();
Response.Redirect("default.aspx");
}
protected void btnOK_Click(object sender, EventArgs e)
{
SqlCommand commnd = new SqlCommand("addclas", connections);
commnd.CommandType = CommandType.StoredProcedure;
commnd.Parameters.Add(new SqlParameter("@cid", SqlDbType.VarChar, 50));
commnd.Parameters["@cid"].Value = txtNo.Text;
commnd.Parameters.Add(new SqlParameter("@cname", SqlDbType.VarChar, 50));
commnd.Parameters["@cname"].Value = txtClass.Text;
commnd.Parameters.Add(new SqlParameter("@ccol", SqlDbType.VarChar, 50));
commnd.Parameters["@ccol"].Value = dropCol.SelectedItem.Text;
commnd.Parameters.Add(new SqlParameter("@cdep", SqlDbType.VarChar, 50));
commnd.Parameters["@cdep"].Value = txtDep.Text;
try
{
commnd.ExecuteNonQuery();
lblMsg.Text = "<b>新班级添加成功!</b>";
Bindgrid();
}
catch (SqlException)
{
lblMsg.Text="<b>记录添加失败!</b>";
}
commnd.Connection.Close();
}
public void DataGrid_update(object sender, DataGridCommandEventArgs e)
{
string stringsql = "update tabclas set Cname=@Cname,Cdep=@Cdep,Ccol=@Ccol where Cid=@Cid";
SqlCommand commnd = new SqlCommand(stringsql, connections);
commnd.Parameters.Add(new SqlParameter("@Cname", SqlDbType.VarChar, 50));
commnd.Parameters.Add(new SqlParameter("@Cdep", SqlDbType.VarChar, 50));
commnd.Parameters.Add(new SqlParameter("@Ccol", SqlDbType.VarChar, 50));
commnd.Parameters.Add(new SqlParameter("@Cid", SqlDbType.VarChar, 50));
string colvalue = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
commnd.Parameters["@Cname"].Value = colvalue;
colvalue = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
commnd.Parameters["@Cdep"].Value = colvalue;
colvalue = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
commnd.Parameters["@Ccol"].Value = colvalue;
commnd.Parameters["@Cid"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];
try
{
commnd.ExecuteNonQuery();
lblMsg.Text = "<b>编辑成功</b>";
DataGrid1.EditItemIndex = -1;
}
catch (SqlException)
{
lblMsg.Text = "<b>编辑失败</b>";
}
commnd.Connection.Close();
Bindgrid();
}
public void DataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
Bindgrid();
}
public void DataGrid_edit(object sender, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
Bindgrid();
}
public void DataGrid_cancel(object sender, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
Bindgrid();
}
public void DataGrid_delete(object sender, DataGridCommandEventArgs e)
{
string stringsql = "delete from tabclas where Cid=@cid";
int lastPageIndex = DataGrid1.CurrentPageIndex;
SqlCommand commnd = new SqlCommand(stringsql, connections);
commnd.Parameters.Add(new SqlParameter("@cid", SqlDbType.VarChar, 50));
commnd.Parameters["@cid"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];
try
{
commnd.ExecuteNonQuery();
if (DataGrid1.PageCount - DataGrid1.CurrentPageIndex == 1 && DataGrid1.Items.Count == 1)
{
if (DataGrid1.PageCount > 1)
lastPageIndex = lastPageIndex - 1;
else
lastPageIndex = 0;
}
DataGrid1.CurrentPageIndex = lastPageIndex;
lblMsg.Text = "<b>删除成功</b>";
}
catch (SqlException)
{
lblMsg.Text = "<b>删除失败</b>";
}
commnd.Connection.Close();
DataGrid1.EditItemIndex = -1;
Bindgrid();
}
protected void lbtnStudent_Click(object sender, EventArgs e)
{
Response.Redirect("admin_stu.aspx");
}
protected void lbtnCourse_Click(object sender, EventArgs e)
{
Response.Redirect("admin_course.aspx");
}
protected void lbtnResult_Click(object sender, EventArgs e)
{
Response.Redirect("admin_grade.aspx");
}
protected void lbtnSelect_Click(object sender, EventArgs e)
{
Response.Redirect("admin_choose.aspx");
}
protected void lbtnTeacher_Click(object sender, EventArgs e)
{
Response.Redirect("admin_teacher.aspx");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -