Click here to Skip to main content
15,936,903 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

I have one web application in ASP.NET C#.

Currently, I'm working to display Question from the QuestionList to the candidate.

I have created a stored procedure to get a list of Questions, which is global varaible.

Private List<QuestionInfo> _questionList;

 public List<QuestionInfo> QuestionList
 {
      get
      {
          if(_questionList != null)
             return _questionList;
          else
                 _questionList = 
   QuestionDetailsController.Instance.ExamQuestionsGetRandomly(Module.TotalQuestions, QuestionLanguageID, PracticeModuleID).Select(item => item.Question).ToList();
                return _questionList;
    }
}


What I have tried:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindQuestion(_questionNumber);
                Timer1.Enabled = true;
            }

        }


protected void BindQuestion(int nthQuestion)
{
    if (nthQuestion <= QuestionList.Count())
    {
        QuestionInfo CurrentQuestion = QuestionList.ElementAt(nthQuestion);
        if (CurrentQuestion != null)
        {
            lblNoOfQuestions.Text = string.Format("Question {0} of {1}", nthQuestion + 1, ExamQuestion.QuestionList.Count());
            lblQuestionText.Text = CurrentQuestion.QuestionDetails.First().Details.Text.QuestionText;
        }
    }
}


I'm using Ajax UpdatePanel & Timer(to display time remaining).

I have three buttons, like Previous, Pause, Next.

Whenever I click Previous or Next button, the global variable is getting null & on timer_tick event, as well.

How can I stop global variables not to get null, on clicking the Next, previous(buttons) or TimerEvent.

Can anyone please help me.


Thanks in advance
Posted
Updated 21-Nov-17 22:28pm
Comments
phil.o 22-Nov-17 4:04am    
Unrelated to your question, but in the BindQuestion method, your test should be if (nthQuestion > -1 && nthQuestion < QuestionList.Count()). For now, if nthQuestion is equal to the collection count, you would get an index out of bounds exception.
And you could try to make your list a static variable (and property).
abdul subhan mohammed 22-Nov-17 4:15am    
Dear Phil,
Actually, whenever I click next or previous button, i'm getting new questionlist. Where as I want to use the same questionlist but not new one, on every click.

1 solution

Only your control data or things in the ViewState are persisted across postbacks, your local variables are not so on every postback _questionlist will be null as you don't call BindQuestion on postback. You just need to remove the "if" check.

protected void Page_Load(object sender, EventArgs e)
        {
            BindQuestion(_questionNumber);
            if (!IsPostBack)
            {
                Timer1.Enabled = true;
            }
 
        }


You don't show where _questionNumber comes from, but as I said above if you want that value to be remembered you'll need to store it in the ViewState, something like

<pre>protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Timer1.Enabled = true;
            }
            else
            {
                _questionNumber = (int)ViewState["questionNumber"];
            }
            BindQuestion(_questionNumber);

            ViewState["questionNumber"] = _questionNumber; 
        }
 
Share this answer
 
Comments
abdul subhan mohammed 22-Nov-17 4:46am    
on every click on Previous or next, the question list is getting bind every time, as new.
F-ES Sitecore 22-Nov-17 5:31am    
You'll need to split your code such that BindQuestion does the things you don't want to happen on postback such as update the controls so it can be inside the if(!PostBack) check, then put the stuff you do need to happen every time like bind events, set up variables etc in the PageLoad method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900