//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 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"); } }