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

📄 showquestion.aspx.cs

📁 网上自动答疑系统(C#)
💻 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.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace dayi
{
	/// <summary>
	/// showQuestion 的摘要说明。
	/// </summary>
	public class showQuestion : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Label aboutquestion;
		protected System.Web.UI.WebControls.Panel Panel1;
		protected System.Web.UI.WebControls.Label lblUserName;
		protected System.Web.UI.WebControls.TextBox txtUserName;
		protected System.Web.UI.WebControls.Label lblPassword;
		protected System.Web.UI.WebControls.TextBox txtPassword;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.TextBox txtContent;
		protected System.Web.UI.WebControls.Label lblHotRec;
		protected System.Web.UI.WebControls.RequiredFieldValidator userNameValid;
		protected System.Web.UI.WebControls.RequiredFieldValidator userPwdValid;
		protected System.Web.UI.WebControls.RequiredFieldValidator contentValid;
		protected System.Web.UI.WebControls.Label lblAnswerAndAbout;
		protected System.Web.UI.WebControls.Label lblQuestion;
		protected System.Web.UI.WebControls.Label lblQuestionAndAnswer;
		protected System.Web.UI.WebControls.Label lblAboutArgu;
		protected System.Web.UI.WebControls.Button btnDelete;
		protected FreeTextBoxControls.FreeTextBox FreeTextBox1;
		protected System.Web.UI.WebControls.Button btnAnswer;
		protected System.Web.UI.WebControls.Panel panAnswer;
		protected System.Web.UI.WebControls.Button btnShowModify;
		protected System.Web.UI.WebControls.Label lblError;
	
		dayi.controlDatabase cd=new controlDatabase();

		private void showHide(int questionid)
		{
			if(Session["userName"].ToString()!="")
			{
				//得到问题的根ID,以检验当前用户是否为此问题的合法管理者
				DataTable dt=cd.getDataTable("select * from [questions] where [id]="+questionid);
				string rootID="";
				if(dt.Rows.Count>0)
				{
					rootID=dt.Rows[0]["rootID"].ToString().Trim();
				}
				this.txtPassword.Visible=false;
				this.txtUserName.Visible=false;
				this.lblPassword.Visible=false;
				this.lblUserName.Visible=false;
				this.userNameValid.Enabled=false;
				this.userPwdValid.Enabled=false;
				if(Session["userType"].ToString()=="admin" || Session["userType"].ToString()=="master"+rootID)
				{
					//管理员
					this.btnDelete.Visible=true;
					this.btnShowModify.Visible=true;
					this.panAnswer.Visible=true;
					this.Panel1.Visible=false;
					//显示回答问题按钮
					if(!cd.isAnswered(questionid))
					{
						this.btnShowModify.Visible=false;
						this.btnAnswer.Text="回答此问题";
					}
					else
					{
						this.panAnswer.Visible=false;
						DataTable dataTable=cd.getDataTable("select * from [questions] where [id]="+questionid);
						if(dataTable.Rows.Count>0)
						{
							this.FreeTextBox1.Text=dataTable.Rows[0]["answerContent"].ToString();
						}
						this.btnAnswer.Text="修改此回答";
					}
				}
			}
		}

		private void showQuestionAndAnswer(int questionid)
		{
			//显示问题与解答
			string keyword="";
			if(Request.QueryString["keyword"]!=null)
			{
				keyword=Request.QueryString["keyword"].ToString();
			}
			this.lblQuestionAndAnswer.Text=cd.showQuestionAndAnswer(questionid,Convert.ToInt32(Request.QueryString["page"]),keyword);
		}

		private void showAboutArgu(int questionid)
		{
			//显示相关讨论
			this.lblAboutArgu.Text=cd.showQuestionAbout(questionid,10,Convert.ToInt32(Request.QueryString["page"]));
		}
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{
				try
				{
					this.btnDelete.Visible=false;
					this.btnDelete.CausesValidation=false;
					this.panAnswer.Visible=false;
					this.btnShowModify.Visible=false;
					int questionid=Convert.ToInt32(Request.QueryString["questionID"]);

					DataTable dataTable;
					//相关控制显示
					showHide(questionid);

					//显示类别路径
					string path="";
					string selectsql="select * from [questions] where id="+questionid;
					dataTable=cd.getDataTable(selectsql);
					if(dataTable.Rows.Count>0)
					{
						path=dataTable.Rows[0]["boardPath"].ToString().Trim();

					}
					this.Label1.Text=cd.pathToString(path,"showBoard.aspx");
					this.Label1.Text+=cd.ErrorString;

					//显示问题与解答
					showQuestionAndAnswer(questionid);

					//显示相关讨论
					showAboutArgu(questionid);

					//显示相关问题
					this.aboutquestion.Text=cd.showAboutQuestionList(questionid);

					//显示本类热贴
					selectsql="select * from [questions] where id="+Request.QueryString["questionID"].Trim();
					dataTable=cd.getDataTable(selectsql);
					if(dataTable.Rows.Count>0)
					{
						int rootid=Convert.ToInt32(dataTable.Rows[0]["rootid"]);//根ID
						this.lblHotRec.Text=cd.showHotRecord(rootid,"",10,"");
					}
				}
				catch(Exception ee)
				{
					Response.Redirect("err.aspx?err="+ee.Message);
					//this.txtContent.Text=ee.ToString();

				}
			}
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnShowModify.Click += new System.EventHandler(this.btnShowModify_Click);
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			this.btnAnswer.Click += new System.EventHandler(this.btnAnswer_Click);
			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)
		{
			//保存相关讨论
			//如果已经登录,则相接得到讨论内容
			//如果没有登录,则必须验证合法性
			//Session["userName"]="shanzhu";
			try
			{
				int questionid=Convert.ToInt32(Request.QueryString["questionID"]);
				if(Session["userName"].ToString()=="")
				{
					//用户未登录
					string username=this.txtUserName.Text.Trim();
					string userPwd=this.txtPassword.Text.Trim();
					userPwd=FormsAuthentication.HashPasswordForStoringInConfigFile(userPwd,"sha1");//加密
					string sql="select * from [user] where userName='"+username+"' and userPwd='"+userPwd+"'";
					if(cd.catchRecord(sql))
					{
						string content=this.txtContent.Text;
						string insertsql="insert into [questionAbout](addUser,questionID,content) values('"+username+"',"+questionid+",'"+content+"')";
						cd.updateDatabase(insertsql);
						
						showAboutArgu(questionid);

						//this.txtContent.Text=cd.ErrorString;
					}
				}
				else
				{
					//用户已登录
					string content=this.txtContent.Text;
					string insertsql="insert into [questionAbout](addUser,questionID,content) values('"+Session["userName"].ToString()+"',"+questionid+",'"+content+"')";
					cd.updateDatabase(insertsql);
					
					showAboutArgu(questionid);

					this.txtContent.Text="";
				}
				this.txtContent.Text="";
				this.txtUserName.Text="";
			}
			catch(Exception ee)
			{
				//Response.Redirect("err.aspx?err="+ee.Message);
				Response.Write(ee.ToString());
			}
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			//删除当前的贴子
			try
			{
				//this.contentValid.Enabled=false;
				string delsql="delete from [questions] where [questions].[id]="+Request.QueryString["questionID"].ToString();
				this.txtContent.Text=cd.updateDatabase(delsql).ToString();
				delsql="delete from [questionAbout] where [questionAbout].[questionID]="+Request.QueryString["questionID"].ToString();
				this.txtContent.Text+=cd.updateDatabase(delsql);
				Response.Redirect("default.aspx");
			}
			catch(Exception)
			{
				//Response.Redirect("err.aspx?err="+ee.Message);
			}
		}

		private void btnShowModify_Click(object sender, System.EventArgs e)
		{
			this.panAnswer.Visible=!this.panAnswer.Visible;
		}

		private void btnAnswer_Click(object sender, System.EventArgs e)
		{
			string answercontent=this.FreeTextBox1.Text;
			int questionid=Convert.ToInt32(Request.QueryString["questionID"]);
			try
			{
				if(answercontent.Trim()!="")
				{
					cd.modifyQuestion(questionid,"","","",answercontent,"answer");
				
					this.showHide(questionid);

					this.showQuestionAndAnswer(questionid);

					this.lblError.Text=cd.ErrorString;
				}
				else
				{
					this.lblError.Text="<font color=red>请输入回答内容</font>";
				}
			}
			catch(Exception ee)
			{
				this.lblError.Text+=ee.ToString();
			}
		}
	}
}

⌨️ 快捷键说明

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