repeater

G

Guest

Hi,

I am working on a questionnaire. I have displayed a questionnaire using a
repeater control. The itemtemplate is as below (quite cut down):

<ItemTemplate>
<tr><td>
<%# DataBinder.Eval(Container.DataItem, "question")%>
</td></tr>
<tr><td>
<asp:CheckBox id="chkboxA" runat="server" Checked="false" visible='<%#
DataBinder.Eval(Container.DataItem, "choiceA").ToString() != "" %>' >
</asp:CheckBox>
<%# DataBinder.Eval(Container.DataItem, "choiceA")%>
</td></tr>
<tr><td>
<asp:CheckBox id="chkboxB" runat="server" Checked="false" visible='<%#
DataBinder.Eval(Container.DataItem, "choiceB").ToString() != "" %>' >
</asp:CheckBox>
<%# DataBinder.Eval(Container.DataItem, "choiceB")%>
</td></tr> ....

I want to display each question (there are 50) questions on a separate
webpage, one after another. My question is:

1) How do I structure the webpage .aspx page to display 50 consecutive
questions ? Do I have to bind the repeater control to the dataset each time?
The questions that I want to display is taken from a composite table each
time. There has to be a better way than that.

2) how do I store the user's answers eg. Q1 has 4 choices, which the user
has checked. Do I use an array ? If yes, then how do I retrieve the data from
the repeater control into the array and then retrieve the data from the array
into the dataset to store into the db ?

All suggestions are welcomed. Thanks.
Andrew.
 
G

Grant Merwitz

What i would do, is pull all the questions down into a 2 dimensional array
list.
The first value will contain the question, and the second initially empty
will be ready for the answer.

To be efficient, this arraylist will be pulled from the Cache and not the DB
everytime.

Now you can save this arraylist into a session/viewstate, to keep it
persistant among your pages.
As its sessioned, each user will have there own copy of the arraylist pulled
from the Cache

As you only want to display one at a time, you needen't use a repeater.
You can just create a Label and textbox for the question and answer. (maybe
an invisible literal/label to store the questions index number in the array)
There can also be a next and previous button, which once clicked will ave
the answer from the Textbox into the array at its relevant index number.
Then you can check if its the last question, if not display the next
question by incrementing your arraylist by 1 (setting the answer box to
blank or if already answered the answer for that question)

You could also maybe use a repeater to allow the user to jump to questions
easier and view answers they've already done - like a question overview

HTH
 
G

Guest

Thanks for your reply.

The reason why I used a repeater to display my questions is because my
questions are multiple choice with checkboxes.And each question might have a
different number of choices. The repeater allows for displaying a varied
number of choices for each question.

I think to use the arraylist is a good idea, because I just need to store
the questionId and the studentAnswer for each question. But at the moment, I
do not know how to store it in a Cache ??

I do have a previous button, which once clicked should display the previous
question WITH the checked answers from the array at its relevant index
number. Does anyone know how I can do this?

TIA. Any sample code would be of great help.
 
G

Grant Merwitz

CACHING

Regarding Cache, you would not store the answers in the Cache.
The Cache will be like a blank questionaire,
When the questionaire starts, you pull that from the Cache and keep a
session copy of it for the current user.
This will avoid a trip to the Database for every user, and every question.

Caching is pretty simple. a one liner like so:
Cache.Insert("KEY NAME", "VALUE YOU WANT TO SAVE");
and retrieval like
ArrayList arr = (ArrayList)Cache["KEY NAME"];

You would typically use this by,
1)pulling your questionairre from your database (save in a dataset or
array), and then cache it.
2)When you need to use this, try retrieve from the Cache, checking it is not
null, and if it is, do step 1 again

PREVIOUS AND NEXT

If you use an array (not just to save the answers, but for the questions as
well), you can easily go to next and previous buttons based on the index
number.
Say this is the 3rd question, you save the number 3 in a hidden on the page.
Then when the user clicks previous, you take this 3, decrement it by 1, and
then show question 2.
Vice Versa if they clicked Next.

STORING MULTIPLE ANSWERS IN AN ARRAY

Now storing a multiple choice questionairre in an array with a variable
number of choices its not so straight forward.
But there are ways around it.
Like, You can setup your array to be as wide as the most the question with
the most choices, and then leave blank questions that don't exist.
e.g.

Qid Answer1 Answer2 Answer3
1 1 2 3
2 1 2 -

So for question 2, you could see that there are only 2 options.
Then you could also make one final column to store the answer

Qid Answer1 Answer2 Answer3 StudentAnswer
1 1 2 3 2

OR

you could use a 2 arrays, the first being questions, and the other being
options.
This would work much like a DataBase having a parent table and child tables.
You could then also store the answers for the given user in the Questions
Array

Questions Array
Qid Student Answer
1 1
2 3

Options Array
Qid OptionId
1 1
1 2
1 3
2 1
2 2
2 3
2 4

(bear in mind, there would also be text here not just id's so you can
display them to the user as well)

FINALLY

You can also use a DataSet with related DataTables
DataTables are very much like arrays but with much more functionality and
overhead.
But arrays are more efficient and therefore quicker.

Anyways, i;ve gone on enough for now. Better get back to work before i lose
my job :)

Good luck and i hope my comments are helping
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top