protected void BtnPost_Click(object sender, EventArgs e) { int temp; double cr; if (IsOK()) { using (var db = new CstwebEntities()) { if (Session["lessonID"].ToString() != "0") { try { temp = Convert.ToInt32(Session["lessonID"].ToString()); var del = db.lesrelation.Where(a => a.lesson == temp).ToList(); lesrelation les1; try { les1 = del[0]; db.lesrelation.Remove(les1); db.SaveChanges(); } catch { } try { les1 = del[1]; db.lesrelation.Remove(les1); db.SaveChanges(); } catch { } try { les1 = del[2]; db.lesrelation.Remove(les1); db.SaveChanges(); } catch { } lesson les = db.lesson.First(a => a.id == temp); les.classname = TxtLessonName.Text.Trim(); cr = Convert.ToDouble(TxtCredits.Text.Trim()); les.credits = cr; temp = Convert.ToInt32(TxtTeach.Text.Trim()); les.teach = temp; temp = Convert.ToInt32(TxtExp.Text.Trim()); les.experiment = temp; temp = Convert.ToInt32(DDLClass.SelectedValue); les.lesscla = temp; temp = Convert.ToInt32(Session["lessonID"].ToString()); if (LblFilename.Text != "") { les.lesfile = LblFilename.Text; } string tempstr = Server.HtmlDecode(myEditor.InnerHtml); les.etc = tempstr.Trim(); les1 = new lesrelation(); les1.lesson = temp; db.SaveChanges(); if (Label1.Text != "0") { temp = Convert.ToInt32(Label1.Text); les1.firstlesson = temp; db.lesrelation.Add(les1); db.SaveChanges(); } if (Label2.Text != "0") { temp = Convert.ToInt32(Label2.Text); les1.firstlesson = temp; db.lesrelation.Add(les1); db.SaveChanges(); } if (Label3.Text != "0") { temp = Convert.ToInt32(Label3.Text); les1.firstlesson = temp; db.lesrelation.Add(les1); db.SaveChanges(); } Session["FList"] = null; Session["Less"] = null; Session["First1"] = null; Session["First2"] = null; Session["First3"] = null; Session["lessonID"] = null; Response.Write("<script language=javascript>alert('修改成功');window.location = 'lessons.aspx';</script>"); } catch { Response.Write("<script>alert('修改失败')</script>"); } } else { try { lesson les = new lesson(); lesrelation les1 = new lesrelation(); les.classname = TxtLessonName.Text.Trim(); cr = Convert.ToDouble(TxtCredits.Text.Trim()); les.credits = cr; temp = Convert.ToInt32(TxtTeach.Text.Trim()); les.teach = temp; temp = Convert.ToInt32(TxtExp.Text.Trim()); les.experiment = temp; temp = Convert.ToInt32(DDLClass.SelectedValue); les.lesscla = temp; temp = Convert.ToInt32(Session["lessonID"].ToString()); if (LblFilename.Text != "") { les.lesfile = LblFilename.Text; } string tempstr = Server.HtmlDecode(myEditor.InnerHtml); les.etc = tempstr.Trim(); db.lesson.Add(les); db.SaveChanges(); temp = db.lesson.First(a => a.classname == TxtLessonName.Text.Trim()).id; les1.lesson = temp; if (Label1.Text != "0") { temp = Convert.ToInt32(Label1.Text); les1.firstlesson = temp; db.lesrelation.Add(les1); db.SaveChanges(); } if (Label2.Text != "0") { temp = Convert.ToInt32(Label2.Text); les1.firstlesson = temp; db.lesrelation.Add(les1); db.SaveChanges(); } if (Label3.Text != "0") { temp = Convert.ToInt32(Label3.Text); les1.firstlesson = temp; db.lesrelation.Add(les1); db.SaveChanges(); } Session["FList"] = null; Session["Less"] = null; Session["First1"] = null; Session["First2"] = null; Session["First3"] = null; Session["lessonID"] = null; Response.Write("<script language=javascript>alert('添加成功');window.location = 'lessons.aspx';</script>"); } catch { Response.Write("<script>alert('添加失败')</script>"); } } } } }