Extracting values from child controls of a Repeater

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>

....
 
P

Phillip Williams

Hi Mark,

The repeater contains a collection of type RepeaterItem (therefore the type
checks that you placed will all fail because you are checking for the
controls that are within the repeater items). You should wite something like
this instead:

Dim ri As RepeaterItem
For Each ri In Repeater1.Items
Dim cntrl As Control
For Each cntrl In ri.Controls
If TypeOf cntrl Is QuestionLabel Then
'continue processing
End If
Next
Next
 
M

Mark Jones

Phillip said:
Hi Mark,

The repeater contains a collection of type RepeaterItem (therefore the type
checks that you placed will all fail because you are checking for the
controls that are within the repeater items). You should wite something like
this instead:

Dim ri As RepeaterItem
For Each ri In Repeater1.Items
Dim cntrl As Control
For Each cntrl In ri.Controls
If TypeOf cntrl Is QuestionLabel Then
'continue processing
End If
Next
Next


Thanks very much for the answer, Phillip.

I enjoyed your website too. Interesting.

Mark
 

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

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top