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

📄 personal.aspx.cs

📁 asp做的新闻系统
💻 CS
字号:
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Web.Personal
{
	public class Personal : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.ImageButton BtnQuery;
		protected System.Web.UI.WebControls.Label LabRowCount;
		protected System.Web.UI.WebControls.Label LabRowID;
		protected System.Web.UI.WebControls.Label LabID;
		
		private DataSet OleDs;
		private Web.Common.Common Cmon = new Common.Common();
		private Web.DataAccess.DataAccess Das = new DataAccess.DataAccess();

		protected System.Web.UI.WebControls.TextBox TxtState;
		protected System.Web.UI.WebControls.ImageButton IbtQuery;
		protected System.Web.UI.WebControls.Panel PalView;
		protected System.Web.UI.HtmlControls.HtmlTable Table4;
		protected System.Web.UI.WebControls.Panel PalArticle;
		protected System.Web.UI.WebControls.DataGrid GrdArticle;
		protected System.Web.UI.WebControls.Button BtnView;
		protected System.Web.UI.WebControls.Button BtnAdd;
		protected System.Web.UI.WebControls.Button BtnEdit;
		protected System.Web.UI.WebControls.Button BtnDel;
		protected System.Web.UI.WebControls.Button BtnPrevious;
		protected System.Web.UI.WebControls.Button BtnNext;
		protected System.Web.UI.WebControls.Button BtnSave;
		protected System.Web.UI.WebControls.Button BtnCancel;
		protected System.Web.UI.HtmlControls.HtmlTable Table1;
		protected System.Web.UI.WebControls.TextBox TxtUserName;
		protected System.Web.UI.WebControls.TextBox TxtPwd;
		protected System.Web.UI.WebControls.TextBox TxtVerifyPwd;
		protected System.Web.UI.WebControls.TextBox TxtTel;
		protected System.Web.UI.WebControls.TextBox TxtEMail;
		protected System.Web.UI.WebControls.TextBox TxtAddress;
				
		const int nGrdRows = 10;
		
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{ 
				BindGrdArticle();
				TxtState.Text = "0";

				PageInit();
			}

			GrdArticle.Attributes.Add("onclick","javascript:Click('GrdArticle');");
			GrdArticle.Attributes.Add("ondblclick","javascript:DbClick('GrdArticle');");
			BtnSave.Attributes.Add("OnClick","javascript:return CheckInput();");
			BtnDel.Attributes.Add("OnClick","javascript:return Del('GrdArticle');");
			BtnView.Attributes.Add("Onclick","javascript:return CheckSelect('GrdArticle','浏览');");
			BtnEdit.Attributes.Add("Onclick","javascript:return CheckSelect('GrdArticle','修改');");
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.BtnView.Click += new System.EventHandler(this.BtnView_Click);
			this.BtnAdd.Click += new System.EventHandler(this.BtnAdd_Click);
			this.BtnEdit.Click += new System.EventHandler(this.BtnEdit_Click);
			this.BtnDel.Click += new System.EventHandler(this.BtnDel_Click);
			this.BtnPrevious.Click += new System.EventHandler(this.BtnPrevious_Click);
			this.BtnNext.Click += new System.EventHandler(this.BtnNext_Click);
			this.BtnSave.Click += new System.EventHandler(this.BtnSave_Click);
			this.BtnCancel.Click += new System.EventHandler(this.BtnCancel_Click);
			this.GrdArticle.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.GrdArticle_PageIndexChanged);
			this.GrdArticle.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.GrdArticle_SortCommand);
			this.ID = "Form1";
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
		
		//初始化页面控件状态
		//LabState:操作状态,0--查询,1--浏览,2--增加,3--修改,4--删除
		private void PageInit()
		{
			int nRow;
			int nState = int.Parse(TxtState.Text);

			PalView.Visible = (nState != 0) ? true:false;
			PalArticle.Visible = (nState == 0) ? true:false;

			TxtUserName.Enabled = (nState == 2) ? true:false;
			TxtPwd.Enabled = (nState != 1) ? true:false;
			TxtVerifyPwd.Enabled = (nState != 1) ? true:false;
			TxtTel.Enabled = (nState != 1) ? true:false;
			TxtEMail.Enabled = (nState != 1) ? true:false;
			TxtAddress.Enabled = (nState != 1) ? true:false;

			BtnView.Enabled = (nState == 0) ? true:false;
			BtnAdd.Enabled = (nState == 0 || nState == 1) ? true:false;
			BtnEdit.Enabled = (nState == 0 || nState == 1) ? true:false;
			BtnDel.Enabled = (nState == 0 || nState == 1) ? true:false;
			BtnPrevious.Enabled = (nState == 1) ? true:false;
			BtnNext.Enabled = (nState == 1) ? true:false;
			BtnSave.Enabled = (nState == 2 || nState == 3) ? true:false;
			BtnCancel.Enabled = (nState == 1 || nState == 2 || nState == 3) ? true:false;

			if(nState == 1)
			{
				nRow = GetRow();				
				BtnPrevious.Enabled = (int.Parse(LabRowCount.Text)>=1 && nRow>1)?true:false;
				BtnNext.Enabled = (int.Parse(LabRowCount.Text)>=1 && nRow<int.Parse(LabRowCount.Text))?true:false;
			}
		}
		
		//刷新GrdArticle
		private void BindGrdArticle()
		{

			OleDs = new DataSet();

			OleDs=Das.GetDataSet("select * from users");
			GrdArticle.DataSource=OleDs;
			
			int nPage=0;
			nPage = GrdArticle.CurrentPageIndex;
			GrdArticle.CurrentPageIndex =0;
			GrdArticle.DataBind();

			if ((nPage >= GrdArticle.PageCount) && (GrdArticle.PageCount > 0))
			{
				nPage = GrdArticle.PageCount - 1;
			}
			
			LabRowCount.Text=OleDs.Tables[0].Rows.Count.ToString();

			GrdArticle.CurrentPageIndex = nPage;
			GrdArticle.DataBind();
		}

		private void BtnAdd_Click(object sender, System.EventArgs e)
		{
			TxtState.Text = "2";
			PageInit();
			Clear();
		}

		private void Clear()
		{
			TxtUserName.Text = "";
			TxtPwd.Text = "";
			TxtVerifyPwd.Text = "";
			TxtTel.Text = "";
			TxtEMail.Text = "";
			TxtAddress.Text = "";
		}

		private void BtnCancel_Click(object sender, System.EventArgs e)
		{
			TxtState.Text = "0";
			PageInit();
			Clear();
		}

		//保存信息
		private void BtnSave_Click(object sender, System.EventArgs e)
		{
			string strUserName,strPwd,strTel,strEMail,strAddress,strState,strSql;
			
			strUserName = TxtUserName.Text.Trim();
			strPwd = TxtPwd.Text.Trim();
			strTel = TxtTel.Text.Trim();
			strEMail = TxtEMail.Text.Trim();
			strAddress = TxtAddress.Text.Trim();
			strState = TxtState.Text;
			strSql = "";
	
			if(strState == "2")
			{
				if(Cmon.IdIfExist(TxtUserName.Text.Trim(),"username","users"))
					Response.Write("<script language='javascript'>alert('此用户名已经存在,请重新输入!');</script>");
				else
					strSql = "insert into users(username,pwd,tel,email,address) values('" 
						+ strUserName + "','"
						+ strPwd + "','"
						+ strTel + "','"
						+ strEMail + "','"
						+ strAddress + "')";
			}
			else
				strSql = "update users set " 
					+ " pwd = '" + strPwd + "',"
					+ " tel = '" + strTel + "',"
					+ " email = '" + strEMail + "',"
					+ " address = '" + strAddress + "'"
					+ " where username ='" + strUserName + "'";

			if(Das.ExecSql(strSql))
			{
				Response.Write("<script language='javascript'>alert('保存成功!');</script>");
				
				if(strState == "3")
					TxtState.Text= "0";
				else
					Clear();
				PageInit();
				BindGrdArticle();
			}
			else
			{
				Response.Write("<script language='javascript'>alert('保存不成功,请稍后再试!');</script>");
			}
		}

		//浏览信息
		private void BtnView_Click(object sender, System.EventArgs e)
		{
			int nRow = CheckCbx();
			string strTableID = LabID.Text;

			if (nRow == -1)
				Response.Write("<script>alert('请先选择选择一条记录!');</script>");
			else
				if (strTableID.IndexOf(",") >0)
				Response.Write("<script>alert('浏览只能选择一条记录!');</script>");
			else
			{	
				DispDetail(nRow);
				TxtState.Text = "1";
				PageInit();
			}
		}

		//检查用户选择DataGrid中记录的总数,返回DG中单选的记录号,多选的记录号保存LabDeptID中。
		private int CheckCbx()
		{
			int nRow = -1;
			LabID.Text = "";
			for (int i = 0;i < GrdArticle.Items.Count;i++)
			{
				CheckBox CbxTable = (CheckBox)GrdArticle.Items[i].FindControl("CbxSelect");
				if(CbxTable.Checked == true)
				{
					if(LabID.Text == "")
					{
						LabID.Text = GrdArticle.Items[i].Cells[1].Text.Trim();
						nRow = i;
					}
					else
					{
						LabID.Text = LabID.Text + "," + GrdArticle.Items[i].Cells[1].Text.Trim();
					}
				}
			}
			return nRow;
		}

		//显示明细数据
		private void DispDetail(int nRow)
		{
			TxtUserName.Text = GrdArticle.Items[nRow].Cells[1].Text;
			TxtTel.Text = GrdArticle.Items[nRow].Cells[2].Text;
			TxtEMail.Text = GrdArticle.Items[nRow].Cells[3].Text;
			TxtAddress.Text = GrdArticle.Items[nRow].Cells[4].Text;
		}

		private void BtnNext_Click(object sender, System.EventArgs e)
		{
			Next();
		}


		//记录向前移动一条
		private void Previous()
		{
			int nPage,nRow;

			nPage = GrdArticle.CurrentPageIndex;

			//当前选中记录行号
			nRow = CheckCbx();

			//是否为当前页的第一条记录
			if(nRow == 0)
			{
				GrdArticle.CurrentPageIndex = nPage-1;
				BindGrdArticle();
				SetCbxState(nGrdRows-1,true);
				DispDetail(nGrdRows-1);
			}
			else
			{
				SetCbxState(nRow,false);
				SetCbxState(nRow-1,true);
				DispDetail(nRow-1);
			}
			PageInit();
		}
		
		//记录向下移动一条
		private void Next()
		{
			int nPage,nRow;

			nPage = GrdArticle.CurrentPageIndex;

			//当前选中记录行号
			nRow = CheckCbx();

			//是否为当前页的最后一条记录
			if(nRow == (nGrdRows-1))
			{
				GrdArticle.CurrentPageIndex = nPage+1;
				BindGrdArticle();
				SetCbxState(0,true);
				DispDetail(0);
			}
			else
			{
				SetCbxState(nRow,false);
				SetCbxState(nRow+1,true);
				DispDetail(nRow+1);
			}
			PageInit();
		}

		//设置复选框状态。
		private void SetCbxState(int nRow,bool bState)
		{
			CheckBox CbxDept;
			CbxDept = (CheckBox)GrdArticle.Items[nRow].FindControl("CbxSelect");
			CbxDept.Checked = bState;
		}

		private void BtnPrevious_Click(object sender, System.EventArgs e)
		{
			Previous();
		}

		//返回当前选中的记录行数。
		private int GetRow()
		{
			int nRow,nPage;
			nPage = GrdArticle.CurrentPageIndex;
			nRow = nPage * nGrdRows + CheckCbx() + 1;
			return nRow;
		}
 
		//换页
		private void GrdArticle_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			GrdArticle.CurrentPageIndex = e.NewPageIndex;
			BindGrdArticle();
		}

		//排序
		private void GrdArticle_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			DataView dv = new DataView();
			BindGrdArticle();
			dv.Table = OleDs.Tables[0];
			dv.Sort = e.SortExpression;
			GrdArticle.DataSource = dv;
			GrdArticle.DataBind();
			GrdArticle.EditItemIndex = -1;
			GrdArticle.SelectedIndex = 0;
		}

		//修改信息
		private void BtnEdit_Click(object sender, System.EventArgs e)
		{
			int nRow = CheckCbx();
			string strTableID = LabID.Text;

			if (nRow == -1)
				Response.Write("<script>alert('请先选择选择一条记录!');</script>");
			else
				if (strTableID.IndexOf(",") >0)
				Response.Write("<script>alert('修改只能选择一条记录!');</script>");
			else
			{	
				DispDetail(nRow);
				TxtState.Text = "3";
				PageInit();
			}
		}

		//删除信息
		private void BtnDel_Click(object sender, System.EventArgs e)
		{
			int nRow = 0,nNewRow = 0,nRowCount,nPage;
			string strTableID,strSql;

			nRow = CheckCbx();
			strTableID = LabID.Text.Trim();

			strSql = "delete from article where id in(" + strTableID + ")";

			if(TxtState.Text == "1")
			{
				//当前选中记录行号				
				SetCbxState(nRow,true);

				//删除执行前,判断当前记录号是否第一条,最后一条。i=0||i=max||i=orther
				nRowCount = int.Parse(LabRowCount.Text);
				nPage = GrdArticle.CurrentPageIndex;
				if(nRow == 0 && nRowCount-1 == 0)
					nNewRow = -1;
				else
				{
					//是否删除最末一条记录
					if(((nPage * nGrdRows) + nRow) == nRowCount-1)
					{
						Previous();	//上移一条记录
						if(nRow == 0)	//是否为当前页的首记录
							nNewRow = nGrdRows-1;	//上一页的尾记录
						else
							nNewRow = nRow-1;
					}
					else 
					{
						Next();	//下移一条记录
						nNewRow = nRow;
					}
				}
			}

			if (Das.ExecSql(strSql))
			{
				Response.Write("<script>alert('删除成功!');</script>");
				Clear();
				BindGrdArticle();

				//重新选中要浏览的记录
				if(TxtState.Text == "1")
				{
					if(nNewRow != -1)
					{
						SetCbxState(nNewRow,true);
						DispDetail(nNewRow);
					}
					else
					{
						TxtState.Text = "0";
					}
					PageInit();
				}
			}
			else
			{
				Response.Write("<script>alert('删除不成功,请稍后再试!');</script>");
				if(TxtState.Text=="1")
				{
					PageInit();
				}
			}
		}
	}
}

⌨️ 快捷键说明

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