0

我需要能够隐藏从我的动态项目符号列表(构建在 DetailsView 内部)中显示的 2 个选项。每次我尝试通过 BulletedList 编写循环时,都会收到一条错误消息,指出它不是集合类型,所以我想我可以循环通过 DetailsView 来找到我想要隐藏的项目。

我无法更改 SQL,因为这个特殊的项目符号列表在 2 个不同的页面上使用,只是在一个页面上,我只需要显示与 ID 关联的 4 个项目中的 2 个。

<asp:TemplateField HeaderText="Answer(s)" SortExpression="PicklistID">
        <ItemTemplate>
            <asp:HiddenField ID="hiddenPicklistID" runat="server"  
            Value='<%# Bind("PicklistID") %>' />
            <asp:BulletedList ID="blText" runat="server" DataSourceID="dsPicklist" 
            DataTextField="TEXT">
            </asp:BulletedList>
        <asp:SqlDataSource ID="dsPicklist" runat="server" 
        ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>" 
        SelectCommand="SELECT p.TEXT FROM PICKLIST p 
                       JOIN C_Survey_Questions c 
                       ON p.PICKLISTID = c.PicklistID 
                       AND c.QuestionID = @QuestionID 
                       AND c.SurveyID = @SurveyID 
                       WHERE p.PICKLISTID IS NOT NULL 
                       AND c.PicklistID IS NOT NULL">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="SurveyID" 
                PropertyName="SelectedValue" Type="Int32" />
                <asp:ControlParameter ControlID="hiddenQuestionID" Name="QuestionID" 
                PropertyName="Value" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
        </ItemTemplate>
    </asp:TemplateField>

我试过了:

    Protected Sub blText_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim blText As BulletedList
    For Each BulletedListItem In blText

    Next
End Sub

但 Visual Studio 告诉我它不是集合类型。所以我想我可以在下面的代码中放一个 For Each 。我不知道如何遍历DetailsView,有人可以在这里指出正确的方向吗?

Protected Sub dvSurveyQuestions_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dvSurveyQuestions.DataBound
End Sub

更新 2/6:我使用 FindControl 声明了我的 BulletedList 并且没有更多错误说 BulletedList 没有声明。

Protected Sub blText_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim blText As BulletedList = dvSurveyQuestions.FindControl("blText")
    For Each i As ListItem In blText.Items

    Next
End Sub

另一个更新:我需要为 33 的唯一 QuestionID 获取 blText。 QuestionID 是一个整数,但我不知道如何将 HiddenField 与整数字段相关联。在这段代码中,“Is”被下划线表示Is operator does not accept opeands of type 'Integer.'所以我将 Is 更改为 = 并得到错误Operator = is not defined for types HiddenField and Integer.

Dim QuestionID = DirectCast(dvSurveyQuestions.FindControl("hiddenQuestionID"), HiddenField)
    If QuestionID Is 33 Then
        Dim blText As BulletedList = dvSurveyQuestions.FindControl("blText")
        For intCursor = (blText.Items.Count - 1) To 0 Step -1
            If blText.Items(intCursor).Text = "Self Directed" Or "Systems" Then
                blText.Items.RemoveAt(intCursor)
            End If
        Next
    End If

这是有效的

 Dim QuestionID As Integer = CInt(CType(dvSurveyQuestions.FindControl("hiddenQuestionID"), HiddenField).Value)
    If QuestionID = 33 Then
        Dim blText As BulletedList = dvSurveyQuestions.FindControl("blText")
        For intCursor = (blText.Items.Count - 1) To 0 Step -1
            If blText.Items(intCursor).Text = "Self Directed" Or blText.Items(intCursor).Text = "Systems" Then
                blText.Items.RemoveAt(intCursor)
            End If
        Next
    End If
4

1 回答 1

0

从 BulletList 的 Items 属性循环。

    For Each i As ListItem In blText.Items

    Next

这可能更具体到你的问题......

    For intCursor = (blText.Items.Count - 1) To 0 Step -1

        If blText.Items(intCursor).Text = "TextValueToRemove" Then

            blText.Items.RemoveAt(intCursor)

        End If

    Next
于 2012-02-02T22:38:03.400 回答