using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Telerik.Web.UI; namespace VRS.Management.VSAT { public partial class ExamTheoryBase_Add_Custom : BasePage { DataService.BLL.pro_exam_batch bll = new DataService.BLL.pro_exam_batch(); DataService.BLL.admin_log log = new DataService.BLL.admin_log(); DataService.BLL.admin_user bll_user = new DataService.BLL.admin_user(); DataService.BLL.pro_theory_base bll_theory_base = new DataService.BLL.pro_theory_base(); DataService.BLL.pro_theory_base_item bll_theory_base_item = new DataService.BLL.pro_theory_base_item(); protected override void Page_Load(object sender, EventArgs e) { base.Page_Load(sender, e); if (!IsPostBack) { //var user = DataService.BLL.admin_user.load_login(); //var user_login = bll_user.GetModel(user.user_id); //DataLoad(); BindDeviceType("TheoryBaseKnow", dp_know, ""); } } public void DataLoad() { var list = bll_theory_base.GetModelList("").ToList(); RadListBoxDestination.DataSource = list; RadListBoxDestination.DataValueField = "id"; RadListBoxDestination.DataTextField = "question"; RadListBoxDestination.DataSortField = "question"; RadListBoxDestination.DataBind(); } protected void RadListBoxDestination_Reordered(object sender, RadListBoxEventArgs e) { RadListBoxDestination.DataBind(); ReorderAZ(); } protected void RadListBoxDestination_Deleted(object sender, RadListBoxEventArgs e) { span_name.InnerHtml = ""; RadListBoxDestination.DataBind(); ReorderAZ(); } protected void RadListBoxDestination_Deleting(object sender, RadListBoxDeletingEventArgs e) { if (RadListBoxDestination.Items.Count <= 2) { span_name.InnerHtml = "至少有两个选项!"; e.Cancel = true; } } //添加单个选项 protected void btnAddItem_Click(object sender, EventArgs e) { /* var item = new RadListBoxItem(); item.Text = txt_additem.Text.Trim(); var addItem = new RadListBoxItem(item.Text, item.Text); RadListBoxDestination.Items.Add(addItem); var added_index = addItem.Index; RadioButtonList radbuttonlist = addItem.FindControl("RadioButtonList_Answer") as RadioButtonList; if (null != radbuttonlist) { radbuttonlist.SelectedValue = RadioButtonList_answer.SelectedValue; } */ var kind = dp_kind.SelectedValue; if (string.IsNullOrEmpty(kind)) { span_name.InnerHtml = "题目类型不能为空!"; return; } if (kind == "简答题" || kind == "填空题") { span_name.InnerHtml = "该题目类型不能添加选项!"; return; } if (kind == "判断题") { if (RadListBoxDestination.Items.Count >= 2) { span_name.InnerHtml = "判断题只有两个选项!"; return; } } else if (kind == "单选题") { } else if (kind == "多选题") { } span_name.InnerHtml = ""; AddNewItem(); RadListBoxDestination.DataBind(); } /// /// 更新选项值 /// /// /// protected void option_name_TextChanged(object sender, EventArgs e) { var txtBox = sender as RadTextBox; var rad_item = txtBox.Parent as RadListBoxItem; if (null != rad_item) { rad_item.Text = txtBox.Text.Trim(); } } protected void btnSure_Click(object sender, EventArgs e) { btnSure.Enabled = false; DataService.Model.pro_theory_base model = new DataService.Model.pro_theory_base(); var user = DataService.BLL.admin_user.load_login(); if (string.IsNullOrEmpty(dp_kind.SelectedValue)) { RadAjaxManager1.Alert("题目类型不能为空!"); btnSure.Enabled = true; return; } if (string.IsNullOrEmpty(question_name.Text.Trim())) { RadAjaxManager1.Alert("题干不能为空!"); btnSure.Enabled = true; return; } if (string.IsNullOrEmpty(dp_know.SelectedValue)) { RadAjaxManager1.Alert("知识领域不能为空!"); btnSure.Enabled = true; return; } int right_count = 0; var questionTK = question_name.Text.Trim(); var answerTK = answer_write.Text.Trim(); List list_answer = new List(); if (dp_kind.SelectedValue != "简答题" && dp_kind.SelectedValue != "填空题") { var flag = ExamTheoryBase_Edit_Custom.CheckRightAnswerCount(RadListBoxDestination, ref right_count, list_answer); if (!flag) { RadAjaxManager1.Alert("选项值不能为空!"); btnSure.Enabled = true; return; } } var total_count = RadListBoxDestination.Items.Count; //判断题、单选题、多选题 if (dp_kind.SelectedValue == "判断题") { if (total_count != 2) { RadAjaxManager1.Alert("判断题只有两个选项!"); btnSure.Enabled = true; return; } if (right_count != 1) { RadAjaxManager1.Alert("判断题只有1个选项答案!"); btnSure.Enabled = true; return; } } else if (dp_kind.SelectedValue == "单选题") { if (total_count < 2) { RadAjaxManager1.Alert("单选题至少两个选项!"); btnSure.Enabled = true; return; } if (right_count != 1) { RadAjaxManager1.Alert("单选题只有1个选项答案!"); btnSure.Enabled = true; return; } } else if (dp_kind.SelectedValue == "多选题") { if (total_count < 2) { RadAjaxManager1.Alert("多选题至少两个选项!"); btnSure.Enabled = true; return; } if (right_count < 2) { RadAjaxManager1.Alert("多选题至少2个选项答案!"); btnSure.Enabled = true; return; } } else if (dp_kind.SelectedValue == "简答题") { var answer = answer_write.Text.Trim(); if (string.IsNullOrEmpty(answer)) { RadAjaxManager1.Alert("简答题答案不能为空!"); btnSure.Enabled = true; return; } } else if (dp_kind.SelectedValue == "填空题") { var answer = answer_write.Text.Trim(); if (string.IsNullOrEmpty(answer)) { RadAjaxManager1.Alert("填空题答案不能为空!"); btnSure.Enabled = true; return; } var line2 = "__"; questionTK = questionTK.Replace("__________", line2) .Replace("_________", line2) .Replace("________", line2) .Replace("_______", line2) .Replace("______", line2) .Replace("_____", line2) .Replace("____", line2) .Replace("___", line2); // var question_array = question.Split(new string[] { line2 }, StringSplitOptions.RemoveEmptyEntries); var question_count = CountSubstringOccurrences(questionTK, line2); answerTK = answerTK.Replace("__________", line2) .Replace("_________", line2) .Replace("________", line2) .Replace("_______", line2) .Replace("______", line2) .Replace("_____", line2) .Replace("____", line2) .Replace("___", line2); var answer_array = answerTK.Split(new string[] { line2 }, StringSplitOptions.None); if (question_count != answer_array.Length) { RadAjaxManager1.Alert(string.Format("填空题空格数:{0} ,与答案数:{1} 不一致!", question_count, answer_array.Length)); btnSure.Enabled = true; return; } } var id = BasePage.GetId(); model.id = id; model.target = dp_know.SelectedValue; model.kind = dp_kind.SelectedValue; model.question = question_name.Text.Trim(); model.create_time = DateTime.Now; model.create_by = user.user_id; //model.r1 = r1.Text.Trim();//备注 if (dp_kind.SelectedValue == "填空题") { model.question = questionTK; model.answer = answerTK; } else if (dp_kind.SelectedValue == "简答题") { model.answer = answer_write.Text.Trim(); } else //其它题型 { model.answer = string.Join(",", list_answer); } int index = 1; var now = DateTime.Now; Dictionary dic = new Dictionary(); string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var length = alphabet.Length; for (int i = 0; i < 26; i++) { dic.Add(i, alphabet.Substring(i, 1)); } if (bll_theory_base.Add(model)) { log.write_log("添加题目成功。" + "名称:" + model.question + ",id:" + model.id); for (int i = 0; i < RadListBoxDestination.Items.Count; i++) { var list_item = RadListBoxDestination.Items[i]; var item = new DataService.Model.pro_theory_base_item(); item.id = BasePage.GetNextId(id, index++); item.theory_base_id = id; item.no = (i + 1); if (dic.ContainsKey(i)) { item.item_letter = dic[i]; } item.create_by = user.user_id; item.create_time = now; item.item = list_item.Text; RadTextBox box = list_item.FindControl("option_name") as RadTextBox; if (null != box) { } RadioButtonList radbuttonlist = list_item.FindControl("RadioButtonList_Answer") as RadioButtonList; if (null != radbuttonlist) { item.is_answer = int.Parse(radbuttonlist.SelectedValue); } bll_theory_base_item.Add(item); } RadAjaxManager1.ResponseScripts.Add("alert('添加题目成功!');CloseAndRebind();"); } else { RadAjaxManager1.Alert("添加题目失败!"); btnSure.Enabled = true; } } public static int CountSubstringOccurrences(string str, string substr) { int index = 0; int count = 0; while ((index = str.IndexOf(substr, index)) != -1) { count++; index += substr.Length; } return count; } /// /// 旧有添加 /// /// /// protected void btnSure_Click_old(object sender, EventArgs e) { DataService.Model.pro_theory_base model = new DataService.Model.pro_theory_base(); var user = DataService.BLL.admin_user.load_login(); if (string.IsNullOrEmpty(dp_kind.SelectedValue)) { RadAjaxManager1.Alert("题目类型不能为空!"); return; } if (string.IsNullOrEmpty(question_name.Text.Trim())) { RadAjaxManager1.Alert("题干不能为空!"); return; } if (dp_kind.SelectedValue == "简答题") { if (string.IsNullOrEmpty(anwser_jianda.Text.Trim())) { RadAjaxManager1.Alert("简答题答案不能为空!"); return; } } else { if (dp_answer.CheckedItems.Count <= 0) { RadAjaxManager1.Alert("请选择答案!"); return; } } model.id = BasePage.GetId(); model.kind = dp_kind.SelectedValue; model.question = question_name.Text.Trim(); model.create_time = DateTime.Now; model.create_by = user.user_id; model.r1 = r1.Text.Trim(); if (dp_kind.SelectedValue == "简答题") { model.answer = anwser_jianda.Text.Trim(); model.option_a = ""; model.option_b = ""; } else { var array = dp_answer.CheckedItems.Select(s => s.Text).OrderBy(s => s).ToArray(); model.answer = string.Join(",", array); model.option_a = option_a.Text.Trim(); model.option_b = option_b.Text.Trim(); } if (bll_theory_base.Add(model)) { log.write_log("添加题目成功。" + "名称:" + model.question + ",id:" + model.id); RadAjaxManager1.ResponseScripts.Add("alert('添加成功!');CloseAndRebind();"); } else { RadAjaxManager1.Alert("添加失败!"); } } protected void btnRowEdit_Click(object sender, EventArgs e) { var item = sender as LinkButton; var rad_item = item.Parent as RadListBoxItem; //item //var rad_list = item.Parent.Parent; //radlist var text = rad_item.Text; var value = rad_item.Value; var txtbox = rad_item.FindControl("option_name") as RadTextBox; if (txtbox.Text != text) { rad_item.Text = txtbox.Text.Trim(); } } protected void dp_kind_SelectedIndexChanged(object sender, DropDownListEventArgs e) { var kind = dp_kind.SelectedValue; if (kind == "单选题" || kind == "多选题") { //tr_items.Visible = true; //tr_answer.Visible = false; //hid_add.Value = "0"; btnAddItem.Visible = true; answer_write.Text = ""; answer_write.Enabled = false; if (RadListBoxDestination.Items.Count <= 3) { RadListBoxDestination.Items.Clear(); AddNewItem(); AddNewItem(); AddNewItem(); AddNewItem(); RadListBoxDestination.DataBind(); } } else if (kind == "判断题") { //tr_items.Visible = true; //tr_answer.Visible = false; //hid_add.Value = "0"; btnAddItem.Visible = true; answer_write.Text = ""; answer_write.Enabled = false; RadListBoxDestination.Items.Clear(); //AddNewItem(); //AddNewItem(); AddNewItem("正确"); AddNewItem("错误"); RadListBoxDestination.DataBind(); } else { // tr_items.Visible = false; // tr_answer.Visible = true; //hid_add.Value = "1"; btnAddItem.Visible = false; answer_write.Enabled = true; RadListBoxDestination.Items.Clear(); RadListBoxDestination.DataBind(); } span_name.InnerHtml = ""; } public void AddNewItem(string text = "") { Dictionary dic = new Dictionary(); string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var length = alphabet.Length; for (int i = 0; i < 26; i++) { dic.Add(i, alphabet.Substring(i, 1)); } var item = new RadListBoxItem(); // item.Text = txt_additem.Text.Trim(); //$$$$ if (!string.IsNullOrEmpty(text)) { item.Text = text; } else { item.Text = txt_additem.Text.Trim(); } var addItem = new RadListBoxItem(item.Text, item.Text); RadListBoxDestination.Items.Add(addItem); var added_index = addItem.Index; Label labelAZ = addItem.FindControl("labelAZ") as Label; if (null != labelAZ) { if (dic.ContainsKey(added_index)) { labelAZ.Text = dic[added_index]; } } } public void ReorderAZ() { Dictionary dic = new Dictionary(); string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var length = alphabet.Length; for (int i = 0; i < 26; i++) { dic.Add(i, alphabet.Substring(i, 1)); } for (int i = 0; i < RadListBoxDestination.Items.Count; i++) { var item = RadListBoxDestination.Items[i]; var added_index = item.Index; Label labelAZ = item.FindControl("labelAZ") as Label; if (null != labelAZ) { if (dic.ContainsKey(added_index)) { labelAZ.Text = dic[added_index]; } } } } #region public void upload_click(RadAsyncUpload Upload1, Image img) { if (Upload1.UploadedFiles.Count < 1) { RadAjaxManager1.Alert("未选择图片!"); return; } var uploadFile = Upload1.UploadedFiles[0]; var extension = uploadFile.GetExtension(); var extension_list = new string[] { ".jpg", ".png", ".jpeg", ".bmp", ".gif" }; if (!extension_list.Contains(extension)) { RadAjaxManager1.Alert("文件格式错误!"); return; } if (BasePage.SaveFile(uploadFile, Context, out string filePath)) { img.ImageUrl = "~" + filePath; } else { RadAjaxManager1.Alert("上传失败!"); } } public void Remove_Click(Image img) { if (!string.IsNullOrEmpty(img.ImageUrl)) { string path = Server.MapPath(img.ImageUrl); if (File.Exists(path)) { File.Delete(path); } img.ImageUrl = ""; } } #endregion } }