//删除多条记录
 //程军修改,单选题,当前添加了两个题目,点击任意某题前的选择框,点击【删除】,【全选】不同步。2010-4-26
 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
 {
     SingleProblem single = new SingleProblem();//创建SingleProblem对象
     foreach(GridViewRow dr in GridView1.Rows)//对GridView中的每一行进行判断
     {
         if (((CheckBox)dr.FindControl("xuanze")).Checked)//如果选择了进行删除
         {
             int ID = int.Parse(((Label)dr.FindControl("Label1")).Text);
             single.ID = ID;
             single.DeleteByProc(ID);
         }
     }
     this.GridView1.DataBind();
 }
 //GridView控件绑定数据事件
 protected void GridViewBind()
 {
     SingleProblem singleproblem = new SingleProblem();  //创建单选题对象
     DataSet ds = singleproblem.QuerySingleProblem(int.Parse(ddlCourse.SelectedValue));//根据考试科目查询单选题信息
     GridView1.DataSource = ds.Tables[0].DefaultView;    //为GridView控件指名数据源
     GridView1.DataBind();//绑定数据
     //胡媛媛修改,为gridview1中的复选框添加事件,2010-5-5
     if (GridView1.Rows.Count>0)
     {
         foreach (GridViewRow dr in GridView1.Rows)
         {
             ((CheckBox)dr.FindControl("xuanze")).Attributes.Add("onclick", "SigchkChg()");
         }
     }
     //胡媛媛修改,为gridview1中的复选框添加事件,2010-5-5
 }
 //删除试题事件
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
 {
     SingleProblem singleproblem = new SingleProblem();  //创建单选题对象
     int ID=int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString()); //取出要删除记录的主键值
     if (singleproblem.DeleteByProc(ID))
     {
         //程军修改,让页面字体不变。2010-5-5
         Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script language=javascript>alert('删除成功!')</script>");
     }
     else
     {
         Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script language=javascript>alert('该题已被收录入试卷,请勿删除!')</script>");
         //程军修改,让页面字体不变。2010-5-5
     }
     GridViewBind();//重新绑定数据
     GridView1.EditIndex = -1;
 }
    //添加或修改事件
    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 = "添加该单选题失败!";
                }
            }
        }
    }
    //初始化数据
    protected void InitData()
    {
        int SingleProblemID = int.Parse(Request["ID"].ToString());  //取出传递过来的试题编号
        SingleProblem singleproblem = new SingleProblem();          //创建单选题对象
        if (singleproblem.LoadData(SingleProblemID))                //如果取出题目信息,分别放在相应控件显示
        {
            ddlCourse.SelectedValue = singleproblem.CourseID.ToString();
            txtTitle.Text = singleproblem.Title;
            txtAnswerA.Text = singleproblem.AnswerA;
            txtAnswerB.Text = singleproblem.AnswerB;
            txtAnswerC.Text = singleproblem.AnswerC;
            txtAnswerD.Text = singleproblem.AnswerD;

            //程军修改,单选题维护,每改动一次标准答案,A选项所在的位置都会被替换为当前最近最新的那个选项。2010-4-26

            //ddlAnswer.SelectedItem.Text = singleproblem.Answer;
            string answer = singleproblem.Answer;
            if (answer == "A")
            {
                ddlAnswer.Items.FindByValue("A").Selected = true;
            }
            if (answer == "B")
            {
                ddlAnswer.Items.FindByValue("B").Selected = true;
            }
            if (answer == "C")
            {
                ddlAnswer.Items.FindByValue("C").Selected = true;
            }
            if (answer == "D")
            {
                ddlAnswer.Items.FindByValue("D").Selected = true;
            }
            //程军修改,单选题维护,每改动一次标准答案,A选项所在的位置都会被替换为当前最近最新的那个选项。2010-4-26
        }
        else                //查询出错,给出提示
        {
            lblMessage.Text = "加载数据出错!";
        }
    }