Home All Groups Group Topic Archive Search About

Extracting values from child controls of a Repeater

Author
21 Jan 2006 8:46 PM
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>

....

Author
21 Jan 2006 9:46 PM
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
Show quoteHide quote
"Mark Jones" wrote:

> 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>
>
> ....
>
>
Author
22 Jan 2006 4:49 PM
Mark Jones
Phillip Williams wrote:
Show quoteHide quote
> 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
Author
22 Jan 2006 8:41 PM
Phillip Williams
You are very welcome.
Show quoteHide quote
"Mark Jones" wrote:

>
> Phillip Williams wrote:
> > 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
>