NET中的按钮事件之前调用页面加载 [英] Page Load being called before Button Event in .net

查看:59
本文介绍了NET中的按钮事件之前调用页面加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

 受保护的空白Page_Load(对象发送者,EventArgs e){//获取问题列表<问题>问题= Question.GetQuestionsForTesting();//对于每个问题foreach(问题中的currQuestion问题){//显示问题答案控件QuestionAnswer newControl =(QuestionAnswer)LoadControl(〜/Account/QuestionAnswer.ascx");newControl.newQuestion = currQuestion;questionAnswerPanel.Controls.Add(newControl);}}受保护的void SubmitAnswers_Click(对象发送者,EventArgs e){//对于每个控件foreach(QuestionAnswer问题AnswerControl有问题AnswerPanel.Controls){//获取问题的ID并更正/不正确元组< Boolean,int>correctIncorrectANDquestionID = questionAnswerControl.isAnswerCorrect();//保存在数据库中}} 

 < asp:Panel runat =服务器" ID ="questionAnswerPanel"></asp:Panel>< asp:按钮runat ="server" ID ="submitAnswers" OnClick ="submitAnswers_Click" Text =提交答案"/> 

当我单击submitAnswers按钮时,将调用页面加载,然后调用按钮事件.我需要某种方式来检查提交的答案,但是由于控件和问题及其答案一起消失在回发中,所以我目前无法做到这一点.因此,当事件方法最终被调用时,我只是在没有用户输入的情况下检查新问题的答案.

我看过其他各种问题,但找不到类似的问题.

解决方案

您可以在页面周期中的多个点重新构造操作.您可以在 ASP.NET页面生命周期说明中了解更多信息..>

根据您的情况,我建议添加Page_PreRender步骤.添加将当前的Page_Load操作移至其中.

因此它将是:

  • 第1步:调用Page_Load,您可以在其中执行常规操作
  • 第2步:处理按钮点击
  • 第3步:调用Page_PreRender,在其中可以向动态控件加载问题.此时,您可以从数据库中重新加载数据.

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.

解决方案

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.

这篇关于NET中的按钮事件之前调用页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆