2009-05-31 169 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构建自定义寻呼机。 现在一切正常。

相关问题