2011-03-11 51 views
1

我想ASP.NET DataPager控件鏈接到ASP.NET的ListViewASP.NET DataPager的

<div> 
    <asp:ListView ID="ListView1" runat="server"> 
     <ItemTemplate> 
      <a href="#"><%#DataBinder.Eval(Container.DataItem,"LinkText")%></a> 
     </ItemTemplate>   
    </asp:ListView> 

    <asp:DataPager runat="server" ID="DataPager1" PagedControlID="ListView1" PageSize="2"> 
     <Fields> 
      <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" /> 
     </Fields> 
    </asp:DataPager>   

</div> 

在Page_Load中我加載使用數據表

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Page.IsPostBack == false) 
     { 
      _fl.ConnectToSQLServer(); 

      DataTable dt = new DataTable(); 

      dt.Load(_fl.GetCategories()); 

      ListView1.DataSource = dt; 
      ListView1.DataBind(); 

      _fl.DisconnectFromSQLServer(); 
     } 
    } 

ListView的數據顯示正常,但DataPager的呢不工作,因爲它應該

任何幫助將不勝感激。謝謝。從Page_Load中

回答

4

切換到的OnPreRender用於填充ListView和跳過的IsPostBack檢查

嘗試

protected override void OnPreRender(EventArgs e) 
{ 
      base.OnPreRender(e); 
      _fl.ConnectToSQLServer(); 

      DataTable dt = new DataTable(); 

      dt.Load(_fl.GetCategories()); 

      ListView1.DataSource = dt; 
      ListView1.DataBind(); 

      _fl.DisconnectFromSQLServer(); 


} 
+0

謝謝你,它的工作。 :) –

+0

爲我工作:) – BG100