2011-02-23 86 views
2

嘗試分頁。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(); 
    } 

的代碼不給任何錯誤。我可以看到它將可見屬性設置爲true/false。 但頁面上的實際控制保持不變(每個頁面始終可見)。 '

出了什麼問題?

+0

您可以發佈您的FillGrid方法的代碼? – PhilPursglove 2011-02-23 18:04:21

+0

做任何答案都有幫助嗎?如果是這樣,你能回答這個問題嗎? – Brady 2011-02-24 14:49:21

回答

2

如果您的FillGrid()方法重新綁定gvProduct(即gvProduct.DataBind()),那麼lnkPrv和lnkNext Visible值將在數據綁定時使用標記中的默認值。您需要在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; 
    } 
}