protected void btnAdd_Click(object sender, EventArgs e) { name = txtName.Text;//to store the name studentTpye = dpdStudentType.SelectedValue; txtName.Text = ""; //to make the button empty when clicked dpdStudentType.SelectedIndex = -1; //to make the studentType return to default if (name != "" && studentTpye != "") { Student addStudent = null; //create a null variable switch (studentTpye) // get student type { case "1": addStudent = new Fulltime(name); break; case "2": addStudent = new ParttimeStudent(name); break; case "3": addStudent = new CoopStudent(name); break; } //Student addStudent = new Student(name); registeredStudent.Add(addStudent); Session["students"] = registeredStudent; //store objects into session["students"] } if (registeredStudent.Count > 0) { warn.Attributes.Add("style", "display:none"); } else { warn.Attributes.Remove("style"); } }
protected void Main(object sender, EventArgs e) { studentCoursesLabel.Text = ""; alert1.Text = ""; if (!rfvStudent.IsValid) { return; } List <Course> selectedCourses = new List <Course>(); for (int i = 0; i < availableCourses.Count; i++) { Course course = availableCourses[i] as Course; if (CheckBoxList1.Items[i].Selected) { Course selectedCourse = availableCourses[i]; selectedCourses.Add(selectedCourse); weeklyHours += course.WeeklyHours; } } if (selectedCourses.Count == 0) { alert1.Text = "Select at least 1 course"; return; } try { registeredStudents = (List <Student>)Session["students"];//make a new session for students for (int i = 0; i < registeredStudents.Count; i++) { Student student = registeredStudents[i]; // index[i] is important if (student.Id == drpStudent.SelectedValue) { if (drpStudent.SelectedItem.Text.Contains("Coop")) { CoopStudent coopStudent = (CoopStudent)student;//connect this student with CoopStudent class coopStudent.RegisterCourses(selectedCourses); registeredStudents[i] = coopStudent; } else if (drpStudent.SelectedItem.Text.Contains("Full")) { Fulltime fulltimeStudent = student as Fulltime;//casting to fulltime student fulltimeStudent.RegisterCourses(selectedCourses); registeredStudents[i] = fulltimeStudent; } else if (drpStudent.SelectedItem.Text.Contains("Part")) { ParttimeStudent parttimeStudent = (ParttimeStudent)student; parttimeStudent.RegisterCourses(selectedCourses); registeredStudents[i] = parttimeStudent; } Session["students"] = registeredStudents; } } studentCoursesLabel.Text = string.Format("Selected student has registered {0} course(s), {1} hours weekly.", selectedCourses.Count, weeklyHours); } catch (Exception ex) { alert1.Text = ex.Message; } }