访问 ListView 内的 DataList

发布于 2024-12-12 07:58:40 字数 1933 浏览 0 评论 0原文

我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无畏 2024-12-19 07:58:40

您需要这样做:

for each item As ListViewDataItem in lvSection.Items
    Dim list As DataList = item.FindControl("dlQuestion")
    If (list IsNot Nothing) Then
        For Each dlItem As DataListItem In quest.Items

            Dim questionId As HiddenField = dlItem.FindControl("hfQuestionId")

        Next
    End If
Next

您必须首先访问 ListView 中每个项目中的数据列表,而不是在列表视图级别。

You would need to do it as this:

for each item As ListViewDataItem in lvSection.Items
    Dim list As DataList = item.FindControl("dlQuestion")
    If (list IsNot Nothing) Then
        For Each dlItem As DataListItem In quest.Items

            Dim questionId As HiddenField = dlItem.FindControl("hfQuestionId")

        Next
    End If
Next

You have to access the data list in each item in the ListView first, not at the listview level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文