473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

DataBinder.Eval and an ArrayList type property

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(currentQuestionAndA nswers)

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.

--
Mark

majg12uk at yahoo(nospamm) dot co dot uk
Jan 1 '06 #1
3 2533
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

"Mark Jones" wrote:
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(currentQuestionAndA nswers)

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.

--
Mark

majg12uk at yahoo(nospamm) dot co dot uk

Jan 1 '06 #2
Elton W wrote:
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
--
Mark
Jan 2 '06 #3
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
"Mark Jones" wrote:
Elton W wrote:
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
--
Mark

Jan 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michelle Keys | last post by:
Subject: DataBinder.Eval Error! Server Error in '/MSPOS' Application. ------------------------------------------------------------------------ -------- DataBinder.Eval:...
2
by: Tom Lee | last post by:
Hi all, I have the following problem and I cannot solve it. If anyone can help me solve this problem. I use the following code <%#DataBinder.Eval(Container.DataItem, "Property")%> to display...
5
by: bg | last post by:
Hi! How do I check if "date" exists before using that code? I've built a RSSreader and sometimes there's a date in it and sometimes not. How can I check if it exists to avoid crash...
2
by: JohnZing | last post by:
Hi, Should I use <%# Container.DataItem("Author")%> or <%# DataBinder.Eval(Container.DataItem,"Author") %> What's the difference between both? Thank You
3
by: Eric Newton | last post by:
Given databinding an array of System.Version types: Given that "SomeObject" type has a Version property: public class SomeObject { public Version Version { get; } public string Description {...
1
by: Eric Newton | last post by:
Given databinding an array of System.Version types: Given that "SomeObject" type has a Version property: public class SomeObject { public Version Version { get; } public string Description {...
1
by: morisse caglis via .NET 247 | last post by:
Hi, I would like to list out all files in a directory. To do that, I created a CFileInfo class with string members (strName, strDate, strLength...) and I fill an ArrayList with those object. It...
2
by: Oleg Ogurok | last post by:
Hi all, I have a DatePicker ASP.NET control that allows users to enter date (e.g. a calendar control). The control has the following property public DateTime Value { get; set; } I've placed...
4
by: CharlesA | last post by:
a general question... I'm a bit mystified by Databinder.eval(object, Colname, ) if controls can be bound to their individual data sources within the load event of the page... why would we ever...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.