2

Trying paging of a grid.

  <PagerStyle HorizontalAlign="Right" CssClass="paging"/>
          <PagerTemplate>
            <table width="100%">
                <tr>
                    <td style="text-align:left; width:50%">

                        <asp:LinkButton ID="lnkPrv" Visible="false" CommandName="Page" CommandArgument="Prev" runat="server">Previous</asp:LinkButton>
                    </td>
                    <td style="text-align:right; width:50%;padding-left:50%;">                        
                        <asp:LinkButton ID="lnkNext" CommandName="Page" CommandArgument="Next" runat="server">Next</asp:LinkButton>
                    </td>
                </tr>
            </table>
        </PagerTemplate>

Code behind is below

    protected void gvProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Literal1.Visible = gvProduct.PageIndex == 0;

        LinkButton lnkPrv = (LinkButton)gvProduct.BottomPagerRow.FindControl("lnkPrv");
        LinkButton lnkNext = (LinkButton)gvProduct.BottomPagerRow.FindControl("lnkNext");

        lnkPrv.Visible = e.NewPageIndex > 0;
        lnkNext.Visible = e.NewPageIndex < gvProduct.PageCount - 1;
        gvProduct.PageIndex = e.NewPageIndex;
        FillGrid();
    }

The code does not give any error. I can see it set the visible property to true/false. But actual control on page remain same (always visible on every page). '

What is wrong?

2
  • Can you post the code for your FillGrid method? Commented Feb 23, 2011 at 18:04
  • Do any of the answers help? If so, can you mark this question answered? Commented Feb 24, 2011 at 14:49

1 Answer 1

2

If your FillGrid() method is rebinding gvProduct (i.e. gvProduct.DataBind()) then lnkPrv and lnkNext Visible values are going to use their defaults from the markup when databinding. You need to set the visibility of these controls in an event handler for RowDataBound event of gvProduct.

protected void gvProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
            Literal1.Visible = gvProduct.PageIndex == 0;
            gvProduct.PageIndex = e.NewPageIndex;
            FillGrid();
}

protected void gvProduct_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Pager) {
      LinkButton lnkPrv = (LinkButton)e.Row.FindControl("lnkPrv");
      LinkButton lnkNext = (LinkButton)e.Row.FindControl("lnkNext");
      lnkPrv.Visible = gvProduct.PageIndex > 0;
      lnkNext.Visible = gvProduct.PageIndex < gvProduct.PageCount - 1;
     }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.