2012-05-18 96 views
0

我有這個奇怪的問題與C#asp.net listview,我無法指出確切的原因和問題。這裏是場景數據源填充,列表視圖綁定,但顯示EmptyDataTemplate

我有一個使用AutoCompleteExtender的搜索文本框。在PageLoad()中,listview將填充從DataTable中提取的一堆數據。當某人在文本框中輸入內容時,從webservice獲取結果,將結果填充到DataTable中,並且listview將綁定到DataTable。

一切工作正常 - listview綁定與DataPager最初正常工作。在列表視圖的第一頁,如果用戶輸入搜索,listview將綁定並顯示新的結果。

但是,當我在第2頁或更多時,listview綁定但顯示EmptyDataTemplate。我檢查了DataTable,並確定它在listview.DataBind之前填充了新數據。這個問題只發生在我離開列表視圖的第1頁時。

ASPX

<asp:ListView ID="productList" runat="server" onitemcommand="productList_ItemCommand" DataKeyNames="PrimaryID"> 

      <LayoutTemplate> 
      <table> 
      <tr runat="server"> 
       <th runat="server">Actions</th> 
       <th runat="server">PrimaryID</th> 
       <th runat="server">Product</th> 
      <th runat="server">Description</th> 
      </tr> 
      <tr runat="server" id="itemPlaceholder" /> 
      </table> 
     <asp:DataPager runat="server" ID="productDataPager" PageSize="20" PagedControlID="productList" QueryStringField="pageNumber"> 
      <Fields> 
       <asp:NextPreviousPagerField ButtonType="Image" ShowFirstPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" FirstPageText="|&lt;&lt; " /> 
       <asp:NumericPagerField ButtonCount="10" /> 
       <asp:NextPreviousPagerField ButtonType="Image" ShowLastPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" LastPageText=" &gt;&gt;|" /> 

      </Fields> 
      </asp:DataPager>  
     </LayoutTemplate> 

     <ItemTemplate> 
      <tr id="Tr1" class="even" runat="server"> 
      <td> 
       <asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit_product"/> 
      </td> 

      <td"> 
       <asp:Label ID="primarylbl" runat="Server" Text='<%#Bind("PrimaryID") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="productlbl" runat="Server" Text='<%#Bind("Product") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="descriptionlbl" runat="Server" Text='<%#Bind("Description") %>' />   </td> 
      </tr> 
     </ItemTemplate> 
     <AlternatingItemTemplate> 
     <tr id="Tr1" class="odd" runat="server"> 
      <td> 
       <asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit_product"/> 
      </td> 

      <td> 
       <asp:Label ID="primarylbl" runat="Server" Text='<%#Bind("PrimaryID") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="productlbl" runat="Server" Text='<%#Bind("Product") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="descriptionlbl" runat="Server" Text='<%#Bind("Description") %>' /> 
      </td> 
      </tr> 

     </AlternatingItemTemplate> 

     <EmptyDataTemplate> 
     No Records Found 
     </EmptyDataTemplate> 
      </asp:ListView> 

代碼隱藏

protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       string productkey = "0"; 
       getWeb(productkey); //call WebService to get all Products 

      } 

     } 

    private void createTable(Products[] product) 
     { 


      DataTable productTable = new DataTable(); 
      productTable.Columns.Add(new DataColumn("PrimaryID", typeof(string))); 
      prouctTable.Columns.Add(new DataColumn("Product", typeof(string))); 
      productTable.Columns.Add(new DataColumn("Description", typeof(string))); 

      for (int i = 0; i < product.Length; i++) 
      { 

       DataRow dr = productTable.NewRow(); 
       dr["PrimaryID"] = product[i].PrimaryID.ToString(); 
       dr["Product"] = product[i].Product.ToString(); 
       dr["Description"] product[i].Description.ToString(); 


       productTable.Rows.Add(dr); 
       productTable.AcceptChanges(); 
      } 


      bindtoList(productTable); 

protected void bindtoList(DataTable prodTab) 
     { 
      if (productList.DataSource == null) 
      { 
       productList.DataSource = prodTab; 
       productList.DataBind(); 

       Updatepanel1.Update(); 
      } 
      else 
      { 
       productList.DataSource = null; 
       productList.DataSource = proTab; 
       productList.DataBind(); 
      } 


      if (prodTab.Rows.Count > 20) 
      { 
       ((DataPager)productList.FindControl("productDataPager")).Visible = true; 
      } 
      else 
      { 

       if (((DataPager)productList.FindControl("productDataPager")) != null && ((DataPager)productList.FindControl("productDataPager")).Visible == true) 
       { 
        ((DataPager)productList.FindControl("productDataPager")).Visible = false; 
       } 
      } 

     } 
+0

你可以發佈你的HTML標記和代碼? –

+0

添加了代碼。在問題出在哪裏,你最初的想法是什麼? – Dee

+0

當這個'createTable'被調用? –

回答

0

我認爲你需要從DataPager刪除QueryStringField="pageNumber"

問題是當你從頁面號搜索任何東西時。 x如果結果小於x頁面列表視圖顯示空數據模板。

但是,如果結果有x或更多的頁面列表視圖顯示結果包含在頁面號碼。 X。

而當您的搜索結果x或更多頁面,如果您更改包含listview的新結果中的任何頁面,listview的數據源將更改爲最初的頁面。

如果您刪除QueryStringField="pageNumber"部分,問題將得到解決。

編輯
使用此爲您<LayoutTemplate>

<LayoutTemplate> 
      <table> 
      <tr runat="server"> 
       <th runat="server">Actions</th> 
       <th runat="server">PrimaryID</th> 
       <th runat="server">Product</th> 
      <th runat="server">Description</th> 
      </tr> 
           <tr ID="itemPlaceholder" runat="server"> 
           </tr> 
          </table> 
         </td> 
        </tr> 
        <tr runat="server"> 
         <td runat="server"> 
          <asp:DataPager ID="DataPager1" runat="server" pagesize="20"> 
           <Fields> 
       <asp:NextPreviousPagerField ButtonType="Image" ShowFirstPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" FirstPageText="|&lt;&lt; " /> 
       <asp:NumericPagerField ButtonCount="10" /> 
       <asp:NextPreviousPagerField ButtonType="Image" ShowLastPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" LastPageText=" &gt;&gt;|" /> 

      </Fields> 
          </asp:DataPager> 
         </td> 
        </tr> 
       </table> 
      </LayoutTemplate> 
+0

刪除了QueryStringField,我無法移動到不同的頁面。沒有它,DataPager似乎無法正常工作。我錯過了什麼嗎? – Dee

0

我認爲getWeb(的ProductKey);應該在「if(!PostBack)」之外調用,因爲更改頁面會創建一個回發(而不是新鮮的Get),所以你的表格將爲空(或者至少這是它在網格中的工作原理)。

+0

但是如果'getWeb(productkey);'在if(!PostBack)之外調用'',那麼'productkey'將會是** Unassigned variable **。 –

+0

老實說,我希望這個表在bindtoList函數中爲null。至少我會知道該找什麼。但問題在於傳遞的DataTable不爲null。我能夠從bindtoList函數中的DataTable prodTab中提取數據 – Dee

0

我找到了一種解決方法,可能是一個愚蠢的解決方法,因爲它並不能真正解決問題的真正原因。

這裏的變通方法:

由於列表視圖將僅顯示搜索結果時,現有列表是在PagerNumber = 1,I重置了DataPager的,使用DataPager.SetPageProperties(0,DataPager.PageSize,真)。這工作!

我還在納悶爲什麼我有這個問題,開始與...

不管怎麼說,謝謝大家的幫助。