问题
Hii,
I need to develop an online quiz website that would be having MCQs. I would want to have one question appearing per page with a Numeric Pager so that the user can go back and forth.
I tried using the FormView for displaying the questions and RadioButtons. I created a class QANS that would hold the answer selected by the user for the questions that he did answer so that I can later sum up the total Score.
Now, the problem I'm facing is as below:
Let the user select a RadioButton, say R, on a PageIndex, say I, and then go to some other page, say K. When the user returns back to page I, none of the RadioButtons is selected. I tried adding code for setting the Checked property of the RadioButton true in the Page_Load(), but it didn't help. I also tried the same with the PageIndexChanged and PageIndexChanging event, but the RadioButton doesn't get checked.
The RadioButton that was selected for a particular question has been stored and I am able to print which one it was using Response.Write() but I'm not able to keep the RadioButton Checked. How shall this be done?
回答1:
You should be able to make the radio button checked in the
protected override void OnPreRender(EventArgs e)
event of the page lifecycle
回答2:
Have you considered using the .Net wizard control for this? The ASP.Net 2.0 Wizard Control
回答3:
You can save the state of the selected controls in session and repopulate them after the round trip.
It's odd that it's losing state after the postback. Is there any crazy code in you on_load or pageIndexChanging or anything?
回答4:
From what I understand, you have to persist the checked state of the radiobutton across the pageindexes. You can use ViewState for that as that can preserve data across postbacks for a page.
something like this:-
// Define a list of question ids attempted List questionIdsAttempted;
//Add the List to the ViewState if(ViewState["QuestionIdsAttempted"]!=null) { questionIdsAttempted= new List() ViewState["QuestionIdsAttempted"] = questionIdsAttempted }
// Add the question id to the list when when the user attempts a question questionIdsAttempted.Add(qId);
// Add the list back to the viewstate ViewState["QuestionIdsAttempted"] = questionIdsAttempted
And in the page load in checked attribute of the radiobuttons , you can call a function with the current question id and the function will get the list from the viewstate and returns true or false based on if the id exists in the list.
Ofcourse, you need to check the nullability of the viewstate in various places based on the flow of the program.
Let me know
来源:https://stackoverflow.com/questions/2438219/online-quiz-using-asp-dot-net