0

im doing a pagination using repeater and im trying to change the color of the current page. Im getting the error "Object reference not set to an instance of an object". I cant debug what is wrong with my codes, so sorry because im a newbie to asp.net and vb as well. please help and thank you in advance.

<div id="pages" class="pages-top">
    <asp:Repeater ID="rptPages" runat="server">
        <HeaderTemplate>
            <div>Page&nbsp</div>
        </HeaderTemplate>
        <ItemTemplate>
            <a >
                <asp:LinkButton ID="btnPage" runat="server"
                    CommandName="Page" CommandArgument="<%# Container.DataItem%>">
                    &nbsp<%# Container.DataItem%></asp:LinkButton>
            </a>
        </ItemTemplate>
    </asp:Repeater>
</div>

Public Property PageNumber() As Integer
    Get
        If ViewState("PageNumber") IsNot Nothing Then
            Return Convert.ToInt32(ViewState("PageNumber"))
        Else
            Return 0
        End If
    End Get
    Set(ByVal value As Integer)
        ViewState("PageNumber") = value
    End Set
End Property

 Public Sub LoadData()
    Dim pgitems As New PagedDataSource()
    Dim dv As New DataView(OutOfStockDt)

        ......

    pgitems.DataSource = dv
    pgitems.AllowPaging = True
    pgitems.PageSize = intPageItems
    pgitems.CurrentPageIndex = PageNumber

         .......

    rptPages.DataSource = pages
    rptPages.DataBind()

         ........

    rptOutOfStock.DataSource = pgitems
    rptOutOfStock.DataBind()

End Sub

Protected Sub rptPages_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles rptPages.ItemDataBound

    Dim lnkPage As LinkButton

    lnkPage = e.Item.FindControl("btnPage")
    --> im getting the error from the code below (if condition)
    **If lnkPage.CommandArgument.ToString = PageNumber.ToString** Then
        lnkPage.Enabled = False
        lnkPage.ForeColor = Drawing.Color.Black
    End If
End Sub
sd4ksb
  • 241
  • 1
  • 4
  • 16

2 Answers2

1

I fixed the error by validating first the ListItemType prior to finding the control. Im just new in web developing, my thoughts here is maybe I didn't clearly pointed out the location of the control that I'm looking for that is why a null/nothing value was being returned. here's the updated code,

If e.Item.ItemType = ListItemType.Item Then
    lnkPage = e.Item.FindControl("btnPage")
    If lnkPage.CommandArgument.ToString() = PageNumber.ToString() Then
       lnkPage.Enabled = False
       lnkPage.ForeColor = Drawing.Color.Black
    End If
End If
sd4ksb
  • 241
  • 1
  • 4
  • 16
0

This is the error you get when you try to use a variable that is still null/Nothing.

Either the call to FindControl() failed, and the lnkPage variable is still null/Nothing, or it succeeds and the CommandArgument property is null/Nothing. You can set a breakpoint here to see which one.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • i do get a nothing value in the CommandArgument, is it because the FindControl failed? Is there something wrong with my code with regards to the FindControl? – sd4ksb May 08 '14 at 16:58
  • A blank/null container.dataitem might cause this. – Joel Coehoorn May 08 '14 at 17:00
  • im sorry joel i didnt get it. there's a value with regards to the container.dataitem because the pagination is working, its just i want the current page to change color. it appears to me that i cant point to the btnPage correctly that's why im getting a null/nothing value in the commandArguments? – sd4ksb May 08 '14 at 17:18
  • hi joel thanks for your help. i fixed it, i need to identify the ListItemType first prior to finding the control. – sd4ksb May 08 '14 at 17:29
  • Provide your fix as an answer to your own question. – Joel Coehoorn May 08 '14 at 18:04
  • i cant do it right now, it says since i dont have enough reputation i can answer it after 8hrs... i'll do it tomorrow... thanks again.. – sd4ksb May 08 '14 at 18:46