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

📄 stuedit.aspx.cs

📁 用vs2005和sql2000完成的学生信息管理系统
💻 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 stu
{
	/// <summary>
	/// stuedit 的摘要说明。
	/// </summary>
	public class stuedit : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label GridForm_Title;
		protected System.Web.UI.WebControls.Label stu_ValidationSummary;
		protected System.Web.UI.WebControls.RequiredFieldValidator stu_name_Validator_Req;
		protected System.Web.UI.WebControls.TextBox stu_name;
		protected System.Web.UI.WebControls.RequiredFieldValidator stu_sex_Validator_Req;
		protected System.Web.UI.WebControls.TextBox stu_sex;
		protected System.Web.UI.WebControls.TextBox stu_age;
		protected System.Web.UI.WebControls.RequiredFieldValidator stu_business_Validator_Req;
		protected System.Web.UI.WebControls.TextBox stu_business;
		protected System.Web.UI.WebControls.RequiredFieldValidator stu_student_login_Validator_Req;
		protected System.Web.UI.WebControls.TextBox stu_student_login;
		protected System.Web.UI.WebControls.RequiredFieldValidator stu_student_password_Validator_Req;
		protected System.Web.UI.WebControls.TextBox stu_student_password;
		protected System.Web.UI.WebControls.RequiredFieldValidator stu_student_level_Validator_Req;
		protected System.Web.UI.WebControls.CustomValidator stu_student_level_Validator_Num;
		protected System.Web.UI.WebControls.DropDownList stu_student_level;
		protected System.Web.UI.WebControls.RequiredFieldValidator stu_class_id_Validator_Req;
		protected System.Web.UI.WebControls.CustomValidator stu_class_id_Validator_Num;
		protected System.Web.UI.WebControls.DropDownList stu_class_id;
		protected System.Web.UI.WebControls.TextBox stu_address;
		protected System.Web.UI.WebControls.TextBox stu_email;
		protected System.Web.UI.WebControls.CheckBox stu_super;
		protected System.Web.UI.HtmlControls.HtmlInputHidden p_stu_student_id;
		protected System.Web.UI.HtmlControls.HtmlInputHidden stu_student_id;
		protected System.Web.UI.HtmlControls.HtmlInputButton stu_insert;
		protected System.Web.UI.HtmlControls.HtmlInputButton stu_update;
		protected System.Web.UI.HtmlControls.HtmlInputButton stu_delete;
		protected System.Web.UI.WebControls.RequiredFieldValidator stu_time_Validator_Req;
		protected System.Web.UI.WebControls.RequiredFieldValidator stu_age_Validator_Req;
		protected System.Web.UI.WebControls.TextBox Textbox1;
		protected System.Web.UI.WebControls.TextBox stu_work_ph;
		protected System.Web.UI.WebControls.TextBox picture;
		protected System.Web.UI.HtmlControls.HtmlInputButton stu_cancel;
		protected stu Utility;
		protected string stu_FormAction="student.aspx?";	
		protected String[] stu_student_level_lov = "0;None;3;Admin".Split(new Char[] {';'});
	
		public stuedit()
		{
			this.Init += new System.EventHandler(Page_Init);
		}
		public void ValidateNumeric(object source, ServerValidateEventArgs args) 
		{
			try
			{
				Decimal temp=Decimal.Parse(args.Value);
				args.IsValid=true;
			}
			catch
			{
				args.IsValid=false;	}
		}
		//定义登录窗口显示控件过程
		//初始化页面过程,创建一个Utility实例,并调用其相应的各方法
		protected void Page_Load(object sender, EventArgs e)
		{	
			Utility=new stu(this);
			Utility.CheckSecurity(3);
			// 完成窗口安全验证
			if (!IsPostBack)
			{
				p_stu_student_id.Value = Utility.GetParam("student_id");Page_Show(sender, e);
			}
		}
		//页面关闭过程
		protected void Page_Unload(object sender, EventArgs e)
		{
			if(Utility!=null) Utility.DBClose();
		}
		//窗口中控件定义事件处理过程
		protected void Page_Init(object sender, EventArgs e)
		{
			InitializeComponent();
		}
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.Unload += new System.EventHandler(this.Page_Unload);
			stu_insert.ServerClick += new System.EventHandler (this.stu_Action);
			stu_update.ServerClick += new System.EventHandler (this.stu_Action);
			stu_delete.ServerClick += new System.EventHandler (this.stu_Action);
			stu_cancel.ServerClick += new System.EventHandler (this.stu_Action);		
		}
		//定义整体显示页面过程
		protected void Page_Show(object sender, EventArgs e)
		{
			emps_Show();		
		}		
		//定义控制函数,控制按钮的的显示与否		
		private bool emps_Validate()
		{
			bool result=true;
			stu_ValidationSummary.Text="";

			for(int i=0;i<Page.Validators.Count;i++)
			{
				if(((System.Web.UI.WebControls.BaseValidator)Page.Validators[i]).ID.ToString().StartsWith("stu"))
				{
					if(!Page.Validators[i].IsValid)
					{
						stu_ValidationSummary.Text+=Page.Validators[i].ErrorMessage+"<br>";
						result=false;
					}
				}
			}
			stu_ValidationSummary.Visible=(!result);
			return result;
		}
		//更新记录显示过程
		void emps_Show() 
		{
	
			Utility.buildListBox(stu_student_level.Items,stu_student_level_lov,null,"");
			Utility.buildListBox(stu_class_id.Items,"select class_id,class_name from class order by 2","class_id","class_name",null,"");
			bool ActionInsert=true;	
			if (p_stu_student_id.Value.Length > 0 ) 
			{
				string sWhere = "";
				sWhere += "stu_id=" + stu.ToSQL(p_stu_student_id.Value, stu.FIELD_TYPE_Number);
				string sSQL = "select * from stu where " + sWhere;
				SqlDataAdapter dsCommand = new SqlDataAdapter(sSQL, Utility.Connection);
				DataSet ds = new DataSet();
				DataRow row;
				if (dsCommand.Fill(ds, 0, 1, "stu") > 0) 
				{
					row = ds.Tables[0].Rows[0];
					stu_student_id.Value = stu.GetValue(row, "stu_id");
					stu_name.Text = stu.GetValue(row, "name");
					stu_sex.Text = stu.GetValue(row, "sex");
					stu_business.Text = stu.GetValue(row, "business");
					stu_student_login.Text = stu.GetValue(row, "login");
					stu_student_password.Text = stu.GetValue(row, "password");
				{
					string s;
					s=stu.GetValue(row, "stu_level");
					try 
					{
						stu_student_level.SelectedIndex=stu_student_level.Items.IndexOf(stu_student_level.Items.FindByValue(s));
					}
					catch{}
				}
				{
					string s;
					s=stu.GetValue(row, "class_id");
		
					try 
					{
						stu_class_id.SelectedIndex=stu_class_id.Items.IndexOf(stu_class_id.Items.FindByValue(s));
					}
					catch{}
				}
		
					stu_address.Text = stu.GetValue(row, "address");
					stu_email.Text = stu.GetValue(row, "email");
					stu_work_ph.Text = stu.GetValue(row, "ph");
					picture.Text = stu.GetValue(row, "picture");
					Textbox1.Text = stu.GetValue(row, "sch_time");
					stu_super.Checked=stu.GetValue(row, "super").ToLower().Equals("1".ToLower());
					stu_age.Text = stu.GetValue(row, "bth");
					stu_insert.Visible=false;
					ActionInsert=false;
				}
			}
			if(ActionInsert)
			{
				String pValue;
				pValue = Utility.GetParam("stu_id");stu_student_id.Value = pValue;
				stu_delete.Visible=false;
				stu_update.Visible=false;
			}
		}
		//插入新记录事件代码
		bool stu_insert_Click(Object Src, EventArgs E) 
		{
			string sSQL="";
			bool bResult=emps_Validate();
		{
			int iCount = Utility.DlookupInt("stu", "count(*)", "login=" + stu.ToSQL(Utility.GetParam("stu_student_login"), stu.FIELD_TYPE_Text));
			if (iCount!=0)
			{
				stu_ValidationSummary.Visible=true;
				stu_ValidationSummary.Text+="The value in field Login is already in database."+"<br>";
				bResult=false;}
		}
			if(bResult)
			{			        
				string zh_id="";
				string sql;	
				string zh_name=stu.ToSQL(Utility.GetParam("stu_name"), stu.FIELD_TYPE_Text) ;
				string zh_sex=stu.ToSQL(Utility.GetParam("stu_sex"), stu.FIELD_TYPE_Text) ;
				string zh_age=stu.ToSQL(Utility.GetParam("stu_age"), stu.FIELD_TYPE_Text) ;
				string zh_business=stu.ToSQL(Utility.GetParam("stu_business"), stu.FIELD_TYPE_Text) ;
				string zh_login=stu.ToSQL(Utility.GetParam("stu_student_login"), stu.FIELD_TYPE_Text) ;
				string zh_password=stu.ToSQL(Utility.GetParam("stu_student_password"), stu.FIELD_TYPE_Text) ;
				string zh_level=stu.ToSQL(Utility.GetParam("stu_student_level"), stu.FIELD_TYPE_Number) ;
				string zh_class_id=stu.ToSQL(Utility.GetParam("stu_class_id"), stu.FIELD_TYPE_Number) ;
				string zh_address=stu.ToSQL(Utility.GetParam("stu_address"), stu.FIELD_TYPE_Text) ;
				string zh_email=stu.ToSQL(Utility.GetParam("stu_email"), stu.FIELD_TYPE_Text) ;
				string zh_ph=stu.ToSQL(Utility.GetParam("stu_work_ph"), stu.FIELD_TYPE_Text) ;
				string zh_picture=stu.ToSQL(Utility.GetParam("picture"), stu.FIELD_TYPE_Text) ;
				string zh_time=stu.ToSQL(Utility.GetParam("Textbox1"), stu.FIELD_TYPE_Text) ;
				string zh_super=stu.getCheckBoxValue(Utility.GetParam("stu_super"), "1", "0", stu.FIELD_TYPE_Number) ;				
				
				sSQL= "insert into stu (name,bth,sex,login,password,stu_level,business,class_id,address,"+
					"email,ph,picture,sch_time,super) values (" +									
					zh_name + "," + 
					zh_age + "," + 
					zh_sex + "," + 
					zh_login + "," + 
					zh_password + "," + 
					zh_level + "," + 
					zh_business + "," + 
					zh_class_id + "," + 
					zh_address + "," + 
					zh_email + "," + 
					zh_ph + "," + 
					zh_picture + "," + 
					zh_time + "," + 
					zh_super + ")";
				
				SqlCommand cmd = new SqlCommand(sSQL, Utility.Connection);
				try 
				{
					cmd.ExecuteNonQuery();
				} 
				catch(Exception e) 
				{
					stu_ValidationSummary.Text += e.Message;
					stu_ValidationSummary.Visible = true;
					return false;
				}
			}
			return bResult;
		}
		//更新记录事件代码
		bool stu_update_Click(Object Src, EventArgs E) 
		{
			string sWhere = "";
			string sSQL ="";	
			bool bResult=emps_Validate();
			if(bResult)
			{
				if (p_stu_student_id.Value.Length > 0) 
				{
					sWhere = sWhere + "stu_id=" + stu.ToSQL(p_stu_student_id.Value, stu.FIELD_TYPE_Number);
				}
			else
			{
				int iCount = Utility.DlookupInt("stu", "count(*)", "login=" + stu.ToSQL(Utility.GetParam("stu_student_login"), stu.FIELD_TYPE_Text) + " and not(" + sWhere + ")");
				if (iCount!=0)
				{
					stu_ValidationSummary.Visible=true;
					stu_ValidationSummary.Text+="The value in field Login is already in database."+"<br>";
					bResult=false;}
			}
				if (bResult)
				{
		
					sSQL = "update stu set " +
						"[name]=" +stu.ToSQL(Utility.GetParam("stu_name"),stu.FIELD_TYPE_Text)  +
						",[sex]=" +stu.ToSQL(Utility.GetParam("stu_business"),stu.FIELD_TYPE_Text)  +
						",[login]=" +stu.ToSQL(Utility.GetParam("stu_student_login"),stu.FIELD_TYPE_Text)  +
						",[password]=" +stu.ToSQL(Utility.GetParam("stu_student_password"),stu.FIELD_TYPE_Text)  +
						",[stu_level]=" +stu.ToSQL(Utility.GetParam("stu_student_level"),stu.FIELD_TYPE_Number)  +
						",[class_id]=" +stu.ToSQL(Utility.GetParam("stu_class_id"),stu.FIELD_TYPE_Number)  +
						",[address]=" +stu.ToSQL(Utility.GetParam("stu_address"),stu.FIELD_TYPE_Text)  +
						",[email]=" +stu.ToSQL(Utility.GetParam("stu_email"),stu.FIELD_TYPE_Text)  +
						",[ph]=" +stu.ToSQL(Utility.GetParam("stu_work_ph"),stu.FIELD_TYPE_Text)  +
						",[picture]=" +stu.ToSQL(Utility.GetParam("picture"),stu.FIELD_TYPE_Text)  +
						",[sch_time]=" +stu.ToSQL(Utility.GetParam("Textbox1"),stu.FIELD_TYPE_Text)  +
						",[super]=" +stu.getCheckBoxValue(Utility.GetParam("stu_super"),"1","0",stu.FIELD_TYPE_Number)  +
						",[bth]=" +stu.ToSQL(Utility.GetParam("stu_age"),stu.FIELD_TYPE_Text) +
						",[business]=" +stu.ToSQL(Utility.GetParam("stu_business"),stu.FIELD_TYPE_Text) ;		
					sSQL = sSQL + " where " + sWhere;
					//创建与数据库联结的命令集
					SqlCommand cmd = new SqlCommand(sSQL, Utility.Connection);
					try 
					{
						cmd.ExecuteNonQuery();
					} 
					catch(Exception e) 
					{
						stu_ValidationSummary.Text += e.Message;
						stu_ValidationSummary.Visible = true;
						return false;
					}
				}
			}
			return bResult;
		}

		//删除记录代码
		bool stu_delete_Click(Object Src, EventArgs E) 
		{
			string sWhere = "";
			if (p_stu_student_id.Value.Length > 0) 
			{
				sWhere += "stu_id=" + stu.ToSQL(p_stu_student_id.Value, stu.FIELD_TYPE_Number);
			}
			string sSQL = "delete from stu where " + sWhere;
			SqlCommand cmd = new SqlCommand(sSQL, Utility.Connection);
			try 
			{
				cmd.ExecuteNonQuery();
			} 
			catch(Exception e) 
			{
				stu_ValidationSummary.Text += e.Message;
				stu_ValidationSummary.Visible = true;
				return false;
			}
			return true;
		}
		//取消更新代码
		bool stu_cancel_Click(Object Src, EventArgs E) 
		{
			return true;
		}
		//为各按钮点击事件参数函数代码
		void stu_Action(Object Src, EventArgs E) 
		{
			bool bResult=true;
			if(((HtmlInputButton)Src).ID.IndexOf("insert")>0) bResult=stu_insert_Click(Src,E);
			if(((HtmlInputButton)Src).ID.IndexOf("update")>0) bResult=stu_update_Click(Src,E);
			if(((HtmlInputButton)Src).ID.IndexOf("delete")>0) bResult=stu_delete_Click(Src,E);
			if(((HtmlInputButton)Src).ID.IndexOf("cancel")>0) bResult=stu_cancel_Click(Src,E);
			if(bResult)Response.Redirect(stu_FormAction+"");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -