protected void ValidateForm() { // Get the student name var studentName = txtStudentName.Text; // Get the type of student from radio button var studentType = pnlStudentType.Controls.OfType <RadioButton>().FirstOrDefault(r => r.Checked); if (studentType == null) { return; } Student student = null; // Create a new student instance of the appropriate type switch (studentType.Text) { case "Full Time": student = new FullTimeStudent(studentName); break; case "Part Time": student = new PartTimeStudent(studentName); break; case "Co-op": student = new CoopStudent(studentName); break; } var error = false; // Iterate through the checkbox list for (var i = 0; i < _cblAvailableCourses.Items.Count; i++) { // If an item is selected if (!_cblAvailableCourses.Items[i].Selected) { continue; } // Attempt to register the student in the course try { student.addCourse((Course)Helper.GetCourses()[i]); } catch (Exception e) // Handle any exceptions { // Display error lblError.Text = e.Message; lblError.Visible = true; error = true; } } if (!error) { // If validation passes, display the results in a table DisplayStudentInfo(student, studentType.Text); } }
protected void ValidateForm() { // Get the student name var studentName = txtStudentName.Text; // Get the type of student from radio button var studentType = pnlStudentType.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked); if (studentType == null) return; Student student = null; // Create a new student instance of the appropriate type switch (studentType.Text) { case "Full Time" : student = new FullTimeStudent(studentName); break; case "Part Time" : student = new PartTimeStudent(studentName); break; case "Co-op" : student = new CoopStudent(studentName); break; } var error = false; // Iterate through the checkbox list for (var i = 0; i < _cblAvailableCourses.Items.Count; i++) { // If an item is selected if (!_cblAvailableCourses.Items[i].Selected) continue; // Attempt to register the student in the course try { student.addCourse((Course) Helper.GetCourses()[i]); } catch (Exception e) // Handle any exceptions { // Display error lblError.Text = e.Message; lblError.Visible = true; error = true; } } if (!error) { // If validation passes, display the results in a table DisplayStudentInfo(student, studentType.Text); } }
protected void btnSubmit_Click(object sender, EventArgs e) { if (RadioButton1.Checked) { FullTimeStudent ftStudent = new FullTimeStudent(txtName.Text); studentType.Add(ftStudent); // Add the student to list foreach (ListItem chosenCourse in CheckBoxList.Items) { // if a course is selected do the following if (chosenCourse.Selected) { try { // Add each selected course to the student's enrolled courses foreach (Course course in Helper.GetCourses()) { if (course.ToString() == chosenCourse.Text) { ftStudent.addCourse(course); } } } catch (Exception ex) { // Display error message if courses exceed 8 hours lblError.Text = ex.Message; // Continue with rest of code if there is no error return; } } // End of btn selection } // End of foreach // Display confirmation page for full-time student displayConfirmationPage(ftStudent); } // End of condition else if (RadioButton2.Checked) { PartTimeStudent ptStudent = new PartTimeStudent(txtName.Text); studentType.Add(ptStudent); foreach (ListItem chosenCourse in CheckBoxList.Items) { if (chosenCourse.Selected) { try { foreach (Course course in Helper.GetCourses()) { if (course.ToString() == chosenCourse.Text) { ptStudent.addCourse(course); } } } catch (Exception ex) { lblError.Text = ex.Message; return; } } // end of btn selection } // end of foreach // Display confirmation page for part-time student displayConfirmationPage(ptStudent); } // end of condition else if (RadioButton3.Checked) { CoopStudent cpStudent = new CoopStudent(txtName.Text); studentType.Add(cpStudent); foreach (ListItem chosenCourse in CheckBoxList.Items) { if (chosenCourse.Selected) { try { foreach (Course course in Helper.GetCourses()) { if (course.ToString() == chosenCourse.Text) { cpStudent.addCourse(course); } } } catch (Exception ex) { lblError.Text = ex.Message; return; } } // end of btn selection } // End of foreach // Display confirmation page for co-op student displayConfirmationPage(cpStudent); } // End of condition } // End of btn submit method