欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

reply.aspx.cs

一个比较不错的专门为技术型站点订制的专业论坛源码
CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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.Xml;

namespace bbs
{
	/// <summary>
	/// Summary description for reply.
	/// </summary>
	public class reply : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox title;
		protected System.Web.UI.WebControls.TextBox content;
		protected System.Web.UI.WebControls.Panel Panel1;
		protected System.Web.UI.WebControls.HyperLink HyperLink1;
		protected System.Web.UI.WebControls.Panel Panel2;
		protected System.Web.UI.HtmlControls.HtmlForm post;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
		protected System.Web.UI.WebControls.Button Button1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			string thisURL = "http://" + Request.ServerVariables["SERVER_NAME"].ToString() + Request.ServerVariables["SCRIPT_NAME"].ToString();
			if(!IsAuthed())
			{
				Response.Redirect("login.aspx?returnurl=" + Server.UrlEncode(thisURL + "?board=" + Request["board"] + "&filename=" + Request["filename"]));
			}
		}

		private bool IsAuthed()
		{
			if(Session["userid"] != null)
				return true;
			else
				return false;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Button1_Click(object sender, System.EventArgs e)
		{
			if(Page.IsValid)
			{
				WriteXmlFile();
				WriteDataBase();
				Response.Redirect("topic/" + Request["filename"]);
			}
		}

		private void WriteXmlFile()
		{
			XmlDocument doc = new XmlDocument();
			XmlTextReader reader = new XmlTextReader(Server.MapPath(".") + "\\topic\\" + Request["filename"]);
			doc.Load(reader);
			reader.Close();

			XmlElement message;

			XmlNode root = doc.DocumentElement;
			
			XmlElement Reply = doc.CreateElement("Reply");
			root.AppendChild(Reply);

			//写入标题
			message = doc.CreateElement("Title");
			message.InnerText = this.title.Text;
			Reply.AppendChild(message);
			
			//写入发贴时间
			message = doc.CreateElement("PostTime");
			message.InnerText = DateTime.Now.ToString();
			Reply.AppendChild(message);
			
			//内容
			message = doc.CreateElement("Content");
			message.InnerText = this.content.Text;
			Reply.AppendChild(message);

			message = doc.CreateElement("Solver");
			message.InnerText = "NO";
			Reply.AppendChild(message);
			
			SqlConnection conn = new SqlConnection((string)Application["ConnectionString"]);
			conn.Open();
			string sql = "select * from Users where ID = " + Session["userid"].ToString();;
			SqlCommand cmd = new SqlCommand(sql,conn);
			SqlDataReader sqlreader = cmd.ExecuteReader();
			try
			{
				sqlreader.Read();

				//作者信息
				XmlElement poster = doc.CreateElement("Poster");
				Reply.AppendChild(poster);

				message = doc.CreateElement("ID");
				message.InnerText = sqlreader["ID"].ToString();
				poster.AppendChild(message);

				message = doc.CreateElement("UserName");
				message.InnerText = sqlreader["UserName"].ToString();
				poster.AppendChild(message);

				message = doc.CreateElement("Email");
				message.InnerText = sqlreader["Email"].ToString();
				poster.AppendChild(message);

				SqlConnection connection = new SqlConnection((string)Application["ConnectionString"]);
				connection.Open();
				string query = "select count(*) from Users where Expert >= " + sqlreader["Expert"].ToString();
				SqlCommand command = new SqlCommand(query,connection);
				SqlDataReader r = command.ExecuteReader();
				try
				{
					r.Read();
					string rate;
					if(r.GetInt32(0) < 100)
						rate = "第" + r.GetInt32(0).ToString() + "名";
					else
						rate = "---";
					message = doc.CreateElement("Rate");
					message.InnerText = rate;
					poster.AppendChild(message);
				}
				catch
				{
					Response.Write("数据库读写出错");
					Response.End();
					return;
				}
				finally
				{
					r.Close();
					command.Dispose();
					connection.Close();
				}
			}
			catch
			{
				Response.Write("数据库读写出错");
				Response.End();
				return;
			}
			finally
			{
				conn.Close();
				cmd.Dispose();
				sqlreader.Close();
			}
			
			XmlTextWriter xmlWriter = new XmlTextWriter(Server.MapPath(".") + "\\topic\\" + Request["filename"],null);
			xmlWriter.Formatting = Formatting.Indented;
			try
			{
				doc.Save(xmlWriter);
			}
			catch
			{
				Response.Write("数据库读写出错");
				Response.End();
				return;
			}

			xmlWriter.Close();
			reader.Close();
		}

		private void WriteDataBase()
		{
			string[] filename = Request["filename"].Split('.');
			SqlConnection conn = new SqlConnection((string)Application["ConnectionString"]);
			conn.Open();
			string sql = "update BBS set Reply = Reply + 1,LastReply = '" + DateTime.Now + "' where FileName = '" + filename[0] + "'";
			SqlCommand cmd = new SqlCommand(sql,conn);
			try
			{
				cmd.ExecuteNonQuery();
			}
			catch
			{
				Response.Write("数据库读写出错");
				Response.End();
			}
		}
	}
}

⌨️ 快捷键说明

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