2013-01-02 63 views
0

我有一個DataPager,我用它來翻閱我的搜索結果。我的DataPager失去了它的結果後,我已經點擊並分頁,雖然其中一些,例如,如果我的頁面2-3次,我會得到像ID和沒有數據的標籤。DataPager分頁後丟失數據?

標記

<asp:ListView runat="server" ID="LVCAdmin"> 
    <!-- Templates here --> 
</asp:ListView> 

<asp:DataPager ID="DataPager1" PagedControlID="LVCAdmin" runat="server"> 

    <Fields> 
     <asp:NextPreviousPagerField ButtonType="Button" 
     ShowFirstPageButton="True" ShowNextPageButton="False" 
     ShowPreviousPageButton="False" /> 

     <asp:NumericPagerField /> 

     <asp:NextPreviousPagerField ButtonType="Button" 
      ShowLastPageButton="True" ShowNextPageButton="False" 
      ShowPreviousPageButton="False" /> 
     </Fields> 

</asp:DataPager> 

代碼隱藏

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
    string keyword = txtSearch.Text.Trim(); 

    List<dynamic> Cresults = AdminSearchAll(keyword); 

    if (Cresults.Count != 0) 
    {  
     LVCAdmin.DataSource = Cresults; 
     LVCAdmin.DataBind(); 

     NoResults.Visible = false; 
     LVCAdmin.Visible = true; 
    } 
    else 
    { 
     NoResults.Visible = true; 

     LVCAdmin.Visible = false; 
    } 
} 

回答

0

我想通了。我添加了一個OnPreRender="Pager_PreRender"到我的DataPager控件。以下是方法。到目前爲止,它已經按照預期工作。

protected void Pager_PreRender(object sender, EventArgs e) 
     { 
      if (IsPostBack) 
      { 
        string keyword = txtSearch.Text.Trim(); 


        List<dynamic> Cresults = AdminSearchAll(keyword); 


        if (Cresults.Count != 0) 
        { 

         LVCAdmin.DataSource = Cresults; 
         LVCAdmin.DataBind(); 

         NoResults.Visible = false; 
         LVCAdmin.Visible = true; 
        } 
        else 
        { 

         NoResults.Visible = true; 

         LVCAdmin.Visible = false; 


        } 

       } 

     }