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

📄 admin.aspx.cs

📁 功能介绍: 支持站长管理(删除、修改、回复) 支持邮件发送 支持数据库加密码 留言含邮箱、主页、QQ、MSN、来自、邮件提示、IP获取、回复等功能 初始用户:admin 密码:admin
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient ;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Gbook
{
	/// <summary>
	/// Admin 的摘要说明。
	/// </summary>
	public class Admin : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.CheckBox ckbAll;
		protected System.Web.UI.WebControls.Button btnLogout;
		protected System.Web.UI.WebControls.Button btnDel;
		protected System.Web.UI.WebControls.Label lblMessage;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		private Gbook.Conn myConn			= new Gbook.Conn();			// [打开数据库]

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(Request.Cookies["MyCookies"] == null) 
			{
				Response.Redirect("Login.aspx");
			}
			else
			{
				if(!Page.IsPostBack) 
				{
					BindDataGrid(0);
					btnDel.Attributes.Add ("onclick","return confirm (\"确定要删除这些留言记录吗?\");");
					btnLogout.Attributes.Add ("onclick","return confirm (\"您真的要退出管理吗?\");");
				}

			}
		}

		/// <summary>
		/// 获取数据库连接
		/// </summary>
		private string strConn
		{
			get	{	return myConn.ConnString();}
		}

		private void BindDataGrid(int intPageNumber) 
		{
			// -----------	数据库连接	-----------
			SqlConnection myConnection = new SqlConnection(strConn);
			myConnection.Open();
			string SqlCmd = "Select * From GuestBook_Message Order By GM_ID Desc";
			SqlDataAdapter DataAdapter1 = new SqlDataAdapter(SqlCmd, myConnection);
			DataSet ds  = new DataSet();
			DataAdapter1.Fill(ds,"GBookList");
			try 
			{
				DataTable GBookList = ds.Tables["GBookList"];
				DataGrid1.DataSource = GBookList;
				/*-----------------------------------------
				* 翻页的时候要设置的就是下面这个属性
				* ----------------------------------------*/
				DataGrid1.CurrentPageIndex = intPageNumber;
				DataGrid1.DataBind();
				for(int i=0;i<DataGrid1.Items.Count;i++)
				{
					string strTmpTitle	= "";
					string strEmail		= GBookList.Rows[i]["GM_EMAIL"].ToString().Trim();
					string strHomePage	= GBookList.Rows[i]["GM_HOMEPAGE"].ToString().Trim();
					string strArea		= GBookList.Rows[i]["GM_AREA"].ToString().Trim();
					string strOICQ		= GBookList.Rows[i]["GM_OICQ"].ToString().Trim();
					string strMsn		= GBookList.Rows[i]["GM_MSN"].ToString().Trim();
					string strReTime	= GBookList.Rows[i]["GM_RETIME"].ToString().Trim();
					string strReply		= GBookList.Rows[i]["GM_REPLY"].ToString().Trim();
					if (strEmail!="")
						strTmpTitle		= "<a title='邮箱:"+ strEmail +"' href='mailto:"+ strEmail +"' class='char1'><font class='size'>Email</font></a>&nbsp;";
					if (strHomePage!="")
						strTmpTitle		+= "<a title='主页:"+ strHomePage +"' href='"+ strHomePage +"' class='char1' target='_blank'><font class='size'>Homepage</font></a>&nbsp;";
					if (strArea!="")
						strTmpTitle		+= "<a title='来自"+ strArea +"' href='javascript:来自"+ strArea +"' onclick='return false;' class='char1'><font class='size'>From</font></a>&nbsp;";
					if (strOICQ!="")
						strTmpTitle		+= "<a title='OICQ:"+ strOICQ +"' href='http://search.tencent.com/cgi-bin/friend/user_show_info?ln="+ strOICQ +"' class='char1' target='_blank'><font class='size'>Oicq</font></a>&nbsp;";
					if (strMsn!="")
						strTmpTitle		+= "<a title='MSN:"+ strMsn +"' href='mailto:"+ strMsn +"' class='char1'><font class='size'>MSN</font></a>&nbsp;";
					if (strReply!="") 
					{
						((System.Web.UI.HtmlControls.HtmlTableRow)DataGrid1.Items[i].FindControl("ItemTr1")).Visible = true;
						((System.Web.UI.HtmlControls.HtmlTableRow)DataGrid1.Items[i].FindControl("ItemTr2")).Visible = true;
					}
					if (strReply!="") 
					{
						((Label)DataGrid1.Items[i].FindControl("ReplyTime")).Visible = true;
						((Label)DataGrid1.Items[i].FindControl("ReplyTime")).Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Retime:</b>" + strReTime;
					}
					((Label)DataGrid1.Items[i].FindControl("ItemTitle")).Text = strTmpTitle;
				}
			}
			catch (SqlException ex)
			{
				lblMessage.Visible	= true;
				lblMessage.Text		= ex.Message;
			}
			finally
			{
				ds.Dispose();
				ds.Clear();
				DataAdapter1.Dispose();
				myConnection.Close();
			}
		}



		private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			// -----------	数据库连接	-----------
			SqlConnection myConnection = new SqlConnection(strConn);
			int index = e.Item.ItemIndex;
			int GM_ID = Convert.ToInt32(DataGrid1.DataKeys[index].ToString());
			string SqlCmd  = "Delete From GuestBook_Message Where GM_ID	='" + GM_ID	+ "'";
		
			SqlCommand myCommand = new SqlCommand(SqlCmd, myConnection);
			myConnection.Open();
			try
			{
				myCommand.ExecuteNonQuery();
				BindDataGrid(0);
			}
			catch(SqlException ex)
			{	
				lblMessage.Visible  = true;
				lblMessage.Text		= ex.Message;
			}
		}

		private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			ListItemType itemType = e.Item.ItemType;
			if (e.Item.ItemType == ListItemType.Item ||
				e.Item.ItemType == ListItemType.AlternatingItem) 
			{
				((LinkButton)e.Item.Cells[0].FindControl("deleButton")).Attributes.Add ("onclick",
					"return confirm (\"确定要删除此留言记录吗?\");");
			}
		}

		private void ChangePage(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			BindDataGrid(e.NewPageIndex);
		}
		
		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.ckbAll.CheckedChanged += new System.EventHandler(this.ckbAll_CheckedChanged);
			this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
			this.btnLogout.Click += new System.EventHandler(this.btnLogout_Click);
			this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.ChangePage);
			this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
			this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void ckbAll_CheckedChanged(object sender, System.EventArgs e)
		{
			foreach(DataGridItem thisItem in DataGrid1.Items)
			{
				((CheckBox)thisItem.Cells[0].FindControl("ItemCheckBox")).Checked = ckbAll.Checked;
			}
		}

		private void btnDel_Click(object sender, System.EventArgs e)
		{
			string arrID = "";
			foreach(DataGridItem thisItem in DataGrid1.Items)
			{
				if(((CheckBox)thisItem.Cells[0].FindControl("ItemCheckBox")).Checked) 
				{
					arrID += DataGrid1.DataKeys[thisItem.ItemIndex].ToString() + ",";
				}
			}
			if (arrID.Length > 1)
			{
				arrID = arrID.Substring(0,arrID.Length-1);

				// -----------	数据库连接	-----------
				SqlConnection myConnection = new SqlConnection(strConn);
				string SqlCmd  = "Delete From GuestBook_Message Where GM_ID	in (" + arrID	+ ")";
		
				SqlCommand myCommand = new SqlCommand(SqlCmd, myConnection);
				myConnection.Open();
				try
				{
					myCommand.ExecuteNonQuery();
					BindDataGrid(0);
				}
				catch(SqlException ex)
				{	
					lblMessage.Visible  = true;
					lblMessage.Text		= ex.Message;
				}
			}
			else
			{
				lblMessage.Visible  = true;
				lblMessage.Text		= "请选择留言记录,再进行删除操作!";
			}
		}

		private void btnLogout_Click(object sender, System.EventArgs e)
		{
			Response.Cookies["MyCookies"].Expires = DateTime.Now.AddTicks(1);
			Response.Redirect("Index.aspx");
		}
	}
}

⌨️ 快捷键说明

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