⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studentmanage.aspx.cs

📁 yi ge xue sheng cheng ji guan li xi tong de yuan ma .
💻 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>
	/// studentmanage 的摘要说明。
	/// </summary>
	public class studentmanage : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox txtnumber;
		protected System.Web.UI.WebControls.TextBox txtname;
		protected System.Web.UI.WebControls.TextBox txtpwd;
		protected System.Web.UI.WebControls.TextBox txtpwdRepeat;
		protected System.Web.UI.WebControls.Button btnsubmit;
		protected System.Web.UI.WebControls.Button btnclear;
		protected SqlConnection conn;
		protected SqlCommand myCmd;
		protected SqlDataAdapter myDa;
		protected System.Web.UI.WebControls.Button btndelete;
		protected System.Web.UI.WebControls.DataGrid dg_student;
		protected System.Web.UI.WebControls.CompareValidator cvpwd;
		protected System.Web.UI.WebControls.RegularExpressionValidator rev;
		protected System.Web.UI.WebControls.ValidationSummary vsShow;
		protected System.Web.UI.WebControls.RegularExpressionValidator revpwd;
		protected System.Web.UI.WebControls.DropDownList ddlclass;
		protected System.Web.UI.WebControls.CustomValidator cuvnumber;
		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();
				ds=new DataSet();
				if(!Page.IsPostBack)
				{
					this.bindDdl();
					this.BindDg();
					this.btndelete.Attributes.Add("onclick","if(confirm('你确定要删除吗?'))return true;else return false;");
				}
			}
		}
		private void ExecuteInsert()
		{
			string str_number=this.txtnumber.Text.Trim().Replace("'","''");
			string str_name=this.txtname.Text.Trim().Replace("'","''");
			string str_pwd=this.txtpwd.Text.Trim().Replace("'","''");
			string str_class=this.ddlclass.SelectedValue.ToString();
			string str_pwdMD5=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str_pwd,"MD5").ToString();
			conn=connDB.createConn();
			conn.Open();
			myCmd=new SqlCommand("procStudent",conn);
			myCmd.CommandType=CommandType.StoredProcedure;
			myCmd.Parameters.Add("@Anumber",SqlDbType.Int);
			myCmd.Parameters.Add("@Aname",SqlDbType.VarChar,50);
			myCmd.Parameters.Add("@Apwd",SqlDbType.VarChar,50);
			myCmd.Parameters.Add("@AClassNumber",SqlDbType.VarChar,50);
			myCmd.Parameters["@Anumber"].Value=Convert.ToInt32(str_number);
			myCmd.Parameters["@Aname"].Value=str_name;
			myCmd.Parameters["@Apwd"].Value=str_pwdMD5;
			myCmd.Parameters["@AClassNumber"].Value=str_class;
			myCmd.ExecuteNonQuery();
			conn.Close();
			string Dhtmlalert="";
			Dhtmlalert += "<script language=javascript>";
			Dhtmlalert += "alert('添加成功!');";
			Dhtmlalert += "</script>";
			Page.RegisterClientScriptBlock("alert_news",Dhtmlalert);
		}
		private void bindDdl()
		{
			string str_sel="select ClassNumber from Class";
			myDa=new SqlDataAdapter(str_sel,conn);
			myDa.Fill(ds,"Class");
			ddlclass.DataSource=ds.Tables["Class"].DefaultView;
			ddlclass.DataTextField="ClassNumber";
			ddlclass.DataBind();
		}
		private void BindDg()
		{			
			myDa=new SqlDataAdapter("procSelStudent",conn);
			myDa.SelectCommand.CommandType=CommandType.StoredProcedure;		
			myDa.Fill(ds,"Student");
			dg_student.DataSource=ds.Tables["Student"].DefaultView;
			dg_student.DataKeyField="StudentId";
			dg_student.DataBind();
		}
		public void CheckAll(object sender,System.EventArgs e)
		{
			CheckBox chball=(CheckBox)sender;
			if(chball.Text=="全选")
			{
				foreach(DataGridItem dg in this.dg_student.Items)
				{
					CheckBox chb=(CheckBox)dg.FindControl("chb");
					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.cuvnumber.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.cuvnumber_ServerValidate);
			this.btnsubmit.Click += new System.EventHandler(this.btnsubmit_Click);
			this.btnclear.Click += new System.EventHandler(this.btnclear_Click);
			this.btndelete.Click += new System.EventHandler(this.btndelete_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnsubmit_Click(object sender, System.EventArgs e)
		{
			if(this.IsValid)
			{
				this.ExecuteInsert();
				this.BindDg();
			}
		}

		private void btnclear_Click(object sender, System.EventArgs e)
		{
			if(this.IsValid)
			{
				this.txtnumber.Text="";
				this.txtname.Text="";
				this.txtpwd.Text="";
				this.txtpwdRepeat.Text="";
			}
		}

		private void btndelete_Click(object sender, System.EventArgs e)
		{
			if(this.IsValid)
			{
				foreach(DataGridItem dg in this.dg_student.Items)
				{
					CheckBox chb=(CheckBox)dg.FindControl("chb");
					if(chb.Checked)
					{
						string str_id=this.dg_student.DataKeys[dg.ItemIndex].ToString();//获得该行ID
						int int_id=Convert.ToInt32(str_id);
						string str_del="delete from Student where StudentId='"+int_id+"'";//删除学生表中记录
						string str_delR="delete from Results where Snumber=(select Snumber from Student where StudentId='"+int_id+"')";//删除成绩表中记录
						data.ExecuteDel(str_delR);
						data.ExecuteDel(str_del);
					}
				}
				this.dg_student.CurrentPageIndex=0;
				this.BindDg();
			}
		}

		private void cuvnumber_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			string str_number=args.Value;
			string str_sel="select count(Snumber) from Student where Snumber='"+str_number+"'";
			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 + -