📄 course.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace myResult
{
/// <summary>
/// Course 的摘要说明。
/// </summary>
public class Course : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnCourse;
protected System.Web.UI.WebControls.TextBox txtcourse;
protected SqlConnection conn;
protected SqlCommand myCmd;
protected SqlDataAdapter myDa;
protected System.Web.UI.WebControls.CustomValidator cuvcourse;
protected System.Web.UI.WebControls.RegularExpressionValidator revclass;
protected System.Web.UI.WebControls.Button btnClass;
protected System.Web.UI.WebControls.TextBox txtname;
protected System.Web.UI.WebControls.CustomValidator cuvnumber;
protected System.Web.UI.WebControls.TextBox txtnumber;
protected System.Web.UI.WebControls.Button btndelete;
protected System.Web.UI.WebControls.DataGrid dg_course;
protected System.Web.UI.WebControls.Button btndelclass;
protected System.Web.UI.WebControls.DataGrid dg_class;
protected DataSet ds;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string str_user=Session["Auser"].ToString();//Session传值
if(Session["Auser"]==null)
{
Response.Redirect("adminlogin.aspx");
}
else
{
conn=connDB.createConn();
if(!IsPostBack)
{
this.BindCourse();
this.BindClass();
this.btndelete.Attributes.Add("onclick","if(confirm('你确定要删除吗?'))return true;else return false;");//删除课程
this.btndelclass.Attributes.Add("onclick","if(confirm('你确定要删除吗?'))return true;else return false;");//删除班级
}
}
}
private void ExecuteCourse()
{
string str_course=this.txtcourse.Text.Trim().Replace("'","''");
string str_inCourse="insert into Course(CourseName) values(@Acourse)";
if(conn.State==ConnectionState.Closed)
{
conn.Open();
}
myCmd=new SqlCommand(str_inCourse,conn);
myCmd.Parameters.Add(new SqlParameter("@Acourse",SqlDbType.VarChar,50));
myCmd.Parameters["@Acourse"].Value=str_course;
myCmd.ExecuteNonQuery();
conn.Close();
string Dhtmlalert="";
Dhtmlalert += "<script language=javascript>";
Dhtmlalert += "alert('添加成功!');";
Dhtmlalert += "</script>";
Page.RegisterClientScriptBlock("alert_news",Dhtmlalert);
}
private void ExecuteClass()
{
string str_number=this.txtnumber.Text.Trim().Replace("'","''");
string str_name=this.txtname.Text.Trim().Replace("'","''");
string str_inClass="insert into Class(ClassNumber,ClassName) values(@Anumber,@Aname)";
if(conn.State==ConnectionState.Closed)
{
conn.Open();
}
myCmd=new SqlCommand(str_inClass,conn);
myCmd.Parameters.Add(new SqlParameter("@Anumber",SqlDbType.Int));
myCmd.Parameters.Add(new SqlParameter("@Aname",SqlDbType.VarChar,50));
myCmd.Parameters["@Anumber"].Value=Convert.ToInt32(str_number);
myCmd.Parameters["@Aname"].Value=str_name;
myCmd.ExecuteNonQuery();
conn.Close();
string Dhtmlalert="";
Dhtmlalert += "<script language=javascript>";
Dhtmlalert += "alert('添加成功!');";
Dhtmlalert += "</script>";
Page.RegisterClientScriptBlock("alert_news",Dhtmlalert);
}
private void BindCourse()
{
string str_selcourse="select * from Course";
myDa=new SqlDataAdapter(str_selcourse,conn);
myDa.SelectCommand.CommandType=CommandType.Text;
ds=new DataSet();
myDa.Fill(ds,"Course");
dg_course.DataSource=ds.Tables["Course"].DefaultView;
dg_course.DataKeyField="CourseId";
dg_course.DataBind();
}
private void BindClass()
{
string str_selclass="select * from Class";
myDa=new SqlDataAdapter(str_selclass,conn);
myDa.SelectCommand.CommandType=CommandType.Text;
ds=new DataSet();
myDa.Fill(ds,"Class");
dg_class.DataSource=ds.Tables["Class"].DefaultView;
dg_class.DataKeyField="classId";
dg_class.DataBind();
}
public void CheckAll(object sender,System.EventArgs e)
{
CheckBox chball=(CheckBox)sender;
if(chball.Text=="全选")
{
foreach(DataGridItem dg in this.dg_course.Items)
{
CheckBox chb=(CheckBox)dg.FindControl("chb");
chb.Checked=chball.Checked;
}
}
}
public void CheckClass(object sender,System.EventArgs e)
{
CheckBox chball=(CheckBox)sender;
if(chball.Text=="全选")
{
foreach(DataGridItem dg in this.dg_class.Items)
{
CheckBox chb=(CheckBox)dg.FindControl("chb2");
chb.Checked=chball.Checked;
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.cuvcourse.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.cuvcourse_ServerValidate);
this.btnCourse.Click += new System.EventHandler(this.btnCourse_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnCourse_Click(object sender, System.EventArgs e)
{//添加课程
if(this.IsValid)
{
this.ExecuteCourse();
this.BindCourse();
}
}
private void btnClass_Click(object sender, System.EventArgs e)
{//添加班级
if(this.IsValid)
{
this.ExecuteClass();
this.BindClass();
}
}
private void btndelete_Click(object sender, System.EventArgs e)
{
if(this.IsValid)
{
foreach(DataGridItem dg in this.dg_course.Items)
{
CheckBox chb=(CheckBox)dg.FindControl("chb");
if(chb.Checked)
{
string str_id=this.dg_course.DataKeys[dg.ItemIndex].ToString();//获得该行ID
int int_id=Convert.ToInt32(str_id);
string str_del="delete from Course where CourseId='"+int_id+"'";
data.ExecuteDel(str_del);
}
}
this.dg_course.CurrentPageIndex=0;
this.BindCourse();
}
}
private void btndelclass_Click(object sender, System.EventArgs e)
{
if(this.IsValid)
{
foreach(DataGridItem dg in this.dg_class.Items)
{
CheckBox chb=(CheckBox)dg.FindControl("chb2");
if(chb.Checked)
{
string str_id=this.dg_class.DataKeys[dg.ItemIndex].ToString();//获得该行ID
int int_id=Convert.ToInt32(str_id);
string str_del="delete from Class where ClassId='"+int_id+"'";
data.ExecuteDel(str_del);
}
}
this.dg_class.CurrentPageIndex=0;
this.BindCourse();
}
}
private void cuvcourse_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string str_course=args.Value;
string str_sel="select count(CourseName) from Course where CourseName='"+str_course+"'";
int count=data.ExecuteSel(str_sel);
if(count>0)//课程已存在
{
args.IsValid=false;
}
else
{
args.IsValid=true;
}
}
private void cuvnumber_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string str_numbere=args.Value;
string str_sel="select count(ClassNumber) from Class where CourseNumber='"+str_numbere+"'";
int count=data.ExecuteSel(str_sel);
if(count>0)//班级编号已存在
{
args.IsValid=false;
}
else
{
args.IsValid=true;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -