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

📄 editquestion.ascx.cs

📁 一个多用户在线题库管理系统,可以实现各类试题的添加和管理
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using TQMS.BusinessLogicLayer;
using TQMS.BusinessLogicHelper;
using TQMS.DataAccessHelper;
using TQMS.DataAccessLayer;

public partial class modules_EditQuestion : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null)
        {
            Response.Redirect("~/Default.aspx");
            return;
        }
        if (!IsPostBack)
        {
            string action = Request.QueryString["action"].ToString();
            if (action == "add")
            {
                this.btnOk.Text = "添加";
                this.questionImg.Visible = false;
                this.lblMessege.Visible = false;
                this.lblquestionID.Text = "注意:请认真填写下列信息,其中备选答案最多100字符,完成后按确定按钮进行提交!!!";
                this.lblquestionID.Style["Color"] = "Black";
                if (Common.BindDdlCourse(this.ddlCourse) != 0)//绑定科目下拉列表框
                {
                    Common.BindChapter(this.ddlChapter, int.Parse(this.ddlCourse.SelectedValue));
                
                }
                if(this.ddlChapter.Items.Count < 1||this.ddlCourse.Items == null )
                {
                    this.hlkAddChapter.Visible = true;
                    this.btnOk.Enabled = false;
                    this.lblMessege.Visible = true;
                    this.lblMessege.Text = "暂无科目,请添加科目信息";
                    this.lblMessege.Style["Color"] = "red";
                }
                Common.BindDdlType(this.ddlType);
                Common.BindDdlDifficult(this.ddlDifficult);
            }
            else
            {
                string questionID = Request.QueryString["id"].ToString();
                this.btnOk.Text = "修改";
                this.lblquestionID.Text = "编号:" + questionID;
                this.lblquestionID.Style["font-weight:"] = "true";
                Question question = new Question();
                question.LoadData(int.Parse(questionID));
                Chapter chapter = new Chapter();
                chapter.LoadData(question.ChapterID);
               if (Common.BindDdlCourse(this.ddlCourse,chapter.CourseID) != 0)//绑定科目下拉列表框
                {
                    Common.BindChapter(this.ddlChapter, int.Parse(this.ddlCourse.SelectedValue));

                }
                //题型与难度
                  Common.BindDdlType(this.ddlType, question.Type.ToString());
                  Common.BindDdlDifficult(this.ddlDifficult, question.DifficultLevel.ToString());
                this.tbxContent.Text = question.QuestionContent;
                this.tbxRightAnser.Text = question.RightAnser;
                if (question.ImageUrl != "default.jpg")
                {
                    this.questionImg.Visible = true;
                    this.questionImg.ImageUrl = "~/upload/" + question.ImageUrl;
                }
                else
                {
                    this.questionImg.Visible = false;
                }

            }
        }

    }

    /// <summary>
    /// 自动绑定章目信息到章目下拉列表框中
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (this.ddlCourse.SelectedValue == "0")
        {
            this.ddlChapter.Items.Clear();
            return;
        }
        else
        {
            this.ddlChapter.Items.Clear();
            Common.BindChapter(this.ddlChapter, int.Parse(this.ddlCourse.SelectedValue));//绑定章节下拉列表框
            if (this.ddlChapter.Items.Count < 1)
            {
                this.hlkAddChapter.Visible = true;
                this.btnOk.Enabled = false;
                this.lblMessege.Visible = true;
                this.lblMessege.Text = "暂无科目,请添加科目信息";
                this.lblMessege.Style["Color"] = "red";
            }
            else
            {
                this.hlkAddChapter.Visible = false;
                this.btnOk.Enabled = true;
                this.lblMessege.Text = "";
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="source"></param>
    /// <param name="args"></param>
    protected void vudCourse_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (ddlCourse.SelectedValue == "0")
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
    /// <summary>
    /// 自定义验证控件,查看是否选中章节
    /// </summary>
    /// <param name="source"></param>
    /// <param name="args"></param>
    protected void cvdChapter_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (this.ddlChapter.SelectedValue == "0")
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }


    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            ViewState.Add("ddlCourse",this.ddlCourse .SelectedValue);
            ViewState.Add("ddlChapter", ddlChapter.SelectedValue);
            ViewState.Add("ddlDifficult", this.ddlDifficult.SelectedValue);
            ViewState.Add("ddlType", this.ddlType.SelectedValue);

            //1.添加试题信息的哈希表


            Hashtable ht = new Hashtable();
            string content = SqlStringConstructor.GetQuotedString(this.tbxContent.Text.Trim());
            ht.Add("TQ_QuestionContent", content);
            string anser = SqlStringConstructor.GetQuotedString(this.tbxRightAnser.Text.Trim());
            ht.Add("TQ_RightAnser", anser);
            string Level = SqlStringConstructor.GetQuotedString(this.ddlDifficult.SelectedValue);
            ht.Add("TQ_DifficultyLevel", Level);
            string type = SqlStringConstructor.GetQuotedString(this.ddlType.SelectedValue);
            ht.Add("TQ_Type", type);
            ht.Add("TQ_UserID", int.Parse(Session["UserID"].ToString()));
            int chapterID = int.Parse(this.ddlChapter.SelectedValue);
            ht.Add("TQ_ChapterID", chapterID);

            if (Request.QueryString["action"].ToString() == "add")
            {
                string addTime = SqlStringConstructor.GetQuotedString(DateTime.Now.ToShortDateString());
                ht.Add("TQ_AddTime", addTime);
                string editTime = SqlStringConstructor.GetQuotedString(DateTime.Now.ToShortDateString());
                ht.Add("TQ_LastEditTime", editTime);
            }
            else
            {
                string editTime = SqlStringConstructor.GetQuotedString(DateTime.Now.ToString());
                ht.Add("TQ_LastEditTime", editTime);
            }


            // ------------  上传图片 --------------------------------------

            string upLoadName = uploadPictureFile.FileName;
            if (upLoadName != "")
            {
                string fileType = upLoadName.Substring(upLoadName.ToString().LastIndexOf(".") + 1).ToString();//获取文件名后缀
                //判断文件格式   
                if (fileType == "jpg" || fileType == "bmp" || fileType == "gif" || fileType == "jpeg")
                {
                    String savePath = Server.MapPath("~/Upload/");
                    string imageSaveName = DateTime.Now.Ticks.ToString() + "." + fileType;//图片名以当前时间为文件名,确保文件名没有重复
                    savePath += imageSaveName;
                    uploadPictureFile.SaveAs(savePath);
                    ht.Add("TQ_ImageUrl", SqlStringConstructor.GetQuotedString(imageSaveName));

                }
                else
                {
                    Response.Write("<script language='javascript'>alert('文件类型不正确,只能上传jpeg,bmp和gif文件。');</script>");
                }
            }
            //---------------上传图片结束  ----------------------

            // 2.添加或修改试题
            Question question = new Question();
            if (Request["action"] == "add")
            {
                if (question.Add(ht))
                {
                    this.lblMessege.Visible = true;
                    this.lblMessege.Text = "试题添加成功!!!";
                    this.lblMessege.Style["Color"] = "green";
                    this.tbxContent.Text = "";
                    this.tbxRightAnser.Text = "";
                }
                else
                {
                    this.lblMessege.Visible = true;
                    this.lblMessege.Text = "试题添加失败!!!";
                    this.lblMessege.Style["Color"] = "red";
                }
            }
            else
            {
                //问题编号,供update使用
                question.LoadData(int.Parse(Request.QueryString["id"].ToString()));
                if (question.Update(ht))
                {
                    Response.Redirect("~/ShowQuestionByID.aspx?id=" + question.QuestionID);

                }
                else
                {
                    this.lblMessege.Visible = true;
                    this.lblMessege.Text = "试题修改失败!";
                    this.lblMessege.Style["Color"] = "red";
                }
            }

        }
    }

    //定义能恢复下拉列表框的函数
    protected void resetQuery()
    {
        //科目
        foreach (ListItem item in ddlCourse.Items)
        {
            if (item.Value == ViewState["ddlCourse"].ToString())
                item.Selected = true;
            else
                item.Selected = false;
        }
        //章节
        foreach (ListItem item in ddlCourse.Items)
        {
            if (item.Value == ViewState["ddlChapter"].ToString())
                item.Selected = true;
            else
                item.Selected = false;
        }
        //类型
        foreach (ListItem item in ddlType.Items)
        {
            if (item.Value == ViewState["ddlType"].ToString())
                item.Selected = true;
            else
                item.Selected = false;
        }
        //难度
        foreach (ListItem item in ddlDifficult.Items)
        {
            if (item.Value == ViewState["ddlDifficult"].ToString())
                item.Selected = true;
            else
                item.Selected = false;
        }
    }
}

⌨️ 快捷键说明

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