2

I have the following code:

protected void Page_Load(object sender, EventArgs e) { //get questions List<Question> questions = Question.GetQuestionsForTesting(); //for each question foreach (Question currQuestion in questions) { //display the questionAnswer control QuestionAnswer newControl = (QuestionAnswer)LoadControl("~/Account/QuestionAnswer.ascx"); newControl.newQuestion = currQuestion; questionAnswerPanel.Controls.Add(newControl); } } protected void submitAnswers_Click(object sender, EventArgs e) { //for each control foreach (QuestionAnswer questionAnswerControl in questionAnswerPanel.Controls) { //get ID of question and correct/incorrect Tuple<Boolean, int> correctIncorrectANDquestionID = questionAnswerControl.isAnswerCorrect(); //save in DB } } 

and

<asp:Panel runat="server" ID="questionAnswerPanel"></asp:Panel> <asp:Button runat="server" ID="submitAnswers" OnClick="submitAnswers_Click" Text="Submit Answers"/> 

When I click the submitAnswers button the page load gets called and then the button event. I need some way of checking the answers submitted but I am not able to do this currently as my controls disappear on the postback along with the questions and their answers. So when the event method finally gets called I am just checking answers from new questions with no user input.

I have looked at various other questions but I can't find a similar question.

2 Answers 2

2

You can find the answer here: Which control caused the postback?. But you should use !Page.IsPostback check in the Page_Load Event.

Sign up to request clarification or add additional context in comments.

Comments

2

You can restructure your actions on several point in the page cycle. You can read more info on ASP.NET page life cycle explanation.

In your situation I would recommend to add the Page_PreRender step. Add move your current Page_Load actions to it.

So it will be like:

  • Step 1: Page_Load is called, where you can do general actions
  • Step 2: Button Clicks are processed
  • Step 3: Page_PreRender is called, where you can load the dynamic controls with questions. At this point you can reload the data from database.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.