【问题标题】:repeater.findcontrol not working on content pagerepeater.findcontrol 在内容页面上不起作用
【发布时间】:2018-02-08 19:30:42
【问题描述】:

我有一个非内容(无母版)页面,其中包含一个可以按我想要的方式执行的转发器,但是当我将相同的代码移动到内容页面(有母版)时,RepeaterItems 循环中的 findControl 不再有效.

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 练习

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)

后用户交互处理:

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

尝试查找“rby”时,在用户后交互处理中失败。生成的 HTML 的唯一区别是,在内容页面中,控件 ID 获得 MainContent_ 前缀。

我能做些什么来解决这个问题?

【问题讨论】:

    标签: asp.net vb.net findcontrol asprepeater content-pages


    【解决方案1】:

    我发现了我的问题。实际上是我在一个

    中绑定了转发器
    If Not IsPostBack Then
    

    阻止,我显然不应该这样做。

    【讨论】:

      【解决方案2】:

      如果代码位于子页面,而Repeater位于母版页本身,则需要使用FindControl指定母版页并将Repeater定位在那里。

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

      然后

      For Each questionRow As RepeaterItem In rpt.Items
      

      (用代码翻译器从C#翻译成VB,所以可能有点偏,在C#中是Repeater rpt = Master.FindControl("Repeater1") as Repeater;

      【讨论】:

      • 两者都在子页面中。环境中的唯一区别是有一个母版页。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 2018-08-04
      • 2011-10-14
      • 1970-01-01
      相关资源
      最近更新 更多