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

📄 addquestion.aspx.cs

📁 用ASP.NET(C#)、SQL Server2000数据库开发在线考试系统源代码
💻 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;
using System.IO;

namespace ExamineSystem
{
	/// <summary>
	/// Summary description for AddQuestion.
	/// </summary>
	public class AddQuestion : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Button PreviewQuestion;
		protected System.Web.UI.WebControls.Button AddNewQuestion;
		protected System.Web.UI.WebControls.TextBox QuesTitle;
		protected System.Web.UI.WebControls.TextBox QuesBody;
		protected System.Web.UI.WebControls.TextBox Defficult;
		protected System.Web.UI.WebControls.TextBox Mark;
		protected System.Web.UI.WebControls.DropDownList QuesKindList;
		protected System.Web.UI.HtmlControls.HtmlInputFile QuesPicture;
		protected System.Web.UI.WebControls.RegularExpressionValidator rM;
		protected System.Web.UI.WebControls.Button UpdateQuestion;
		protected System.Web.UI.WebControls.RegularExpressionValidator rgDiff;
		protected System.Web.UI.WebControls.RangeValidator rvValue;
		protected System.Web.UI.HtmlControls.HtmlGenericControl title;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			///验证用户的权限
			if(Session["UserID"] == null)
			{
				Response.Redirect("~/Default.aspx");
			}
			
			

			if(!Page.IsPostBack)
			{
				BindQuesKindData();
			}
		}

		private void BindQuesKindData()
		{
			QuesKindList.Items.Clear();

			QuestionDB question = new QuestionDB();
			SqlDataReader rect = question.GetQuestionKinds();

			QuesKindList.DataSource = rect;
			QuesKindList.DataTextField = "KindName";
			QuesKindList.DataValueField = "KindID";
			QuesKindList.DataBind();

			rect.Close();
		}

		#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.AddNewQuestion.Click += new System.EventHandler(this.AddNewQuestion_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void AddNewQuestion_Click(object sender, System.EventArgs e)
		{
			int nPictureID = 0;
			QuestionDB question = new QuestionDB();

			if(QuesPicture.PostedFile.ContentLength > 0)
			{
				nPictureID = UpPictureFile(QuesPicture);
			}
			Decimal diffcult = Decimal.Parse(Defficult.Text.Trim());
			string tempDiffcult = (100 * diffcult).ToString();
			string diffcultString = tempDiffcult.Substring(0,tempDiffcult.IndexOf("."));
			
			question.AddQuestion(QuesTitle.Text,QuesBody.Text,Int32.Parse(diffcultString),
				Int32.Parse(Mark.Text.Trim()),Int32.Parse(QuesKindList.SelectedValue),nPictureID);

			Response.Write("<script>alert(\"添加新的问题成功!!!\")</script>");
		}

		private int UpPictureFile(HtmlInputFile UpLoadFile)
		{
			PictureDB picture = new PictureDB();
			int nPictureID = 0;

			if(UpLoadFile.PostedFile.ContentLength > 0)
			{
				String fileName = UpLoadFile.PostedFile.FileName.Substring(UpLoadFile.PostedFile.FileName.LastIndexOf("\\"),
					UpLoadFile.PostedFile.FileName.Length - UpLoadFile.PostedFile.FileName.LastIndexOf("\\"));
				
				String fileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() 
					+ DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() 
					+ DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
					+ DateTime.Now.Millisecond.ToString();

				fileName = "\\" + fileTime + GetRandomint() 
					+ fileName.Substring(fileName.IndexOf("."),fileName.Length - fileName.IndexOf("."));

				if(File.Exists(Server.MapPath(Request.ApplicationPath) + "\\UpLoads\\Files" + fileName) == false)
				{
					try
					{
						UpLoadFile.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + "\\UpLoads\\Files" + fileName);

						nPictureID = picture.AddPicture("","\\UpLoads\\Files" + fileName);
					}
					catch(Exception ex)
					{
						string sRawURL = Request.RawUrl;

						if(sRawURL.IndexOf("?") > -1)
						{
							sRawURL = sRawURL.Substring(0,sRawURL.IndexOf("?"));
						}				
						Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl="
							+ sRawURL + "&ErrorMessage=" + ex.Message.Replace("\n"," "));
					}
				}
				else
				{
					Response.Write("<script>alert(\"此文件已经存在,请重新命名你的文件!\")</script>");
				}
			}
			else
			{
				Response.Write("<script>alert(\"文件名和内容不能为空!\")</script>");
			}

			return(nPictureID);
		}

		private String GetRandomint()
		{
			Random random = new Random();

			return(random.Next(10000).ToString());
		}
	}
}

⌨️ 快捷键说明

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