2009-05-31 86 views
2

我正在爲我的大學項目製作一個網站。 該項目是網站,從Web服務獲取一切。 有人可以告訴發生了什麼事情,我如何解決它?問題:無法加載視圖狀態

在我的其中一個頁面上,我有ListView控件來顯示產品項目和Pager在同一頁面上。 在第一次頁面呈現良好,並顯示一切。當我在下一頁點擊尋呼機,它的重載頁面,但不顯示第二頁,而顯示相同的數據,如果我推一次話,我剛開:

Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request. 

我的產品頁面我安靜簡單:

<asp:ListView ID="lv_ProductList" runat="server" DataKeyNames="product_id"> 
     <LayoutTemplate> 
      <asp:PlaceHolder ID="itemPlaceholder" runat="server" /> 
      <br /> 
      <br /> 
      <asp:DataPager ID="pageTopics" runat="server" PageSize="8" PagedControlID="lv_ProductList" > 
       <Fields> 
        <asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="true" ShowNextPageButton="false" ButtonCssClass="span" /> 
        <asp:NumericPagerField /> 
        <asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="false" ShowNextPageButton="true" ShowLastPageButton="false" /> 
       </Fields> 
      </asp:DataPager> 
     </LayoutTemplate> 
     <ItemTemplate> 
      <div class="item"> 
       <img src="images/pic_1.jpg" width="91" height="105" alt="iPod" class="left" /> 
       <h3><a href="http://www.justwebtemplates.com"><%# Eval("product_name") %></a></h3> 
       <p><%# Eval("product_desc").ToString().Substring(0,90) + "..." %> </p> 
       <div><a href="http://www.freewebsitetemplates.com" class="details">details</a> <a href="http://www.freewebsitetemplates.com" class="addtocart">add to cart</a></div> 
       <div class="divider"></div> 
      </div> 
     </ItemTemplate> 
     <EmptyDataTemplate>No products found</EmptyDataTemplate> 
     <EmptyItemTemplate>No products found</EmptyItemTemplate> 
    </asp:ListView> 

而且在我後面的代碼有:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      getProductsList(); 
     } 
    } 


    public void getProductsList() 
    { 
     Store.Service myProducts = new Store.Service(); 
     var products = myProducts.getProductList(); 

     lv_ProductList.DataSource = products; 
     lv_ProductList.DataBind(); 
    } 

和Web服務:

[WebMethod] 
    public List<ws_product> getProductList() 
    { 
     using (DataClassesDataContext myProducts = new DataClassesDataContext()) 
     { 

      var productList = from p in myProducts.ws_products 
           select p; 

      return productList.ToList(); 
     } 

}

有人可以告訴發生了什麼事嗎? 此外,如果我把尋呼機外ListView,那麼我不會收到錯誤,但是,如果我點擊下一頁鏈接它顯示相同的數據集。

我該如何解決? 預先感謝您。

回答

3

在後面的代碼添加:

protected void lds_Selecting(object sender, LinqDataSourceSelectEventArgs args) 
{ 
     Store.Service myProducts = new Store.Service(); 
     args.Result = myProducts.getProductList();  
} 

添加ASP:使用LinqDataSource頁面:

<asp:LinqDataSource ID="lds" runat="server" OnSelecting="lds_Selecting" /> 


    <asp:ListView ID="lv_ProductList" runat="server" 
DataKeyNames="product_id" DataSourceID="lds"> 
      <LayoutTemplate> 
       <asp:PlaceHolder ID="itemPlaceholder" runat="server" /> 
       <br /> 
       <br /> 
       <asp:DataPager ID="pageTopics" runat="server" PageSize="8" PagedControlID="lv_ProductList" > 
        <Fields> 
         <asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="true" ShowNextPageButton="false" ButtonCssClass="span" /> 
         <asp:NumericPagerField /> 
         <asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="false" ShowNextPageButton="true" ShowLastPageButton="false" /> 
        </Fields> 
       </asp:DataPager> 
      </LayoutTemplate> 
      <ItemTemplate> 
       <div class="item"> 
        <img src="images/pic_1.jpg" width="91" height="105" alt="iPod" class="left" /> 
        <h3><a href="http://www.justwebtemplates.com"><%# Eval("product_name") %></a></h3> 
        <p><%# Eval("product_desc").ToString().Substring(0,90) + "..." %> </p> 
        <div><a href="http://www.freewebsitetemplates.com" class="details">details</a> <a href="http://www.freewebsitetemplates.com" class="addtocart">add to cart</a></div> 
        <div class="divider"></div> 
       </div> 
      </ItemTemplate> 
      <EmptyDataTemplate>No products found</EmptyDataTemplate> 
      <EmptyItemTemplate>No products found</EmptyItemTemplate> 
     </asp:ListView> 

這應該工作。

2

這不完全是我的專業領域,但我可能會提供一些概念指導。

雖然您已將頁面控制器放在頁面上,但您尚未在代碼隱藏或服務中實施分頁。

您的服務將需要接受索引和長度參數,因此瀏覽器可以說「我現在在項目20上,而且我一次只看10個,所以現在轉到db併發回項目30-39「。

您的服務必須實際支持分頁。或者,您可以將所有項目加載到頁面中一次,然後使用類似jQuery的庫來執行「虛擬」分頁,而不是回傳,但我想這超出了學校項目的範圍。

0

周杰倫,謝謝你的幫助。

在我的頭在牆上打破了一段時間後,我結束了爲我的ListView構建自定義尋呼機。 現在一切正常。