对象引用未设置为寻呼机行中下拉列表的对象实例

时间:2014-04-02 13:52:51

标签: asp.net vb.net gridview nullreferenceexception

我处境很奇怪。我得到了一个下拉列表中的“对象引用未设置为对象实例”错误,该下拉列表存在于gridview的pagerrow中。奇怪的是我每次都没有得到这个错误,但只是在一些不幸的时候。当我发现这个错误但是失败时,我已经尝试调试这种情况。

这是我的HTML

    <asp:GridView ID="gvVendor" runat="server" Width="100%" AutoGenerateColumns="False" AllowPaging="true"  PageSize="10" AllowSorting="true"
                  CssClass="mGrid" ShowFooter="False" CellSpacing="0" CellPadding="0" EnableTheming="false" SelectedRowStyle-CssClass="DashboardDataGridSelectedItem" DataKeyNames="ev_no" OnDataBound="gvVendor_DataBound" >
                  <AlternatingRowStyle CssClass="mGridAlternate"></AlternatingRowStyle>
        <EmptyDataTemplate>No vendors found</EmptyDataTemplate>
        <PagerTemplate>
                <table cellspacing="8px" width="100%">
                    <tr>
                        <td align="left" style="text-align:left">
                            <asp:DropDownList ID="ddl_rowsPerPage" runat="server" AutoPostBack="true"
                                OnSelectedIndexChanged="ddl_rowsPerPage_SelectedIndexChanged"></asp:DropDownList>
                        </td>

                        <td style="text-align:right">
                            <%=gvVendor.PageIndex + 1%> of <%=gvVendor.PageCount%>
                            <asp:DropDownList ID="ddl_Paging" runat="server" AutoPostBack="true" 
                                OnSelectedIndexChanged="ddl_Paging_SelectedIndexChanged"></asp:DropDownList>
                        </td>
                        <td>

                        </td>
                        <td style="text-align:right">
                            <%=CType(Session("VendorGridList"), System.Data.DataView).Count%> rows returned
                        </td>
                     </tr>
                </table>
         </PagerTemplate>
    <Columns>
        some columns
    <Columns>
</asp:GridView>

我面对错误的代码位于gridview的数据绑定功能

Protected Sub gvVendor_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        'Dropdownlist for page number

        Dim ddl_paging As DropDownList = CType(gvVendor.BottomPagerRow.FindControl("ddl_Paging"), DropDownList)

        For count As Integer = 0 To gvVendor.PageCount - 1
            Dim curr As Integer = count + 1
            Dim item As New ListItem(curr.ToString(), curr.ToString())

            If count = gvVendor.PageIndex Then
                item.Selected = True
            End If

            ddl_paging.Items.Add(item)
        Next

        'Dropdownlist for pagesize
        Dim ddl_row As DropDownList = CType(gvVendor.BottomPagerRow.FindControl("ddl_rowsPerPage"), DropDownList)

        ddl_row.Items.Add(New ListItem("All", "all"))
        For count As Integer = 10 To CType(gvVendor.DataSource, DataView).Count
            Dim item As New ListItem(count.ToString(), count.ToString())

            If count = gvVendor.PageSize Then
                item.Selected = True
            End If

            ddl_row.Items.Add(item)
            count += 9
        Next

    End Sub

最初我认为当我用空数据视图绑定它时,我得到了这个。所以我模拟了这个场景,我确实遇到了一次问题,但是在第二次运行时,应用程序运行成功。

如果您能找到问题,请告诉我。

0 个答案:

没有答案