Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.net中的按钮事件之前调用页面加载_C#_Asp.net_Event Handling_Controls_Postback - Fatal编程技术网

C# 在.net中的按钮事件之前调用页面加载

C# 在.net中的按钮事件之前调用页面加载,c#,asp.net,event-handling,controls,postback,C#,Asp.net,Event Handling,Controls,Postback,我有以下代码: protected void Page_Load(object sender, EventArgs e) { //get questions List<Question> questions = Question.GetQuestionsForTesting(); //for each question foreach (Question currQuestion in questions)

我有以下代码:

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
        }
    }

当我单击submitAnswers按钮时,页面加载被调用,然后按钮事件被调用。我需要一些方法来检查提交的答案,但我目前无法做到这一点,因为我的控件与问题及其答案一起消失在回发中。因此,当事件方法最终被调用时,我只是检查新问题的答案,没有用户输入


我已经看了很多其他的问题,但是我找不到类似的问题。

你可以在这里找到答案:。但是你应该使用!Page.IsPostback检查页面加载事件

您可以在页面周期的几个点上重新构造操作。您可以在上阅读更多信息

在您的情况下,我建议添加页面预渲染步骤。将移动当前页面和加载操作添加到其中

所以它会像:

步骤1:调用Page_Load,您可以在其中执行常规操作 步骤2:处理按钮点击 第3步:调用Page_PreRender,您可以在其中加载带有问题的动态控件。此时,您可以从数据库重新加载数据。
<asp:Panel runat="server" ID="questionAnswerPanel"></asp:Panel>
<asp:Button runat="server" ID="submitAnswers" OnClick="submitAnswers_Click" Text="Submit Answers"/>