DataBinder.Eval and an ArrayList type property

M

Mark Jones

I am quite new to ASP and .Net development and I am building a web-based
multiple choice exam application.

The web page displays the questions using a Repeater control and the
answers are nested within the Repeater as RadioButtonLists. Simple
enough I think.

My data consists of a Question table and an Answer table. The Question
table has a QuestionID and a Question field. The Answer table has a
AnswerID field, an Answer field and a QuestionID foreign key (as each
answer belongs to only one question, each question can have one or more
answers).

The code behind file Exam.aspx.vb is as follows:


********************************************

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim allQuestionsAndAnswers As New ArrayList()
allQuestionsAndAnswers = GetAllQuestionsAndAnswers()

QuestionsAndAnswersRepeater.DataSource = allQuestionsAndAnswers
QuestionsAndAnswersRepeater.DataBind()

End Sub

Protected Function GetAllQuestionsAndAnswers() As ArrayList

Dim allQuestionsAndAnswersList As ArrayList = New ArrayList

Dim currentQuestion As QuestionDetails
Dim currentAnswerDetails As AnswerDetails
Dim currentQuestionAndAnswers As QuestionAndAnswers

<... Db connection code snipped ...>

While questionSqlDataReader.Read()
currentQuestion = New QuestionDetails()
currentQuestion.QuestionID = questionSqlDataReader.GetInt32(0)
currentQuestion.Question =
questionSqlDataReader.GetString(1).ToString
currentQuestion.QuestionType =
questionSqlDataReader.GetString(2).ToString

<... Db connection code snipped ...>

Dim tempAnswerDetailsList As New ArrayList

While answerSqlDataReader.Read()
currentAnswerDetails = New AnswerDetails()
currentAnswerDetails.AnswerID = answerSqlDataReader.GetInt32(0)
currentAnswerDetails.Answer = answerSqlDataReader.GetString(1)
tempAnswerDetailsList.Add(currentAnswerDetails)
End While

currentQuestionAndAnswers = New QuestionAndAnswers
currentQuestionAndAnswers.QuestionDetails = currentQuestion
currentQuestionAndAnswers.AnswerDetailsList = tempAnswerDetailsList

allQuestionsAndAnswersList.Add(currentQuestionAndAnswers)

End While

Return allQuestionsAndAnswersList


End Function


' Object to hold Question and related Answers

Protected Class QuestionAndAnswers

Private myQuestionDetails As QuestionDetails
Private myAnswers As ArrayList ' of AnswerHelper objects

Public Property QuestionDetails()
Get
Return myQuestionDetails
End Get
Set(ByVal value)
myQuestionDetails = value
End Set
End Property

Public Property AnswerDetailsList() As ArrayList
Get
Return myAnswers
End Get
Set(ByVal value As ArrayList)
myAnswers = value
End Set
End Property
End Class

'Object to hold question details

Protected Class QuestionDetails

Private myQuestionID As Integer
Private myQuestion As String
Private myQuestionType As String

Public Property QuestionID() As String
Get
Return myQuestionID
End Get
Set(ByVal value As String)
myQuestionID = value
End Set
End Property

Public Property Question() As String
Get
Return myQuestion
End Get
Set(ByVal value As String)
myQuestion = value
End Set
End Property

Public Property QuestionType() As String
Get
Return myQuestionType
End Get
Set(ByVal value As String)
myQuestionType = value
End Set
End Property
End Class

'Object to hold answer details

Protected Class AnswerDetails

Private myAnswerID As Integer
Private myAnswer As String

Public Property AnswerID() As Integer
Get
Return myAnswerID
End Get
Set(ByVal value As Integer)
myAnswerID = value
End Set
End Property

Public Property Answer() As String
Get
Return myAnswer
End Get
Set(ByVal value As String)
myAnswer = value
End Set
End Property
End Class

*********************************************

The relevant part of the 'Exam.aspx' web page is:

<asp:Repeater ID="QuestionsAndAnswersRepeater" runat="server">
<HeaderTemplate>
<ol>
</HeaderTemplate>
<ItemTemplate>
<li>
<p>
<asp:Label runat="server" Font-Bold="true"
Font-Italic="true" Font-Size="Large" Text='<%#
DataBinder.Eval(Container.DataItem, "QuestionDetails.Question") %>'>
</asp:Label>
</p>
<p>
<asp:RadioButtonList runat="server" DataSource='<%#
DataBinder.Eval(Container.DataItem, "AnswerDetailsList") %>'>
</asp:RadioButtonList>
</p>
</li>
</ItemTemplate>
<FooterTemplate>
</ol>
<p>End of questions</p>
</FooterTemplate>
</asp:Repeater>


*************************************************


This works up to a point. The problem I have is with the line:

<asp:RadioButtonList runat="server" DataSource='<%#
DataBinder.Eval(Container.DataItem, "AnswerDetailsList") %>

This page when running currently looks something like this:

1. What is the average temperature of the sun?

o Exam+AnswerDetails o Exam+AnswerDetails o Exam+AnswerDetails

2. Who is the Prime Minister of the United Kingdom?

o Exam+AnswerDetails o Exam+AnswerDetails o Exam+AnswerDetails

etc. etc....

Now this is what I expect as AnswerDetailsList is an ArrayList object
and is being displayed via Object.ToString but not what I want. I
actually want to display the Answer property within the AnswerDetails
object but I don't know make the DataBinder.Eval(container, expression)
method navigate to that property through an ArrayList object.

I may well need to redesign my objects to make the AnswerDetails.Answer
property more accessible to the server control in the page but I feel
the way I have done it is fairly logical and I'd rather not if there is
simpler solution in the ASP file.

Feel free to point out any other flaws in my code/design.

Thanks very much.
 
G

Guest

Hi Mark,

You shoud drectly use AnswerDetailsList for the DataSource of
RadioButtonList rather than via Container.DataItem. It's like:

<asp:RadioButtonList runat="server" DataSource='<%# AnswerDetailsList %>'>
</asp:RadioButtonList>

HTH

Elton Wang
 
M

Mark Jones

Elton said:
Hi Mark,

You shoud drectly use AnswerDetailsList for the DataSource of
RadioButtonList rather than via Container.DataItem. It's like:

<asp:RadioButtonList runat="server" DataSource='<%# AnswerDetailsList %>'>
</asp:RadioButtonList>

Thanks, Elton. It tells me "AnswerDetailsList is not declared".

In the RadioButtonList I actually need to display the Answer property
from each AnswerDetails object in the AnswerDetailsList but I'm not sure
how to get to it or if it is even possible (or whether I'm just making
things too complicated!).

Thanks
 
G

Guest

Hi Mark,

AnswerDetailsList should be a public property/method in page (or a static
method/ property in a public class). So you can call it directly.

HTH
 

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

Similar Threads

DataBinder.Eval and an ArrayList type property 0
Property 1
Property 1
Property 0
Control Property. 3
Arraylist IndexOf Structure 0
passing an arrayList to web service 8
Arraylist only display last item 2

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top