访问 ListView 内的 DataList
我可以使用 C# 和 VB.nET
我在 ListView(部分)中有一个 DataList(问题)。 ListiView 用于保存部分。 DataList 保存一个部分的问题。假设我有 3 个部分,每个部分有 2 个问题。
<asp:ListView ID="lvSection" runat="server">
<LayoutTemplate>
<div id="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<div>
<p><%#Eval("Section")%>
<asp:HiddenField ID="hfSectionId" runat="server" Value='<%#Eval("SectionId")%>' />
<hr />
</p>
</div>
<asp:DataList ID="dlQuestion" runat="server" >
<ItemTemplate>
<asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'></asp:Label>
<asp:HiddenField ID="hfQuestionId" runat="server" Value='<%# Eval("QuestionId") %>' />
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:ListView>
<br/>
<asp:Button runat="server" Text="Submit" onclick="Submit_Click" />
当单击“提交”按钮时,我试图访问 DataList dlQuestion,如下所示:
Protected Sub Submit_Click(ByVal sender As Object, ByVal e As EventArgs)
'but I need to loop through all DataLists inside the ListView
'Maybe there are ways to get all the DataLists into a collection and then can loop through each one of them
'this will get only one DataList. Here's the pseudocode
Dim question As DataList = lvSection.FindControl("dlQuestion")
For Each item As DataListItem In quest.Items
Dim questionId As HiddenField = item.FindControl("hfQuestionId")
Next
End Sub
但它没有得到任何返回,问题总是一无所获。我认为这是因为ListView现在有3个DataList,由于3个部分,它再也找不到DataList dlQuestion了。如何从后面的代码访问ListView的这些DataList?我需要循环遍历 DataList 的每个控件。
谢谢。
I'm OK with C# and VB.nET
I have a DataList (Question) inside the ListView (Section). ListiView is to hold the sections. DataList holds the questions of a section. Let's say I have 3 sections, each section has 2 questions.
<asp:ListView ID="lvSection" runat="server">
<LayoutTemplate>
<div id="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<div>
<p><%#Eval("Section")%>
<asp:HiddenField ID="hfSectionId" runat="server" Value='<%#Eval("SectionId")%>' />
<hr />
</p>
</div>
<asp:DataList ID="dlQuestion" runat="server" >
<ItemTemplate>
<asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'></asp:Label>
<asp:HiddenField ID="hfQuestionId" runat="server" Value='<%# Eval("QuestionId") %>' />
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:ListView>
<br/>
<asp:Button runat="server" Text="Submit" onclick="Submit_Click" />
I'm trying to access the DataList dlQuestion when the button "Submit" is click like this:
Protected Sub Submit_Click(ByVal sender As Object, ByVal e As EventArgs)
'but I need to loop through all DataLists inside the ListView
'Maybe there are ways to get all the DataLists into a collection and then can loop through each one of them
'this will get only one DataList. Here's the pseudocode
Dim question As DataList = lvSection.FindControl("dlQuestion")
For Each item As DataListItem In quest.Items
Dim questionId As HiddenField = item.FindControl("hfQuestionId")
Next
End Sub
But it does not get anything back, question always gets nohting. I think it's because there are 3 DataList inside the ListView now, due to 3 sections, and it cannot find DataList dlQuestion anymore. How do I access these DataLists of ListView from the code behind? I need to loop through each control of the DataList .
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要这样做:
您必须首先访问 ListView 中每个项目中的数据列表,而不是在列表视图级别。
You would need to do it as this:
You have to access the data list in each item in the ListView first, not at the listview level.