📄 editquestion.aspx.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 EditQuestion.
/// </summary>
public class EditQuestion : System.Web.UI.Page
{
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.WebControls.Button PreviewQuestion;
protected System.Web.UI.HtmlControls.HtmlGenericControl title;
protected System.Web.UI.WebControls.Button UpdateQuestion;
protected System.Web.UI.HtmlControls.HtmlInputFile QuesPicture;
private int nQuestionID = 0;
private void Page_Load(object sender, System.EventArgs e)
{
///验证用户的权限
if(Session["UserID"] == null)
{
Response.Redirect("~/Default.aspx");
}
if(Request.Params["QuestionID"] != null)
{
nQuestionID = Int32.Parse(Request.Params["QuestionID"].ToString());
}
if(!Page.IsPostBack)
{
BindQuesKindData();
if(nQuestionID > 0)
{
BindQuestionData(nQuestionID);
}
}
}
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();
}
private void BindQuestionData(int nQuestionID)
{
QuestionDB question = new QuestionDB();
SqlDataReader recq = question.GetSingleQuestion(nQuestionID);
while(recq.Read())
{
QuesTitle.Text = recq["Title"].ToString();
QuesBody.Text = recq["Body"].ToString();
Defficult.Text = (Decimal.Parse(recq["Defficult"].ToString())/100).ToString();
Mark.Text = recq["Mark"].ToString();
SetListIndex(recq);
QuesPicture.Disabled = true;
}
recq.Close();
}
private void SetListIndex(SqlDataReader recq)
{
for(int i = 0; i < QuesKindList.Items.Count; i++)
{
if(QuesKindList.Items[i].Value == recq["KindID"].ToString())
{
QuesKindList.SelectedIndex = i;
break;
}
}
}
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());
}
#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.UpdateQuestion.Click += new System.EventHandler(this.UpdateQuestion_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void UpdateQuestion_Click(object sender, System.EventArgs e)
{
int nPictureID = 0;
QuestionDB question = new QuestionDB();
if(QuesPicture.Disabled == false)
{
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.UpdateQuestion(nQuestionID,QuesTitle.Text,QuesBody.Text,Int32.Parse(diffcultString),
Int32.Parse(Mark.Text.Trim()),Int32.Parse(QuesKindList.SelectedValue),nPictureID);
Response.Write("<script>alert(\"修改问题参数成功!!!\")</script>");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -