Asp .Net Gridview分页

时间:2011-02-23 16:55:06

标签: c# asp.net gridview

尝试分页网格。

  <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>

背后的代码是

    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();
    }

代码不会出错。我可以看到它将visible属性设置为true / false。 但页面上的实际控制保持不变(在每个页面上始终可见)。 “

有什么问题?

1 个答案:

答案 0 :(得分:2)

如果您的FillGrid()方法重新绑定gvProduct(即gvProduct.DataBind()),则lnkPrv和lnkNext可视值将在数据绑定时使用标记中的默认值。您需要在gvProduct的RowDataBound事件的事件处理程序中设置这些控件的可见性。

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;
     }
}