Search code examples
asp.netvb.netlistviewdatapager

ASP.NET VB DataPager buttons not required when only one record of data


I am using a datapager inside a nested Listview succesfully. When only one record of data is available the next previous last and first buttons are faded as per normal. However I would like them to not appear at all.

The aspx code I have is:

                <asp:ListView ID="Pictures" runat="server" DataSourceID="SqlDataSource2" >

                    <EmptyDataTemplate>
                        <span>No data was returned.</span>
                    </EmptyDataTemplate>
                    <ItemTemplate>
                        <span style="">
                            <br />
                            <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' ToolTip='<%# Eval("ToolTip") %>'
                                Height="150px" />
                            <br />
                            <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
                            <br />
                            <br />
                        </span>
                    </ItemTemplate>

                    <LayoutTemplate>
                        <div id="itemPlaceholderContainer" runat="server" style="">
                            <span runat="server" id="itemPlaceholder" />
                        </div>
                        <div style="clear: both;">
                            <asp:DataPager ID="DataPager1" runat="server" PageSize="1">
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
                                </Fields>
                            </asp:DataPager>
                        </div>
                    </LayoutTemplate>
                </asp:ListView>

The code behind that I have attempted to use is:

    Dim pager As DataPager = CType(e.Item.FindControl("DataPager1"), DataPager)

    If (Not pager Is Nothing) Then
        pager.Visible = (pager.PageSize < pager.TotalRowCount)
    End If

However the pager is always nothing in the debugger i.e it can't find the control.

This may be because I have not selected the event handler correctly (I've tried several) or the method may be wrong.

Any advice gratefully received.


Solution

  • Try it in the listview's databound event and also call the FindControl function from the listview.

    Protected Sub Pictures_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles Pictures.DataBound
          Dim pager As DataPager = CType(Pictures.FindControl("DataPager1"), DataPager)
    
          If (Not pager Is Nothing) Then
              pager.Visible = (pager.PageSize < pager.TotalRowCount)
          End If
    
    
    End Sub