protected void NextButton_Click(object sender, EventArgs e) { // Get the answer from last question and store on session // after pageload and all ui elements re-built dynamically and viewstate copied over this.setSurveyQuestionAnswer(); //Get the next question int currentQuestionSequence = SessionControlUtil.getCurrentQuestionSequence(); Question question = null; SurveyLogicControl surveyLogicControl = new SurveyLogicControl(); // Verify the level and additional questions to show before continuing // the sequence of the main questions if (SessionControlUtil.getCurrentQuestionsLevel() > 1) { int nextQuestionId = SessionControlUtil.getNexQuestionIdFromAdditionalQuestionCurrentLevelList(); question = surveyLogicControl.getQuestionAndAnswersByIQuestionID(nextQuestionId); } else { question = surveyLogicControl.getNextQuestionBySequence(currentQuestionSequence); //If there is not more questions the method surveyLogicControl.getNextQuestionBySequence // will return zero that means there are no more questions on sequence. if (question.QuestionSequence == 0) { // Get the list of questions answered from session List <SurveyQuestionAnswer> surveyQuestionAnswerList = new List <SurveyQuestionAnswer>(); surveyQuestionAnswerList = SessionControlUtil.getSurveyQuestionAnswerList(); // Call the logic control to record the complete pageSurvey in the database surveyLogicControl.insertAnsweredSurvey(surveyQuestionAnswerList); Response.Redirect(AppConstants.pageSurveyComplete); } else { SessionControlUtil.setCurrentQuestionSequence(question.QuestionSequence); } } // Store the next question to be showed on the session // as current question. SessionControlUtil.setCurrentQuestion(question); // Redirect back to pageSurvey.aspx to continue the pageSurvey // on next question. Response.Redirect(AppConstants.pageSurvey); }
/// <summary> /// Read and store the answers of last question and set in the session. /// Get answers options from the pageSurvey page. /// Read the controls type: /// TextBox for text answers /// CheckBox for multiple answers between options /// RadioBox for unique answer between options /// DropDownList for unique answer between options /// </summary> /// <exception cref="Exception">Exception</exception> /// <exception cref="AppPageException">AppPageException</exception> private void setSurveyQuestionAnswer() { try { List <SurveyQuestionAnswer> surveyQuestionAnswerList = SessionControlUtil.getSurveyQuestionAnswerList(); Question currentQuestion = SessionControlUtil.getCurrentQuestion(); ArrayList additionalQuestions = new ArrayList(); List <int> additionalQuestionsList = new List <int>(); if (currentQuestion.SurveyQuestionDomainId == AppConstants.fieldTypeTextId) { TextBoxControl textBoxControl = (TextBoxControl)SurveyCheckboxPlaceHolder.FindControl(AppConstants.controlTextBoxControl); if (textBoxControl != null) { SurveyQuestionAnswer surveyQuestionAnswer = new SurveyQuestionAnswer(); surveyQuestionAnswer.RespondentId = SessionControlUtil.getUserID(); surveyQuestionAnswer.SurveyQuestionId = currentQuestion.QuestionId; if (textBoxControl.QuestionAnswerTextBox.Text.Trim() != null) { surveyQuestionAnswer.AnswerDescription = textBoxControl.QuestionAnswerTextBox.Text.Trim(); } surveyQuestionAnswerList.Add(surveyQuestionAnswer); SessionControlUtil.setSurveyQuestionAnswerList(surveyQuestionAnswerList); } } else if (currentQuestion.SurveyQuestionDomainId == AppConstants.fieldTypeCheckBoxId) { CheckBoxControl checkBoxControl = (CheckBoxControl)SurveyCheckboxPlaceHolder.FindControl(AppConstants.controlCheckBoxControl); if (checkBoxControl != null) { int optionsSelected = 0; foreach (ListItem item in checkBoxControl.QuestionAnswerCheckBoxList.Items) { if (item.Selected == true) { optionsSelected++; SurveyQuestionAnswer surveyQuestionAnswer = new SurveyQuestionAnswer(); surveyQuestionAnswer.RespondentId = SessionControlUtil.getUserID(); surveyQuestionAnswer.SurveyQuestionId = currentQuestion.QuestionId; surveyQuestionAnswer.SurveyAnswerOptionId = AppUtil.convertStringToInt(item.Value); surveyQuestionAnswer.AnswerDescription = null; surveyQuestionAnswerList.Add(surveyQuestionAnswer); SessionControlUtil.setSurveyQuestionAnswerList(surveyQuestionAnswerList); QuestionAnswerOptions answerOption = currentQuestion.AnswerOptionsList.Find( QuestionAnswerOptions => QuestionAnswerOptions.SurveyAnswerOptionId == surveyQuestionAnswer.SurveyAnswerOptionId); if (answerOption.AdditionalQuestion > 0) { additionalQuestions.Add(answerOption.AdditionalQuestion); if (additionalQuestionsList.IndexOf(answerOption.AdditionalQuestion) != 0) { additionalQuestionsList.Add(answerOption.AdditionalQuestion); } } } } // If there is not answer for this question, it is recorded the question with answer null. // So it is possible to verify that respondent do not answer this question, // however it was asked to him if (optionsSelected == 0) { SurveyQuestionAnswer surveyQuestionAnswer = new SurveyQuestionAnswer(); surveyQuestionAnswer.RespondentId = SessionControlUtil.getUserID(); surveyQuestionAnswer.SurveyQuestionId = currentQuestion.QuestionId; surveyQuestionAnswer.AnswerDescription = null; surveyQuestionAnswerList.Add(surveyQuestionAnswer); SessionControlUtil.setSurveyQuestionAnswerList(surveyQuestionAnswerList); } } } else if (currentQuestion.SurveyQuestionDomainId == AppConstants.fieldTypeRadioButtonId) { RadioButtonControl radioButtonControl = (RadioButtonControl)SurveyCheckboxPlaceHolder.FindControl(AppConstants.controlRadioButtonControl); if (radioButtonControl != null) { SurveyQuestionAnswer surveyQuestionAnswer = new SurveyQuestionAnswer(); surveyQuestionAnswer.RespondentId = SessionControlUtil.getUserID(); surveyQuestionAnswer.SurveyQuestionId = currentQuestion.QuestionId; surveyQuestionAnswer.SurveyAnswerOptionId = AppUtil.convertStringToInt(radioButtonControl.QuestionAnswerRadioButtonList.SelectedValue); surveyQuestionAnswer.AnswerDescription = null; QuestionAnswerOptions answerOption = currentQuestion.AnswerOptionsList.Find( QuestionAnswerOptions => QuestionAnswerOptions.SurveyAnswerOptionId == surveyQuestionAnswer.SurveyAnswerOptionId); if (answerOption.AdditionalQuestion > 0) { additionalQuestions.Add(answerOption.AdditionalQuestion); } surveyQuestionAnswerList.Add(surveyQuestionAnswer); SessionControlUtil.setSurveyQuestionAnswerList(surveyQuestionAnswerList); } } else if (currentQuestion.SurveyQuestionDomainId == AppConstants.fieldTypeDropDownListId) { DropDownListControl dropDownListControl = (DropDownListControl)SurveyCheckboxPlaceHolder.FindControl(AppConstants.controlDropDownListControl); if (dropDownListControl != null) { SurveyQuestionAnswer surveyQuestionAnswer = new SurveyQuestionAnswer(); surveyQuestionAnswer.RespondentId = SessionControlUtil.getUserID(); surveyQuestionAnswer.SurveyQuestionId = currentQuestion.QuestionId; surveyQuestionAnswer.SurveyAnswerOptionId = AppUtil.convertStringToInt(dropDownListControl.QuestionAnswerDropDownList.SelectedValue); surveyQuestionAnswer.AnswerDescription = null; QuestionAnswerOptions answerOption = currentQuestion.AnswerOptionsList.Find( QuestionAnswerOptions => QuestionAnswerOptions.SurveyAnswerOptionId == surveyQuestionAnswer.SurveyAnswerOptionId); if (answerOption.AdditionalQuestion > 0) { additionalQuestions.Add(answerOption.AdditionalQuestion); } surveyQuestionAnswerList.Add(surveyQuestionAnswer); SessionControlUtil.setSurveyQuestionAnswerList(surveyQuestionAnswerList); } } else { throw new AppPageException(AppConstants.errorInvalidQuestionDomain + " " + currentQuestion.SurveyQuestionDomainId); } if (additionalQuestionsList.Count > 0) { SessionControlUtil.insertAdditionalQuestionsCurrentLevelList(additionalQuestionsList); } } catch (Exception ex) { //Error log simulated Console.WriteLine(ex.ToString()); Console.WriteLine(ex.GetBaseException().ToString()); throw; } }