user3966883
user3966883

Reputation:

Check value in ListView row & enable control in vb.net

I have ListView Control in my web application. Now If a column found with value "Accepted" then I want a HyperLink control to be visible or else visible should be false. So I used FindControl property from Listview but it's showing error "Object Reference not set as an instance of an object".

Private Sub PMS_online_orders_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
        Dim lblStatus As Label = CType(orderList.FindControl("status"), Label)
        Dim lblDecline As HyperLink = CType(orderList.FindControl("decline"), HyperLink)
        If lblStatus.Text = "Accepted" Then
            lblDecline.Visible = True
        End If
    End Sub

Listview

<ItemTemplate>
                     <tbody>
                         <tr>
                             <td><asp:Label ID="Label1" runat="server" Text='<%# Eval("OrderID") %>'></asp:Label></td>
                             <td><asp:Label ID="Label2" runat="server" Text='<%# Eval("name") %>'></asp:Label></td>
                             <td align="center"><asp:ImageButton CssClass="img-thumbnail" ID="ImageButton1" runat="server"
                                           ImageUrl='<%# Eval("prescriptionLink")%>' Style="cursor: pointer"
                                           OnClientClick="return LoadDiv(this.src);" /></td>
                             <td><asp:Label ID="Label4" runat="server" Text='<%# Eval("mobileNumber") %>'></asp:Label></td>
                             <td><asp:Label ID="Label6" runat="server" Text='<%# Eval("address1") + " " + Eval("address2") + " " + Eval("landmark") + " " + Eval("zip") + " " + Eval("city") %>'></asp:Label></td>
                             <td><asp:Label ID="Label3" runat="server" Text='<%# Eval("customerRemark") %>'></asp:Label></td>
                             <td>
                                 <asp:Label ID="status" runat="server" Text='<%# Eval("status") %>'></asp:Label><br />
                                 <asp:HyperLink ID="decline" CssClass="decline-order-icon" NavigateUrl='<%# "decline-order.aspx?orderID=" & Eval("orderID") %>' Target="_blank" runat="server" Visible="false"></asp:HyperLink>
                             </td>
                         </tr>
                     </tbody>
                        </ItemTemplate>

Upvotes: 0

Views: 290

Answers (1)

Neal
Neal

Reputation: 811

In order to find the controls in your ListView you need to look in each "itemtemplate."

    Protected Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender

    For Each lvi As ListViewItem In orderList.Items
        Dim lblStatus As Label = CType(lvi.FindControl("status"), Label)
        Dim lblDecline As HyperLink = CType(lvi.FindControl("decline"), HyperLink)
        If lblStatus.Text = "Accepted" Then
            Label1.Text = "yes"
        End If
    Next
End Sub

Or you can do it on the ItemDataBound event:

Protected Sub orderList_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles orderList.ItemDataBound
    Dim lvi As ListViewItem = CType(e.Item, ListViewItem)

    Dim lblStatus As Label = CType(lvi.FindControl("status"), Label)
    Dim lblDecline As HyperLink = CType(lvi.FindControl("decline"), HyperLink)
    If lblStatus.Text = "Accepted" Then
        lblDecline.Visible = True
    End If
End Sub

Upvotes: 1

Related Questions