Search code examples
vb.netdetailsviewbulletedlist

Looping through DetailsView to hide dynamic BulletedList items


I need to be able to hide 2 options that are displayed from my dynamic bulletedlist (which is built inside of a DetailsView). Every time I try writing a loop through the BulletedList, I get an error saying that it is not a collection type, so I thought I could loop through the DetailsView to find the items I want to hide.

I can't change the SQL because this particular bulleted list gets used on 2 different pages, it's just that on one, I only need to show 2 of the 4 items that are associated with the ID.

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

I've tried:

    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

but Visual Studio tells me that it is not a collection type. So I thought I could just put a For Each in the code below. I don't know how to loop through the DetailsView though, can someone point me in the right direction here?

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

UPDATE 2/6: I declared my BulletedList by using a FindControl and there were no more errors saying the BulletedList wasn't declared.

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

ANOTHER UPDATE: I need to get the blText for the unique QuestionID of 33. The QuestionID is an Integer, but I don't know how to associate a HiddenField with an Integer field. In this code, "Is" gets underlined saying thatIs operator does not accept opeands of type 'Integer.' So I change the Is to an = and get the error 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

This is what works

 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

Solution

  • Loop from the Items property of the BulletList.

        For Each i As ListItem In blText.Items
    
        Next
    

    This is probably more specific to your problem...

        For intCursor = (blText.Items.Count - 1) To 0 Step -1
    
            If blText.Items(intCursor).Text = "TextValueToRemove" Then
    
                blText.Items.RemoveAt(intCursor)
    
            End If
    
        Next