コード例 #1
0
    //添加或修改事件
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            SingleProblem singleproblem = new SingleProblem();          //创建单选题对象
            singleproblem.CourseID = int.Parse(ddlCourse.SelectedValue);//为单选题对象各属性赋值

            //程军添加,单选题题目不能超过1000个字符,自动截取前1000个字符.2010-4-26;
            if (txtTitle.Text.Length > 1000)
            {
                this.Label1.Visible = true;
                this.Label1.Text = "题干不能超过1000字符,自动截取前1000字符";
                //程军修改,单选题自动截取前1000个字符。 2010-5-5
                txtTitle.Text = txtTitle.Text.Substring(0, 1000);
                //程军修改,单选题自动截取前1000个字符。 2010-5-5
                singleproblem.Title = txtTitle.Text.Substring(0, 1000);
            }
            else
            {
                singleproblem.Title = txtTitle.Text;
            }

            if (txtAnswerA.Text.Length > 500)
            {
                this.Label2.Visible = true;
                this.Label2.Text = "答案不能超过500字符,自动截取前500字符";
                //程军修改,单选题答案自动截取前500字符。2010-5-5
                txtAnswerA.Text = txtAnswerA.Text.Substring(0, 500);
                //程军修改,单选题答案自动截取前500字符。2010-5-5
                singleproblem.AnswerA = txtAnswerA.Text.Substring(0, 500);

            }
            else
            {
                singleproblem.AnswerA = txtAnswerA.Text;
            }

            if (txtAnswerB.Text.Length > 500)
            {
                this.Label3.Visible = true;
                this.Label3.Text = "答案不能超过500字符,自动截取前500字符";
                //程军修改,单选题答案自动截取前500字符。2010-5-5
                txtAnswerB.Text = txtAnswerB.Text.Substring(0, 500);
                //程军修改,单选题答案自动截取前500字符。2010-5-5
                singleproblem.AnswerB = txtAnswerB.Text.Substring(0, 500);

            }
            else
            {
                singleproblem.AnswerB = txtAnswerB.Text;
            }

            if (txtAnswerC.Text.Length > 500)
            {
                this.Label4.Visible = true;
                this.Label4.Text = "答案不能超过500字符,自动截取前500字符";
                //程军修改,单选题答案自动截取前500字符。2010-5-5
                txtAnswerC.Text = txtAnswerC.Text.Substring(0, 500);
                //程军修改,单选题答案自动截取前500字符。2010-5-5
                singleproblem.AnswerC = txtAnswerC.Text.Substring(0, 500);

            }
            else
            {
                singleproblem.AnswerC = txtAnswerC.Text;
            }

            if (txtAnswerD.Text.Length > 500)
            {
                this.Label5.Visible = true;
                this.Label5.Text = "答案不能超过500字符,自动截取前500字符";
                //程军修改,单选题答案自动截取前500字符。2010-5-5
                txtAnswerD.Text = txtAnswerD.Text.Substring(0, 500);
                //程军修改,单选题答案自动截取前500字符。2010-5-5
                singleproblem.AnswerD = txtAnswerD.Text.Substring(0, 500);

            }
            else
            {
                singleproblem.AnswerD = txtAnswerD.Text;
            }
            //程军添加,单选题题目不能超过1000个字符,自动截取前1000个字符.2010-4-26;

            //胡媛媛添加,判断备选答案是否有相同,2010-4-26
            if (txtAnswerA.Text == txtAnswerB.Text || txtAnswerA.Text == txtAnswerC.Text || txtAnswerA.Text == txtAnswerD.Text)
            {
                lblMessage.Text = "不能有相同的备选答案!";
                return;
            }
            else if (txtAnswerB.Text == txtAnswerC.Text || txtAnswerB.Text == txtAnswerD.Text)
            {
                lblMessage.Text = "不能有相同的备选答案!";
                return;
            }
            else if (txtAnswerC.Text != "" && txtAnswerD.Text != "" && txtAnswerC.Text == txtAnswerD.Text)
            {
                lblMessage.Text = "不能有相同的备选答案!";
                return;
            }

            //胡媛媛添加,判断备选答案是否有相同,2010-4-26

            //singleproblem.Title = txtTitle.Text;
            //singleproblem.AnswerA = txtAnswerA.Text;
            //singleproblem.AnswerB = txtAnswerB.Text;
            //singleproblem.AnswerC = txtAnswerC.Text;
            //singleproblem.AnswerD = txtAnswerD.Text;
            //程军修改,单选题维护,每改动一次标准答案,A选项所在的位置都会被替换为当前最近最新的那个选项。2010-4-26
            //singleproblem.Answer = ddlAnswer.SelectedItem.Text;
            if (ddlAnswer.Items.FindByValue("A").Selected)
            {
                singleproblem.Answer = "A";
            }
            if(ddlAnswer.Items.FindByValue("B").Selected)
            {
                singleproblem.Answer = "B";
            }
            //start:胡媛媛修改,答案CD没有时,不能选择该答案
            if (ddlAnswer.Items.FindByValue("C").Selected)
            {
                if (txtAnswerC.Text != "")
                {
                    singleproblem.Answer = "C";
                }
                else
                {
                    lblMessage.Text = "没有答案C!";
                    return;
                }
            }
            if (ddlAnswer.Items.FindByValue("D").Selected)
            {
                if (txtAnswerD.Text != "")
                {
                    singleproblem.Answer = "D";
                }
                else
                {
                    lblMessage.Text = "没有答案D!";
                    return;
                }
            }
            //end:胡媛媛修改,答案CD没有时,不能选择该答案

            //程军修改,单选题维护,每改动一次标准答案,A选项所在的位置都会被替换为当前最近最新的那个选项。2010-4-26
            if (Request["ID"] != null)                                  //如果是修改题目信息
            {
                singleproblem.ID = int.Parse(Request["ID"].ToString()); //取出试题主键
                if (singleproblem.UpdateByProc(int.Parse(Request["ID"].ToString()), int.Parse(ddlCourse.SelectedValue)))//调用修改试题方法修改试题
                {
                    lblMessage.Text = "成功修改该单选题!";
                }
                else
                {
                    lblMessage.Text = "修改该单选题失败!";
                }
            }
            else                                                        //如果是添加试题
            {
                if (singleproblem.InsertByProc(int.Parse(ddlCourse.SelectedValue)))                       //调用添加试题方法添加试题
                {
                    lblMessage.Text = "成功添加该单选题!";
                }
                else
                {
                    lblMessage.Text = "添加该单选题失败!";
                }
            }
        }
    }