protected void Page_Load(object sender, EventArgs e) { Object userAnswer = Session["UserAnswer"]; if (userAnswer != null) { Label1.Text = Session["UserAnswer"].ToString(); } //Get Respondent IP here //Get Current Date here Stack <int> nextQuesID = (Stack <int>)Session["nextQueID"]; if (nextQuesID == null) { nextQuesID = new Stack <int>(); nextQuesID.Push(1); followupTracker.Add(1); Session["nextQueID"] = nextQuesID; } if (nextQuesID.Count() > 0) { QuestionID = nextQuesID.Peek(); } Questions question = DbUtlities.getQuestion(QuestionID); //conn.ConnectionString = "Data Source=SQL5017.site4now.net;Initial Catalog=DB_9AB8B7_D19DDA6422;Persist Security Info=True;User ID=DB_9AB8B7_D19DDA6422_admin;Password=hga6jJJZ"; //SqlCommand cmd = new SqlCommand("SELECT * from questions where QuestionID =" + QuestionId, conn); conn.Open(); if (question != null) { string questionText = question.Contents.ToString(); string questionType = question.QuesType.ToString().ToLower(); //just incase, so we dont have to check for textBox vs TextBox vs textbox if ("text".Equals(questionType)) { //TODO load up textbox controls TextBoxUserControl textControl = (TextBoxUserControl)LoadControl("~/UserControls/TextBoxUserControl.ascx"); textControl.ID = "ansTextbox"; textControl.QuestionLabel.Text = questionText; //add it to the ui PlaceHolder1.Controls.Add(textControl); Session["CURRENT_QUESTION_TYPE"] = textControl.ID; } else if ("checkbox".Equals(questionType)) { //TODO load up checkbox controls CheckBoxUserControl checkBoxControl = (CheckBoxUserControl)LoadControl("~/UserControls/CheckBoxUserControl.ascx"); checkBoxControl.ID = "ansCheckbox"; checkBoxControl.QuestionLabel.Text = questionText; Session["CURRENT_QUESTION_TYPE"] = checkBoxControl.ID; //TODO load up checkbox options/choices to add to checkbox control List <QuestionOptions> questionOptions = DbUtlities.getQuestionOptions(QuestionID); foreach (QuestionOptions option in questionOptions) { ListItem newItem = new ListItem(); newItem.Value = option.QueOptId.ToString(); newItem.Text = option.QuesOption; if (option.NextQueID != null) { newItem.Attributes["nextQuestionId"] = option.NextQueID.ToString(); } checkBoxControl.QuestionCheckBoxList.Items.Add(newItem); } //done, add it to placeholder PlaceHolder1.Controls.Add(checkBoxControl); } else if ("radio".Equals(questionType)) { //TODO load up checkbox controls RadioUserControl radioButtonControl = (RadioUserControl)LoadControl("~/UserControls/RadioUserControl.ascx"); radioButtonControl.ID = "ansRadioButton"; radioButtonControl.QuestionLabel.Text = questionText; Session["CURRENT_QUESTION_TYPE"] = radioButtonControl.ID; //TODO load up radio list options/choices to add to checkbox control List <QuestionOptions> questionOptions = DbUtlities.getQuestionOptions(QuestionID); foreach (QuestionOptions option in questionOptions) { ListItem newItem = new ListItem(); newItem.Text = option.QuesOption; newItem.Value = option.QueOptId.ToString(); radioButtonControl.QuestionRadioButtonList.Items.Add(newItem); //add item to option list } //done, add it to placeholder PlaceHolder1.Controls.Add(radioButtonControl); } } conn.Close(); }
//private static int currentQuestionId = 1; protected void Page_Load(object sender, EventArgs e) { /*if (!IsPostBack) * { * Session.Clear(); * }*/ Object userAnser = Session["UserAnswer"]; if (userAnser != null) { Label1.Text = Session["UserAnswer"].ToString(); } //trying to get the attribute from the session Stack <int> followupQuestion = (Stack <int>)Session["FOLLOWUP_ID_LIST"]; if (followupQuestion == null) { followupQuestion = new Stack <int>(); followupQuestion.Push(1); Session["FOLLOWUP_ID_LIST"] = followupQuestion; } int currentQuestionIdInSeeion = followupQuestion.Peek(); //Session["CURRENT_QUESTION_ID"] = currentQuestionIdInSeeion; /*if (Session["CURRENT_QUESTION_ID"] != null) * { * currentQuestionIdInSeeion = (int)Session["CURRENT_QUESTION_ID"]; * } * else { * currentQuestionIdInSeeion = 1; * }*/ /* * // Question text * QuestionText.Text = "What is your name ?"; * * // Question Element * TextBox textBox = new TextBox(); * textBox.ID = "AnswerTxtBox"; * * PlaceHolder1.Controls.Add(textBox);*/ Question question = getNextQuestion(currentQuestionIdInSeeion); if (question != null) { QuestionText.Text = question.text; if (question.type.Equals("text")) { // We gonna create text box question control //TextBox textBox = new TextBox(); TextBoxUserControl textBox = (TextBoxUserControl)LoadControl("~/Controls/TextBoxUserControl.ascx"); textBox.ID = "AnswerTxtBox"; PlaceHolder1.Controls.Add(textBox); Session["CURRENT_QUESTION_TYPE"] = textBox.ID; } else if (question.type.Equals("radio")) { // We gonna create radio button question control RadioButtonUserControl radioBtnQuestion = (RadioButtonUserControl)LoadControl("~/Controls/RadioButtonUserControl.ascx"); radioBtnQuestion.ID = "RadioButton"; //RadioButtonList radioBtnQuestion = new RadioButtonList(); Session["CURRENT_QUESTION_TYPE"] = radioBtnQuestion.ID; List <QuestionOption> questionOptions = getQuestionOptions(currentQuestionIdInSeeion); foreach (QuestionOption option in questionOptions) { ListItem newItem = new ListItem(); newItem.Text = option.text; radioBtnQuestion.getControl().Items.Add(newItem); } PlaceHolder1.Controls.Add(radioBtnQuestion); } else if (question.type.Equals("checkbox")) { // We gonna create check box question control CheckBoxUserControl checkBoxQuestion = (CheckBoxUserControl)LoadControl("~/Controls/CheckBoxUserControl.ascx"); checkBoxQuestion.ID = "CheckBoxButton"; Session["CURRENT_QUESTION_TYPE"] = checkBoxQuestion.ID; //CheckBoxList checkBoxQuestion = new CheckBoxList(); List <QuestionOption> questionOptions = getQuestionOptions(currentQuestionIdInSeeion); foreach (QuestionOption option in questionOptions) { ListItem newItem = new ListItem(); newItem.Value = option.id.ToString(); newItem.Text = option.text; if (option.nextQId != null) { newItem.Attributes["nextQId"] = option.nextQId.ToString(); } checkBoxQuestion.getControl().Items.Add(newItem); } PlaceHolder1.Controls.Add(checkBoxQuestion); } } }
protected void btnNextQue_Click(object sender, EventArgs e) { // Accesss the current question from PlaceHolder Control userControl = PlaceHolder1.FindControl(Session["CURRENT_QUESTION_TYPE"].ToString()); // Access question answers from session List <Answers> questionAnswersInSession = (List <Answers>)Session["Question_ANSWER_LIST"]; if (questionAnswersInSession == null) { // Null means this is the first question so create new question answer list questionAnswersInSession = new List <Answers>(); Session["Question_ANSWER_LIST"] = questionAnswersInSession; } Stack <int> followUpQuestionList = (Stack <int>)Session["nextQueID"]; int QuestionID = followUpQuestionList.Pop(); Questions question = DbUtlities.getQuestion(QuestionID); if (question.NextQueID != null) { //followUpQuestionList.Push((int)question.nextQuestionId); insertNextQuestionId((int)question.NextQueID, followUpQuestionList); } if (userControl is TextBoxUserControl) { TextBoxUserControl textBoxcontr = (TextBoxUserControl)userControl; //Label1.Text = textBoxcontr.getControl().Text; Session["UserAnswer"] = textBoxcontr.QuestionTextBox.Text; System.Diagnostics.Debug.WriteLine("Answer = " + textBoxcontr.QuestionTextBox.Text); Answers answer = new Answers(); answer.Answer = textBoxcontr.QuestionTextBox.Text; answer.QuestionId = QuestionID; questionAnswersInSession.Add(answer); } else if (userControl is CheckBoxUserControl) { CheckBoxUserControl checkBoxcontr = (CheckBoxUserControl)userControl; string answerVar = ""; foreach (ListItem item in checkBoxcontr.QuestionCheckBoxList.Items) { if (item.Selected) { answerVar += item.Text + ","; if (item.Attributes["nextQuestionId"] != null) { //followUpQuestionList.Push(int.Parse(item.Attributes["nextQuestionId"])); insertNextQuestionId(int.Parse(item.Attributes["nextQuestionId"]), followUpQuestionList); } Answers answer = new Answers(); answer.Answer = item.Text; answer.QuestionId = QuestionID; answer.QueOptId = int.Parse(item.Value); questionAnswersInSession.Add(answer); } } Session["UserAnswer"] = answerVar; } else { // Radio button RadioUserControl radioUserControl = (RadioUserControl)userControl; string answerVar = ""; foreach (ListItem item in radioUserControl.QuestionRadioButtonList.Items) { if (item.Selected) { answerVar += item.Text; if (item.Attributes["nextQuestionId"] != null) { //followUpQuestionList.Push(int.Parse(item.Attributes["nextQuestionId"])); insertNextQuestionId(int.Parse(item.Attributes["nextQuestionId"]), followUpQuestionList); } Answers answer = new Answers(); answer.Answer = item.Text; answer.QuestionId = QuestionID; answer.QueOptId = int.Parse(item.Value); questionAnswersInSession.Add(answer); } } Session["UserAnswer"] = answerVar; } // 1. Identify which type of question is current question // 2. Access that question to get answer out from it // Label1.Text = "Test label"; if (followUpQuestionList.Count() > 0) { //Session["CURRENT_QUESTION_ID"] = question.nextQuestionId; Response.Redirect("SurveyPage.aspx"); } else { Response.Redirect("EndofSurvey.aspx"); } }
protected void Button1_Click(object sender, EventArgs e) { // Accesss the current question from PlaceHolder Control userControl = PlaceHolder1.FindControl(Session["CURRENT_QUESTION_TYPE"].ToString()); Stack <int> followupQuestionList = (Stack <int>)Session["FOLLOWUP_ID_LIST"]; int currentQuestionIdInSeeion = ((Stack <int>)Session["FOLLOWUP_ID_LIST"]).Pop(); Question question = getNextQuestion(currentQuestionIdInSeeion); if (question.nextQId != null) { //followupQuestionList.Push((int)question.nextQId); insertNextQuestionId((int)question.nextQId, followupQuestionList); } ListOfQuestion.Add(currentQuestionIdInSeeion.ToString()); if (userControl is TextBoxUserControl) { TextBoxUserControl textBoxcontr = (TextBoxUserControl)userControl; //Label1.Text = textBoxcontr.getControl().Text; Session["UserAnswer"] = textBoxcontr.getControl().Text; System.Diagnostics.Debug.WriteLine("Answer = " + textBoxcontr.getControl().Text); } else if (userControl is CheckBoxUserControl) { CheckBoxUserControl checkBoxcontr = (CheckBoxUserControl)userControl; string answerVar = ""; foreach (ListItem item in checkBoxcontr.getControl().Items) { if (item.Selected) { answerVar += item.Text + ","; if (item.Attributes["nextQId"] != null) { // followupQuestionList.Push(int.Parse(item.Attributes["nextQId"])); insertNextQuestionId((int.Parse(item.Attributes["nextQId"])), followupQuestionList); } } } Session["UserAnswer"] = answerVar; } else { RadioButtonUserControl radioBtnControl = (RadioButtonUserControl)userControl; string answerVar = ""; foreach (ListItem item in radioBtnControl.getControl().Items) { if (item.Selected) { answerVar += item.Value; if (item.Attributes["nextQId"] != null) { insertNextQuestionId((int.Parse(item.Attributes["next_q_id"])), followupQuestionList); } } } Session["UserAnswer"] = answerVar; // Radio button } // 1. Identify which type of question is current question // 2. Access that question to get answer out from it // Label1.Text = "Test label"; if (followupQuestionList.Count > 0) { Object userAnwser = Session["UserAnswer"]; ListOfAnswer.Add(userAnwser.ToString()); Session["CURRENT_QUESTION_ID"] = question.nextQId; Response.Redirect("Survay.aspx"); } else { Object userAnwser = Session["UserAnswer"]; ListOfAnswer.Add(userAnwser.ToString()); Session["ListOfAnswer"] = ListOfAnswer; Response.Redirect("EndSurvey.aspx"); } }