repeater.findcontrol不工作在内容页面
问题描述:
我有一个非内容(没有主)页面与中继器执行我想要的,但是当我将相同的代码移动到内容页面(与主)时,findControl在RepeaterItems循环中不再有效。repeater.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 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)
帖子的用户交互处理:
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_前缀。
我能做些什么来解决这个问题?
答
如果代码位于子页面上,而Repeater位于Master Page本身,则需要指定Master页面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;
)
答
,我发现我的问题。这实际上是我有中继器的绑定在
If Not IsPostBack Then
块,我不应该有明显的。
两者都在子页面中。环境中唯一的区别是有一个主页面。 –