来自asp.net Listview Control的FindControl

时间:2016-05-30 11:28:42

标签: asp.net vb.net listview

我有Listview&几个radiobutton列表。在Listview里面我放了一个超链接控件。现在当从radiobuttonList特定值中获取时,只有该超链接控件应该是可见的。我尝试了从列表视图中的FIndControl但是它给出了这个错误。

指数超出范围。必须是非负数且小于集合的大小。 参数名称:index

<asp:ListView ID="orderList" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
                     <LayoutTemplate>
                         <table class="online-orders">
                     <thead>
                         <tr>
                             <th>Order ID</th>
                             <th>Patient Name</th>         
                             <th>Actions</th>
                         </tr>
                     </thead>
                             <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
                <tr>
                    <td colspan="8">
                        <asp:DataPager ID="DataPager1" runat="server" PagedControlID="orderList" PageSize="10">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
                                    ShowNextPageButton="false" />
                                <asp:NumericPagerField ButtonType="Link" />
                                <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
                            </Fields>
                        </asp:DataPager>
                    </td>
                </tr>
                             </table>
                     </LayoutTemplate>

                     <GroupTemplate>
                         <tr>
                             <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
                         </tr>
                     </GroupTemplate>

                    <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>
                                 <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>
                     </asp:ListView>

vb代码

Private Sub orderFilter_SelectedIndexChanged(sender As Object, e As EventArgs) Handles orderFilter.SelectedIndexChanged
            Dim processing As HyperLink = CType(orderList.Items(orderList.SelectedIndex).FindControl("decline"), HyperLink)
            If orderFilter.SelectedValue = "processing" Then
                processing.Visible = True
            End If
    End Sub

1 个答案:

答案 0 :(得分:0)

不是FindControl,而是orderList.Items(orderList.SelectedIndex)会给您带来此错误。我的猜测是目前ListView中没有选择任何项目,因此SelectedIndex返回-1(如docs中所述)。

请注意,这很有可能 - 您正在处理名为orderFilter的其他控件的选择更改,或至少事件处理程序名称orderFilter_SelectedIndexChanged建议如此。因此无法保证在orderList中选择任何内容。

这里的第一条建议是确保这是有意义的,并且没有错误。也许您真的需要处理orderList而不是orderFilter的选择更改?

无论如何,第二部分是检查-1,以防选择变为空:

Private Sub orderFilter_SelectedIndexChanged(sender As Object, e As EventArgs) Handles orderFilter.SelectedIndexChanged
    If orderList.SelectedIndex = -1 Then
        Return
    End If

    Dim processing As HyperLink = CType(orderList.Items(orderList.SelectedIndex).FindControl("decline"), HyperLink)
    If orderFilter.SelectedValue = "processing" Then
                processing.Visible = True
    End If
End Sub