M
Mark Jones
I am building a multiple choice exam application and trying to extract
values from child controls contained inside a Repeater. I am using a
custom control (QuestionLabel) to display the question text and question
ID and a RadioButtonList for the answers with a DataValueField property
as the answer ID. I need to store in the database both the question ID
and the answer ID.
My code should enumerate over the child controls of the Repeater,
evaluate whether it is of the correct type (e.g. a QuestionLabel or
RadioButtonList) and then store the appropriate value in the database.
However, when stepping through with the debugger I can see that the
statements inside the conditional clauses within the While loop are
never executed.
Can anybody tell me why or if there is a better way of doing this than
the one I have chosen?
I appreciate the help.
Mark
*************************
Exam.aspx.vb:-
Protected Sub submitButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles submitButton.Click
If Page.IsValid Then
If ExamRepeater.HasControls() Then
Dim controls As ControlCollection = ExamRepeater.Controls
Dim controlEnumerator As IEnumerator = controls.GetEnumerator
Dim currentQuestionID As Integer
Dim currentAnswerID As Integer
' Create Sql objects here
While controlEnumerator.MoveNext
If TypeOf controlEnumerator.Current Is QuestionLabel Then
Dim currentQuestionLabel As QuestionLabel =
CType(controlEnumerator.Current, QuestionLabel)
currentQuestionID =
CInt(currentQuestionLabel.QuestionID)
ElseIf TypeOf controlEnumerator.Current Is
RadioButtonList Then
Dim currentRadioButtonList As RadioButtonList =
CType(controlEnumerator.Current, RadioButtonList)
currentAnswerID =
CInt(currentRadioButtonList.DataValueField)
End If
' Store currentQuestionID and currentAnswerID in the
database here
End While
End If
End If
End Sub
************************
Exam.aspx:-
....
<asp:Repeater ID="ExamRepeater" runat="server">
<HeaderTemplate>
<asp:ValidationSummary ID="ValidationSummary" runat="server"
DisplayMode="BulletList"
Font-Bold="true" Font-Italic="true" HeaderText="You have
missed some questions (marked *)." ShowMessageBox="true" />
<ol>
</HeaderTemplate>
<ItemTemplate>
<li>
<p>
<maj:QuestionLabel ID="QuestionLabel" runat="server"
Font-Bold="true" Font-Italic="true" Font-Size="Large"
Text='<%# DataBinder.Eval(Container.DataItem,
"QuestionDetails.Question") %>'
QuestionID ='<%#
DataBinder.Eval(Container.DataItem, "QuestionDetails.QuestionID") %>'>
</maj:QuestionLabel>
<asp:RequiredFieldValidator
ID="AnswerRadioButtonListRequiredFieldValidator" runat="server"
ControlToValidate="AnswerRadioButtonList"
SetFocusOnError="true" Font-Size="X-Large"> *</asp:RequiredFieldValidator>
</p>
<p>
<asp:RadioButtonList ID="AnswerRadioButtonList"
runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem,
"AnswerDetailsList") %>'
DataTextField="Answer" DataValueField="AnswerID">
</asp:RadioButtonList>
</p>
</li>
</ItemTemplate>
<FooterTemplate>
</ol>
<p style="font-weight: bold;">END OF QUESTIONS</p>
</FooterTemplate>
</asp:Repeater>
....
values from child controls contained inside a Repeater. I am using a
custom control (QuestionLabel) to display the question text and question
ID and a RadioButtonList for the answers with a DataValueField property
as the answer ID. I need to store in the database both the question ID
and the answer ID.
My code should enumerate over the child controls of the Repeater,
evaluate whether it is of the correct type (e.g. a QuestionLabel or
RadioButtonList) and then store the appropriate value in the database.
However, when stepping through with the debugger I can see that the
statements inside the conditional clauses within the While loop are
never executed.
Can anybody tell me why or if there is a better way of doing this than
the one I have chosen?
I appreciate the help.
Mark
*************************
Exam.aspx.vb:-
Protected Sub submitButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles submitButton.Click
If Page.IsValid Then
If ExamRepeater.HasControls() Then
Dim controls As ControlCollection = ExamRepeater.Controls
Dim controlEnumerator As IEnumerator = controls.GetEnumerator
Dim currentQuestionID As Integer
Dim currentAnswerID As Integer
' Create Sql objects here
While controlEnumerator.MoveNext
If TypeOf controlEnumerator.Current Is QuestionLabel Then
Dim currentQuestionLabel As QuestionLabel =
CType(controlEnumerator.Current, QuestionLabel)
currentQuestionID =
CInt(currentQuestionLabel.QuestionID)
ElseIf TypeOf controlEnumerator.Current Is
RadioButtonList Then
Dim currentRadioButtonList As RadioButtonList =
CType(controlEnumerator.Current, RadioButtonList)
currentAnswerID =
CInt(currentRadioButtonList.DataValueField)
End If
' Store currentQuestionID and currentAnswerID in the
database here
End While
End If
End If
End Sub
************************
Exam.aspx:-
....
<asp:Repeater ID="ExamRepeater" runat="server">
<HeaderTemplate>
<asp:ValidationSummary ID="ValidationSummary" runat="server"
DisplayMode="BulletList"
Font-Bold="true" Font-Italic="true" HeaderText="You have
missed some questions (marked *)." ShowMessageBox="true" />
<ol>
</HeaderTemplate>
<ItemTemplate>
<li>
<p>
<maj:QuestionLabel ID="QuestionLabel" runat="server"
Font-Bold="true" Font-Italic="true" Font-Size="Large"
Text='<%# DataBinder.Eval(Container.DataItem,
"QuestionDetails.Question") %>'
QuestionID ='<%#
DataBinder.Eval(Container.DataItem, "QuestionDetails.QuestionID") %>'>
</maj:QuestionLabel>
<asp:RequiredFieldValidator
ID="AnswerRadioButtonListRequiredFieldValidator" runat="server"
ControlToValidate="AnswerRadioButtonList"
SetFocusOnError="true" Font-Size="X-Large"> *</asp:RequiredFieldValidator>
</p>
<p>
<asp:RadioButtonList ID="AnswerRadioButtonList"
runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem,
"AnswerDetailsList") %>'
DataTextField="Answer" DataValueField="AnswerID">
</asp:RadioButtonList>
</p>
</li>
</ItemTemplate>
<FooterTemplate>
</ol>
<p style="font-weight: bold;">END OF QUESTIONS</p>
</FooterTemplate>
</asp:Repeater>
....