| re: DataBinder.Eval and an ArrayList type property
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:
[color=blue]
> 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
>[/color] |