repeater.findcontrol not working on content page

472 views Asked by At

I have a non content (no master) page with a repeater that performs as I want it to, but when I move the same code to a content page (with master), the findControl in a loop of RepeaterItems no longer works.

aspx:

<ItemTemplate>
            <div class="row" id="qrow" runat="server" data-id='<%#Eval("callQuestionID") %>' data-type='<%#Eval("callQuestionResponseType") %>' data-parent='<%#Eval("callQuestionParent") %>'>
                <div class="col-md-4">
                    <asp:Label ID="questonTextLabel" runat="server" Text='<%# Eval("callQuestionText") %>'></asp:Label>
                </div>
                <div class="col-md-4">
                    <asp:Panel ID="Panel1" runat="server"></asp:Panel>
                </div>
            </div>
        </ItemTemplate>

ItemDataBound exerp

Dim newRBY As New RadioButton
                newRBY.InputAttributes.Add("data-id", CType(e.Item.DataItem, DataRowView)("callQuestionID"))
                newRBY.InputAttributes.Add("data-idy", CType(e.Item.DataItem, DataRowView)("callQuestionID"))
                newRBY.ID = "rby"
                newRBY.Text = "Yes"
                newRBY.GroupName = "qid" & CType(e.Item.DataItem, DataRowView)("callQuestionID")
                CType(e.Item.FindControl("Panel1"), Panel).Controls.Add(newRBY)
                Dim newRBN As New RadioButton
                newRBN.InputAttributes.Add("data-id", CType(e.Item.DataItem, DataRowView)("callQuestionID"))
                newRBN.InputAttributes.Add("data-idn", CType(e.Item.DataItem, DataRowView)("callQuestionID"))
                newRBN.ID = "rbn"
                newRBN.Text = "No"
                newRBN.GroupName = "qid" & CType(e.Item.DataItem, DataRowView)("callQuestionID")
                CType(e.Item.FindControl("Panel1"), Panel).Controls.Add(newRBN)

Post user interaction processing:

For Each questionRow As RepeaterItem In questionRepeater.Items
    ...
    Dim rby As RadioButton = CType(questionRow.FindControl("rby"), RadioButton) ****** Fails Here *****
                If rby.Checked Then
                    dataAccess.callQuestionAnswerTable_Insert(callIDInteger, CInt(rby.InputAttributes("data-id")), "true")
                ElseIf CType(questionRow.FindControl("rbn"), RadioButton).Checked Then
                    dataAccess.callQuestionAnswerTable_Insert(callIDInteger, CInt(rby.InputAttributes("data-id")), "false")
                End If

It fails in the post user interaction processing when trying to find 'rby'. The only difference in generated HTML is that in the content page the controls ids get a MainContent_ prefix.

What can I do to resolve this?

2

There are 2 answers

1
VDWWD On

If the code is located on the child page, while the Repeater is located on the Master Page itself, you need to specify the Master page with FindControl and locate the Repeater there.

Dim rpt As Repeater = CType(Master.FindControl("Repeater1"),Repeater)

And then

For Each questionRow As RepeaterItem In rpt.Items

(translated from C# to VB with a code translator, so it may be a little off, in C# it is Repeater rpt = Master.FindControl("Repeater1") as Repeater;)

0
Dan Wier On

I found my problem. It was actually that I had the binding of the repeater in a

If Not IsPostBack Then

block and I should not have apparently.