locked
Trying to hide a listview control based on a dataitem value- not working RRS feed

  • Question

  • User646364117 posted

    I have a checkbox control in a listview. I want to hide it if the value of the Block Type Column is not "Multi-Select. My code runs but nothing gets hiden

    Here is markup

     <asp:ListView ID="LvBlock" runat="server" DataSourceID="SqlBlockSource">
                                        <LayoutTemplate>
                                            <table>
                                                <tr class="myitem">
                                                    <th style="border: thin solid #C0C0C0; border-collapse: collapse; width: 100px;">Block Type</th>
                                                    <th style="border: thin solid #C0C0C0; border-collapse: collapse; width: 40px;">Selected</th>
                                                    <th style="border: thin solid #C0C0C0; border-collapse: collapse; width: 200px;">Short Text/Long Text Title</th>
                                                    <th style="border: thin solid #C0C0C0; border-collapse: collapse; width: 400px;">Long Text</th>
    
    
                                                </tr>
    
                                                <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
                                            </table>
                                        </LayoutTemplate>
    
                                        <ItemTemplate>
    
                                            <%# AddGroupingRowIfBlockNameHasChanged() %>
                                            <tr class='data<%# Container.DataItemIndex Mod 2 %>'>
                                                <td style="border: thin solid #C0C0C0; border-collapse: collapse;"><%#Eval("BlockType")%></td>
    
                                                <td style="border: thin solid #C0C0C0; border-collapse: collapse;">
                                                    <asp:CheckBox ID="chkIncluded" runat="server" /></td>
                                                <td style="border: thin solid #C0C0C0; border-collapse: collapse;"><%#Eval("shorttext")%></td>
                                                <td style="border: thin solid #C0C0C0; border-collapse: collapse;"><%#Eval("longtext")%></td>
    
    
                                            </tr>
                                        </ItemTemplate>
                                    </asp:ListView>
                                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DB_A54278_steve48ConnectionString %>"
                                        SelectCommand="SELECT  block.name as BlockName, blocktype.blocktype AS BlockType, block.shorttext, block.longtext, block.color,blocktype.typeorder AS TypeOrder, 
                                        block.included AS Included
                                        FROM block INNER JOIN blocktype ON block.blocktype_id=blocktype.id ORDER BY TypeOrder">
    
                                    </asp:SqlDataSource>

    and here is code. I put same code into both ItemDataBound and Item Created to see if either worked

    Private Sub LvBlock_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles LvBlock.ItemDataBound
            For Each item As ListViewItem In LvBlock.Items
                If item.FindControl("chkIncluded") IsNot Nothing Then
                    Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
                    Dim rowView As DataRowView = CType(dataItem.DataItem, DataRowView)
                    Dim BlockType As String = rowView("BlockType").ToString()
                    If BlockType = "Multi Select" Then
                        item.FindControl("chkIncluded").Visible = True
                    Else
                        item.FindControl("chkIncluded").Visible = False
                    End If
    
                End If
            Next
        End Sub
    
        Private Sub LvBlock_ItemCreated(sender As Object, e As ListViewItemEventArgs) Handles LvBlock.ItemCreated
    
            For Each item As ListViewItem In LvBlock.Items
                If item.FindControl("chkIncluded") IsNot Nothing Then
                    Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
                    Dim rowView As DataRowView = CType(dataItem.DataItem, DataRowView)
                    Dim BlockType As String = rowView("BlockType").ToString()
                    If BlockType = "Multi Select" Then
                        item.FindControl("chkIncluded").Visible = True
                    Else
                        item.FindControl("chkIncluded").Visible = False
                    End If
    
                End If
    
            Next
        End Sub

    Tuesday, January 28, 2020 4:11 PM

Answers

  • User1535942433 posted

    Hi sg48asp,

    Accroding to your codes , I suggest you could set the label tag of blocktype and you could find the label's value to check if the checkbox should be hiden.

    More details, you could refer to below codes:

    ASPX:

     <asp:ListView ID="LvBlock" runat="server" DataSourceID="SqlDataSource2" OnItemDataBound="LvBlock_ItemDataBound" >
                    <LayoutTemplate>
                        <table>
                            <tr class="myitem">
                                <th style="border: thin solid #C0C0C0; border-collapse: collapse; width: 100px;">Block Type</th>
                                <th style="border: thin solid #C0C0C0; border-collapse: collapse; width: 40px;">Selected</th>
                                <th style="border: thin solid #C0C0C0; border-collapse: collapse; width: 200px;">Short Text/Long Text Title</th>
                                <th style="border: thin solid #C0C0C0; border-collapse: collapse; width: 400px;">Long Text</th>
    
    
                            </tr>
    
                            <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
                        </table>
                    </LayoutTemplate>
    
                    <ItemTemplate>
    
                        <tr class="data">
                            <td style="border: thin solid #C0C0C0; border-collapse: collapse;">
                                <asp:Label ID="blocktype" runat="server" Text='<%#Eval("BlockType")%>'></asp:Label></td>
    
                            <td style="border: thin solid #C0C0C0; border-collapse: collapse;">
                                <asp:CheckBox ID="chkIncluded" runat="server" /></td>
                            <td style="border: thin solid #C0C0C0; border-collapse: collapse;"><%#Eval("shorttext")%></td>
                            <td style="border: thin solid #C0C0C0; border-collapse: collapse;"><%#Eval("longtext")%></td>
    
    
                        </tr>
                    </ItemTemplate>
                </asp:ListView>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-TestApplicationWithDatabase-20190820030542 %>"
                    SelectCommand="SELECT  block.name as BlockName, blocktype.blocktype AS BlockType, block.shorttext, block.longtext, block.color,blocktype.typeorder AS TypeOrder, 
                                        block.included AS Included
                                        FROM block INNER JOIN blocktype ON block.blocktype_id=blocktype.id ORDER BY TypeOrder"></asp:SqlDataSource>
    Protected Sub LvBlock_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs)
            If e.Item.ItemType = ListViewItemType.DataItem Then
                Dim blocktype As Label = CType(e.Item.FindControl("blocktype"), Label)
    
                If blocktype.Text.ToString() <> "Multi Select" Then
                    Dim cb As CheckBox = CType(e.Item.FindControl("chkIncluded"), CheckBox)
                    cb.Visible = False
                End If
            End If
        End Sub

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 29, 2020 8:01 AM